
    W8i                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlZd dlZ ej0                  e      Z G d de	      Zd Zy)    )ValidationError)IsAuthenticated)Response)async_to_sync)ListAPIView)DjangoFilterBackend)CatchPhraseReportRepository)CatchPhrasePagination)CatchPhraseReportService)CallPhraseReportSerializer)CatchPhraseReportFilterNc                   6    e Zd ZegZeZegZe	Z
eZd Zd Zd Zy)CatchPhraseReportAPIViewc                 b    t        | j                  j                  dd       }|st        d      |S )Nactive_company_idz*User does not belong to an active company.)getattrrequestuserr   )selfr   s     F/var/www/html/dp2/backend_v2.1/server/apps/calls/views/catch_phrase.pyget_company_idz'CatchPhraseReportAPIView.get_company_id   s1    #DLL$5$57JDQ !"NOO      c                 p    | j                         }t        j                  |      }| j                  |      S )N)
company_id)r   r	   	get_callsfilter_queryset)r   r   querysets      r   get_querysetz%CatchPhraseReportAPIView.get_queryset!   s1    ((*
.88JO##H--r   c                 &   | j                         }t        t        j                  |            }| j	                         }| j                  | j                  |            }| t        t              ||      }|D 	cg c]  }	t        |	dg       s|	 }
}	| j                  |
d      }|j                         D cg c]
  \  }}||d }}}| j                  |j                  |d      S  t        | j                        ||      }|D 	cg c]  }	t        |	dg       s|	 }
}	| j                  |
d      }|j                         D cg c]
  \  }}||d }}}t        t        |      |j                  |d      S c c}	w c c}}w c c}	w c c}}w )N_cached_phrasesT)many)phrasecount)calls
graph_data)total_callsr$   r%   )r   listr	   get_phrasesr   paginate_querysetr   r   annotate_calls_asyncr   get_serializeritemsget_paginated_responsedatar   len)r   r   argskwargsr   phrasesr   pagegraph_countscallpage_with_phrases
serializerr"   r#   r%   s                  r   r'   zCatchPhraseReportAPIView.list&   s   ((*
2>>zJK$$&%%d&:&:8&DE>=)=>tWML26 _$$'$HY[]:^$ _,,->T,JJ &2%7%7%9%9MFE "E2%9   ..#(0  
 @}T%>%>?'R.6_hd'$HY[]:^Th_(():(F
DPDVDVDX
DX=65v.DX 	 
 x=__$
  	' !` `
s$   ;E=E=:F
FF	FN)__name__
__module____qualname__r   permission_classesr   serializer_classr   filter_backendsr   filterset_classr
   pagination_classr   r   r'    r   r   r   r      s3    )*1*+O-O,!.
r   r   c                 2  K   | D cg c]  }t        j                  ||       }}t        j                  |  d {   }|D ci c]  }|j                  d }}t        | |      D ]   \  }}||_        |D ]  }||xx   dz  cc<    " |S c c}w 7 Tc c}w w)Nr      )r   build_call_phrase_report_asyncasynciogatherr"   zipr    )	r$   r2   r5   tasksresultspr4   found_phrasesr"   s	            r   r*   r*   G   s      D 	!??gN 
  NNE**G)01AAHHaKL1"5'2m,#F A%  $  3  +1s+   BBBBBB9BB)django.core.exceptionsr   rest_framework.permissionsr   rest_framework.responser   asgiref.syncr   rest_framework.genericsr   django_filters.rest_frameworkr   apps.calls.repositoriesr	   apps.calls.paginationr
   apps.calls.servicesr   apps.calls.serializersr   apps.calls.filtersr   rD   logging	getLoggerr8   loggerr   r*   r@   r   r   <module>rY      sR    2 6 , & / = ? 7 8 = 6   
		8	$1{ 1fr   