
    i9iB                        d Z ddl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mZ dd
lmZmZ  G d dej(                        Z G d dej(                        Z G d dej(                        Z G d dej(                        Z G d de      Z G d dej(                        Z G d de      Z G d dej8                        Z G d dej8                        Z G d dej8                        Zy)z
Serializers for recipe APIs
    N)timezone)serializers)Avg)LocalizedDateTimeField)Customer)Company)CallCallActivity)DealershipCallSerializerDealershipNameSerializerc                   $    e Zd ZdZ G d d      Zy)CallSerializerzSerializer for calls.c                       e Zd ZeZg dZdgZy)CallSerializer.Meta)idcaller_namefrom_number	to_numbercompanystatus
transcriptdurationcostcall_idtwilio_call_sidr   N__name__
__module____qualname__r	   modelfieldsread_only_fields     E/var/www/html/dp2/backend_v2.1/server/apps/calls/serializers/calls.pyMetar      s    
 !6r$   r&   N)r   r   r   __doc__r&   r#   r$   r%   r   r      s    " "r$   r   c                        e Zd Z G d d      Zy)CustomerSerializerc                       e Zd ZeZdgZy)CustomerSerializer.MetanameN)r   r   r   r   r    r!   r#   r$   r%   r&   r+   (   s    r$   r&   Nr   r   r   r&   r#   r$   r%   r)   r)   '   s     r$   r)   c                        e Zd Z G d d      Zy)CompanySerializerc                       e Zd ZeZddgZy)CompanySerializer.Metar   r,   N)r   r   r   r   r    r!   r#   r$   r%   r&   r1   .   s    r$   r&   Nr-   r#   r$   r%   r/   r/   -   s       r$   r/   c                   H    e Zd Z ej                         Z G d d      Zd Zy)CallActivitySerializerc                       e Zd ZeZg dZy)CallActivitySerializer.Meta)user_fullnameactionperformed_atduration_playedN)r   r   r   r
   r    r!   r#   r$   r%   r&   r5   6   s    Or$   r&   c                 l    t        |j                  dd       }|r|j                   d|j                   S y )Nprofile )getattruser
first_name	last_name)selfobjr;   s      r%   get_user_fullnamez(CallActivitySerializer.get_user_fullname:   s9    #((It4(()7+<+<*=>>r$   N)r   r   r   r   SerializerMethodFieldr6   r&   rC   r#   r$   r%   r3   r3   3   s#    5K557MP Pr$   r3   c                       e Zd ZdZ e       Z ed      Z ej                         Z
 ej                         Z ej                         Z ed      Z G d dej                         Zd Zd Zd Zy	)
CallDetailSerializerz Serializer for call detail view.T)	read_onlyc                   B    e Zd Zej                  j
                  g dz   Zy)CallDetailSerializer.Meta)
created_atread_attransfer_status	sentimentsummaryr   customertransfer_user
activitiesbot_typecall_time_category
dealershipN)r   r   r   r   r&   r!   r#   r$   r%   r&   rI   J   s    $$++ /
 
