B
    b)               	   @   s`  d dl Z d dlmZ d dlmZ d dlmZ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 G dd dejZG dd dejZG dd de jjZG dd dejZ G dd deej!Z"G dd deej#ej!Z$G dd deej%ej&ej'ej!ej(Z)G dd dejZ*G dd deej+Z,dS )     N)gettext_lazy)Q)mixinsserializers)status)Response)viewsets)TrainingCategory)Training)models_choices)stamp2datetime)CACHE_TRAINING_TYPE)APPCustomizeResponseModelMixinc               @   s   e Zd ZG dd dZdS )TrainingListSerializerc               @   s   e Zd ZeZdZdS )zTrainingListSerializer.Meta__all__N)__name__
__module____qualname__r
   modelfields r   r   CG:\easytimepro\master/mysite/mobile\api_rest\views\view_training.pyMeta   s   r   N)r   r   r   r   r   r   r   r   r      s   r   c               @   s:   e Zd Ze Ze Ze ZejdddZdd Z	dS )TrainingCreateSerializerTF)
allow_nullrequiredc             C   sT   |d }|d }t |}t |}||kr@dtd}t|||d< ||d< |S )Nstartendz{0}z%end time can not less then start time
start_timeend_time)r   format_r   ValidationError)selfattrsr   r   r   r   messager   r   r   validate!   s    
z!TrainingCreateSerializer.validateN)
r   r   r   r   	CharFieldr   r   training_typeremarkr&   r   r   r   r   r      s
   r   c               @   sj   e Zd ZejdddZejdddZejdddZejdddZej	ddZ
ej	ddZG d	d
 d
ZdS )TrainingListFilterZstart_time__dateZgte)
field_nameZlookup_exprZlteZend_time__dateZemployee__emp_code)r+   Zemployee__first_namec            	   @   s&   e Zd ZeZddddddddd	g	Zd
S )zTrainingListFilter.Metaemployeecategorystart_time_gtestart_time_lteend_time_gteend_time_lteaudit_statusemp_code
first_nameN)r   r   r   r
   r   r   r   r   r   r   r   6   s   r   N)r   r   r   django_filtersZ
DateFilterr.   r/   r0   r1   Z
CharFilterr3   r4   r   r   r   r   r   r*   .   s   r*   c               @   s*   e Zd Ze ZejdddZdd ZdS )TrainingApproveSerializerFT)r   r   c             C   s.   |d }|t jt jfkr*td}t||S )Napprove_statusZ!param_approve_status_out_of_range)r   ZAUDIT_SUCCESSREFUSEr!   r   r"   )r#   r$   r7   r%   r   r   r   r&   @   s
    
z"TrainingApproveSerializer.validateN)	r   r   r   r   ZIntegerFieldr7   r'   r)   r&   r   r   r   r   r6   <   s   r6   c               @   s   e Zd Zdd ZdS )TrainingCategoryViewc             C   s    |j j}t|}t|tjdS )N)r   )user
company_idr   getr   r   ZHTTP_200_OK)r#   requestr;   choicesr   r   r   listI   s    
zTrainingCategoryView.listN)r   r   r   r?   r   r   r   r   r9   H   s   r9   c               @   s&   e Zd ZeZej Ze	Z
dd ZdS )TrainingViewc             C   s   | j jj}tjj|d}|S )N)company)r=   r:   r;   r
   objectsfilter)r#   r;   querysetr   r   r   get_querysetV   s    
zTrainingView.get_querysetN)r   r   r   r   serializer_classr
   rB   allrD   r*   filter_classrE   r   r   r   r   r@   O   s   
r@   c               @   sR   e Zd ZeZeeeedZe	Z
dd Zdd Zdd Zdd	 Zd
d Zdd ZdS )EmployeeTrainingView)Zretriever?   createZapprovec             C   s   | j jj}tjj|d}|S )N)rA   )r=   r:   r;   r
   rB   rC   )r#   r;   rD   r   r   r   rE   k   s    
z!EmployeeTrainingView.get_querysetc             C   s   | j | jd S )N)serializer_class_dictr<   action)r#   r   r   r   get_serializer_classp   s    z)EmployeeTrainingView.get_serializer_classc       	      O   s~   | d|jj}| jjj}tjj||dd}| 	|}| 
|}|d k	rf| j|dd}| |jS | j|dd}t|jS )NZemployee_pk)employee_idr;   z-apply_timeT)Zmany)r<   r:   pkr=   r;   r
   rB   rC   Zorder_byZfilter_querysetZpaginate_querysetget_serializerZget_paginated_responsedatar   )	r#   r=   argskwargsZempr;   rD   Zpage
serializerr   r   r   r?   s   s    


zEmployeeTrainingView.listc             O   s^   | j |jd}|jdd | ||j}t|tr6|S | | | |j}t|jt	j
|dS )N)rQ   T)Zraise_exception)r   headers)rP   rQ   Zis_validtraining_apply_checkZvalidated_data
isinstancer   perform_createZget_success_headersr   ZHTTP_201_CREATED)r#   r=   rR   rS   rT   datasrU   r   r   r   rJ      s    

zEmployeeTrainingView.createc             C   s   t f |  d S )N)r
   Zsave)r#   rY   r   r   r   rX      s    z#EmployeeTrainingView.perform_createc             C   s   i }|d }|d }|d }|j }tjj|d}|sVdtd}	td|	gitjdS t	j
t	jg}
tjj|jd	t|d
t|dB j|
d}|rdtd}	td|	gitjdS |j|d< ||d< |d|d< d|d< |d |d< ||d< ||d< |S )Nr   r   r(   )rO   z{0}zPlease try againr%   )r   )rN   )Zstart_time__gt)Zend_time__lt)Zaudit_status__inZtraining_time_overlaprN   r-   r)   Zapply_reason   r2   r   )r:   r	   rB   rC   r    r!   r   r   ZHTTP_406_NOT_ACCEPTABLEr   r8   ZCANCEL_AUDIT_SUCCESSr
   idZexcluder   r<   )r#   r=   rY   Z	obj_datasr   r   r(   ZapplierZtraining_categoryZdescribeZselect_statusZtmp_trainingr   r   r   rV      s.    
z)EmployeeTrainingView.training_apply_checkN)r   r   r   r*   rH   r   r   r6   rK   r
   r   rE   rM   r?   rJ   rX   rV   r   r   r   r   rI   \   s   
rI   c               @   s   e Zd ZejedddZejddZe Z	ejedddd	Z
e Ze Zejed
ddd	Zdd ZG dd dZdS )EmployeeTeamTrainingSerializerZemp_field_employeeCodezemployee.emp_code)labelsourcezemployee.first_name)r^   Zstaff_training_trainingCategoryzcategory.category_nameT)r]   r^   r   Zapproval_field_approvalStatusZget_audit_status_displayc             C   s   |j jsdS dj|j jjdS )N z/files/{path})path)r,   photor    name)r#   objr   r   r   	get_photo   s    z(EmployeeTeamTrainingSerializer.get_photoc               @   s   e Zd ZeZdZdS )z#EmployeeTeamTrainingSerializer.Metar   N)r   r   r   r
   r   r   r   r   r   r   r      s   r   N)r   r   r   r   r'   r!   r3   rb   ZSerializerMethodFieldra   Zcategory_nameZReadOnlyFieldZlast_approverZworkflow_current_state_nameZaudit_status_stringrd   r   r   r   r   r   r\      s   

r\   c               @   s   e Zd ZeZeZeZdS )EmployeeTeamTrainingViewsN)	r   r   r   r\   rF   r*   rH   r
   r   r   r   r   r   re      s   re   )-r5   Zdjango.utils.translationr   r!   Zdjango.db.modelsr   Zrest_frameworkr   r   r   Zrest_framework.responser   Zmysite.mobile.api_restr   Z(mysite.att.models.model_trainingcategoryr	   Zmysite.att.modelsr
   Z
mysite.attr   Zmysite.mobile.utilsr   Zmysite.att.global_cacher   Z(mysite.mobile.api_rest.app_customize_mixr   ZModelSerializerr   Z
Serializerr   Z	FilterSetr*   r6   ZGenericViewSetr9   ZNotificationMixinr@   ZListModelMixinZCreateModelMixinZRetrieveModelMixinZApproveFlowMixinrI   r\   ZGenericApprovalListViewre   r   r   r   r   <module>   s8   E