o
    miz                     @   s   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mZ d dlmZ d dlmZ d d	lmZ G d
d deZG dd dejZG dd deZG dd deZG dd deZG dd dejZG dd deZG dd deZdS )    )ModelSerializer)serializers)make_password)Q)Decimal)timedelta_to_custom_formatduration_string_to_timedeltaformat_amount_with_currency)User)Activity)Projectc                       s6   e Zd ZejddZG dd dZ fddZ  ZS )EmployeeCreateAPISerializerT)
write_onlyc                   @   s>   e Zd ZeZdZddiddiddiddiddiddidZdS )z EmployeeCreateAPISerializer.Meta	id
first_name	last_namephone_noemailpasswordpay_per_hourposition	is_activerequiredT)r   r   r   r   r   r   N__name__
__module____qualname__r
   modelfieldsextra_kwargs r!   r!   T/var/www/html/kuke-dev/django-kuke/apps/employee/serializers/employee_serializers.pyMeta   s    
r#   c                    sz   | d}| d}tjt|dt|dB }| r/| }|j|kr*t	dt	dt
|d |d< t |S )Nr   r   r   r   &A user with this email already exists.-A user with this phone number already exists.r   )getr
   objectsfilterr   existsfirstr   r   ValidationErrorr   supervalidate)selfattrsr   r   usersZexisting_user	__class__r!   r"   r/      s   




z$EmployeeCreateAPISerializer.validate)	r   r   r   r   	CharFieldr   r#   r/   __classcell__r!   r!   r3   r"   r      s    r   c                   @   s$   e Zd Zeje dZdd ZdS )EmployeeAssignProjectSerializer)childc                 C   s>   |st d|D ]}t|tr|dkrt d|q	|S )Nz)At least one project ID must be provided.r   zInvalid project ID: {})r   r-   
isinstanceintformat)r0   value
project_idr!   r!   r"   validate_project_id4   s   
z3EmployeeAssignProjectSerializer.validate_project_idN)r   r   r   r   	ListFieldIntegerFieldr=   r>   r!   r!   r!   r"   r7   1   s    r7   c                   @   s4   e Zd ZejdddddZG dd dZdd ZdS )	EmployeeUpdateAPISerializerFT)r   
allow_nullr   allow_blankc                   @   s8   e Zd ZeZdZddiddiddiddiddidZdS )z EmployeeUpdateAPISerializer.Metar   r   T)r   r   r   r   r   Nr   r!   r!   r!   r"   r#   B   s    
r#   c                 C   s   d|v r| d}|d u s|dkr|d nt|d |d< | d}| d}| j}tjt|dt|dB }|rC|j|j	d}|
 ra|j|d
 rTtd|j|d
 ratd	|S )
Nr    r   r   r$   r%   )r   r&   r'   )r(   popr   instancer
   r)   r*   r   excluder   r+   r   r-   )r0   r1   r   r   r   rF   r2   r!   r!   r"   r/   M   s"   




z$EmployeeUpdateAPISerializer.validateN)r   r   r   r   r5   r   r#   r/   r!   r!   r!   r"   rA   ?   s    rA   c                       sR   e Zd ZejddZejddZe Z	G dd dZ
dd Z fddZ  ZS )	EmployeeListAPISerializerT)rB   c                   @      e Zd ZeZdZeZdS )zEmployeeListAPISerializer.Meta)r   	full_namer   r   profile_picr   r   total_working_durationtotal_amount
deleted_atr   Nr   r   r   r
   r   r   read_only_fieldsr!   r!   r!   r"   r#   m       r#   c                 C   sL   | j d jdd }|j }|r|j|d}tdd |D }t|}|S )Nrequestr=   )r=   c                 s   s4    | ]}|j r|jr|j  d  |j ndV  qdS )i  r   N)total_durationr   total_seconds).0activityr!   r!   r"   	<genexpr>w   s   2 z=EmployeeListAPISerializer.get_total_amount.<locals>.<genexpr>)contextquery_paramsr(   activity_setallr*   sumr	   )r0   rF   r=   
activitiesrM   Zformatted_total_amountr!   r!   r"   get_total_amountr   s   
z*EmployeeListAPISerializer.get_total_amountc                    s$   t  |}tt|d |d< |S )NrL   )r.   to_representationr   r   )r0   rF   resr3   r!   r"   r_   |   s   z+EmployeeListAPISerializer.to_representation)r   r   r   r   DurationFieldrL   r5   rJ   SerializerMethodFieldrM   r#   r^   r_   r6   r!   r!   r3   r"   rH   h   s    
rH   c                   @   s   e Zd ZG dd dZdS )EmployeeDeleteAPISerializerc                   @      e Zd ZeZdZdS )z EmployeeDeleteAPISerializer.Meta)r   r   r   N)r   r   r   r
   r   r   r!   r!   r!   r"   r#          r#   N)r   r   r   r#   r!   r!   r!   r"   rc      s    rc   c                   @   s&   e Zd ZejddZG dd dZdS )ProjectEmployeeDetailSerializerzclient.namesourcec                   @   rd   )z$ProjectEmployeeDetailSerializer.Meta)r   nameclientclient_nameN)r   r   r   r   r   r   r!   r!   r!   r"   r#      re   r#   N)r   r   r   r   ReadOnlyFieldrk   r#   r!   r!   r!   r"   rf          rf   c                   @   s&   e Zd ZedddZG dd dZdS )EmployeeDetailAPISerializerT)many	read_onlyc                   @   rI   )z EmployeeDetailAPISerializer.Meta)r   r   r   r   r   rK   r   projectsr   r   rN   NrO   r!   r!   r!   r"   r#      rQ   r#   N)r   r   r   rf   rq   r#   r!   r!   r!   r"   rn      rm   rn   c                   @   s2   e Zd ZejddZejddZG dd dZdS )CurrentActivitySerializerzproject.namerg   zactivity_type.namec                   @   rd   )zCurrentActivitySerializer.Meta)	r   r=   project_nameactivity_type_idactivity_typestart_date_timeend_date_timerS   descriptionN)r   r   r   r   r   r   r!   r!   r!   r"   r#      re   r#   N)r   r   r   r   r5   rs   ru   r#   r!   r!   r!   r"   rr      s    rr   N)rest_framework.serializersr   rest_frameworkr   django.contrib.auth.hashersr   django.db.modelsr   decimalr   core.utils.helperr   r   r	   apps.user.modelsr
   apps.employee.modelsr   apps.project.modelsr   r   
Serializerr7   rA   rH   rc   rf   rn   rr   r!   r!   r!   r"   <module>   s"     )	