r$   r&   c                     t         j                  j                  |j                  |j                        j                         }|rt        |      j                  S y N)phoner   r   objectsfilterr   r   firstr)   datarA   rB   rO   s      r%   get_customerz!CallDetailSerializer.get_customerZ   E    ##**#++*V\\^%h/444r$   c                    |j                   r|j                   j                  }t        |j                   d      ro|j                   j                  rY|j                   j                  j                  }|j                   j                  j
                  }|s|r| d| j                         }|j                   j                  |dS y )Nr;   r<   )r   r,   )rP   usernamehasattrr;   r?   r@   stripr   )rA   rB   	full_namer?   r@   s        r%   get_transfer_userz&CallDetailSerializer.get_transfer_user`   s    ))22Is(()49J9J9R9R ..66AA
--55??	#-,a	{ ; A A CI ''**!  r$   c                 f    |j                   j                  d      }t        |d      j                  S z5Return activities ordered descending by performed_at.z-performed_atT)manyrQ   order_byr3   r\   rA   rB   rQ   s      r%   get_activitiesz#CallDetailSerializer.get_activitieso   )    ^^,,_=
%jt<AAAr$   N)r   r   r   r'   r   rJ   r/   r   r   rD   rO   rP   rQ   r   rT   r   r&   r^   re   rl   r#   r$   r%   rF   rF   A   ss    *')J$/G0{002H5K557M2224J)D9J
~"" 
 Br$   rF   c                        e Zd Z G d d      Zy)SalesCallDetailSerializerc                       e Zd ZeZg dZdgZy)SalesCallDetailSerializer.Meta)r   r   r   rM   rJ   r   Nr   r#   r$   r%   r&   rq   v   s    P 6r$   r&   Nr-   r#   r$   r%   ro   ro   u   s    " "r$   ro   c                       e Zd Z ej                         Z ej                         Z ej                         Z ej                         Z G d de	j                        Z
d Zd Zd Zd Zy)SalesCallSerializerc                   B    e Zd Zej                  j
                  g dz   Zy)SalesCallSerializer.Meta)offtimer,   rQ   rO   N)r   r   r   ro   r&   r!   r#   r$   r%   r&   ru      s    *//66 :
 
r$   r&   c                     t         j                  j                  |j                  |j                        j                         }|rt        |      j                  S y rV   rX   r]   s      r%   r^   z SalesCallSerializer.get_customer   r_   r$   c                     |j                   j                         r$|j                   j                         j                  S d S )N)appointment_setr[   r,   rA   rB   s     r%   get_namezSalesCallSerializer.get_name   s4    363F3F3L3L3Ns""((*//XTXXr$   c                     |j                   j                         r$|j                   j                         j                  S dS )N   )ry   r[   rv   rz   s     r%   get_offtimezSalesCallSerializer.get_offtime   s4    696I6I6O6O6Qs""((*22XWXXr$   c                 f    |j                   j                  d      }t        |d      j                  S rg   ri   rk   s      r%   rl   z"SalesCallSerializer.get_activities   rm   r$   N)r   r   r   r   rD   rv   r,   rO   rQ   ro   r&   r^   r{   r~   rl   r#   r$   r%   rs   rs   |   sl    /k//1G,;,,.D0{002H2224J
(-- 

YYBr$   rs   c                       e Zd Z ej                  ddd      Z ej                  d      Z ej                  d      Z ej                  d      Z
d Zy	)
TopRepeatedCallersSerializer
   r}   d   )default	min_value	max_valueTrequiredFc                 F    |d   |d   k  rt        j                  d      |S )Nend_date
start_datez!End date must be after start dater   ValidationError)rA   r\   s     r%   validatez%TopRepeatedCallersSerializer.validate   s+    
d<00--.QRRr$   N)r   r   r   r   IntegerFieldlimitDateTimeFieldr   r   	CharFieldr   r   r#   r$   r%   r   r      sS    $K$$R1LE***D9J({(($7H#k##U3Gr$   r   c                   ^    e Zd Z ej                  d      Z ej                  d      Zd Zy)DateRangeSerializerTr   c                 N    |d   }|d   }||kD  rt        j                  d      |S )Nr   r   zstart_date must be <= end_dater   )rA   r\   startends       r%   r   zDateRangeSerializer.validate   s4    \":3;--.NOOr$   N)r   r   r   r   r   r   r   r   r#   r$   r%   r   r      s+    ***D9J({(($7Hr$   r   c                   <    e Zd ZdZ ej
                  d      Zd Zy)ReadCallSerializerz&Serializer for marking a call as read.Tr   c                 :    |dk  rt        j                  d      |S )Nr   zID must be a positive integerr   )rA   values     r%   validate_idzReadCallSerializer.validate_id   s     A:--.MNNr$   N)r   r   r   r'   r   r   r   r   r#   r$   r%   r   r      s    0	!	!	!4	0Br$   r   ) r'   pytzdjango.utilsr   rest_frameworkr   django.db.modelsr   apps.core.fieldsr   apps.customers.modelsr   apps.companies.modelsr   apps.calls.modelsr	   r
   apps.companies.serializersr   r   ModelSerializerr   r)   r/   r3   rF   ro   rs   
Serializerr   r   r   r#   r$   r%   <module>r      s     ! &   3 * ) 0 Y"[00 "*44  33  [88 1B> 1Bh" ; ; "B3 B8	;#9#9 	+00 // r$   