B
    Ob=                 @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZm	Z	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 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)m*Z* d dl+m,Z,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? G dd dej@ZAG dd dejBZCG dd  d ejZDG d!d" d"e=ejEejFe&jGZHdS )#    N)CountCaseWhenValue)gettext)make_password)	TruncDate)MaxMin)Q)cache)settings)	FilterSet)mixins)serializers)action)Response)status)WorkflowRole)viewsets)Employee)PayloadBase)LogInSerializerSignOutSerializerResetPasswordSerializer)AppList)save_loggenerate_passworddatetime2stamp
user_photoWEEKDAY
get_hr_min)Company)send_one_mail)APPCustomizeResponseModelMixin)MyUserc               @   s   e Zd ZdS )NoneSerializerN)__name__
__module____qualname__ r*   r*   CG:\easytimepro\master/mysite/mobile\api_rest\views\view_employee.pyr&   '   s   r&   c               @   sF   e Zd Ze ZejdddZejdddZdd Z	G dd dZ
d	S )
EmployeeSerializerzdepartment.dept_nameT)sourceZ
allow_nullzposition.position_namec             C   s   |j s
dS dj|j jdS )N z/files/{path})path)photoformatname)selfobjr*   r*   r+   	get_photo0   s    zEmployeeSerializer.get_photoc               @   s   e Zd ZeZdZdS )zEmployeeSerializer.Meta)idemp_code
first_nameemailr0   department_nameposition_nameN)r'   r(   r)   r   modelfieldsr*   r*   r*   r+   Meta5   s   r>   N)r'   r(   r)   r   ZSerializerMethodFieldr0   Z	CharFieldr:   r;   r5   r>   r*   r*   r*   r+   r,   +   s
   r,   c               @   s0   e Zd ZejdddZG dd dZdd ZdS )	EmployeeContactFiltersearchsearch_method)labelmethodc               @   s   e Zd ZeZddgZdS )zEmployeeContactFilter.Metar7   r8   N)r'   r(   r)   r   r<   r=   r*   r*   r*   r+   r>   =   s   r>   c             C   s   | t|dt|dB S )N)Zemp_code__icontains)Zfirst_name__icontains)filterr   )r3   querysetr2   valuer*   r*   r+   rA   A   s    z#EmployeeContactFilter.search_methodN)r'   r(   r)   django_filtersZ
CharFilterZsearch_fieldr>   rA   r*   r*   r*   r+   r?   :   s   r?   c                   s   e Zd ZeZej Ze	Z
e	e	eeedZdd Z fddZedgddd	d
 Zedgdddd Zedgdddd Zedgdddd Z  ZS )EmployeeView)listexception_summarylog_insign_outreset_passwordc             C   s   | j | jtS )N)serializer_class_dictgetr   r,   )r3   r*   r*   r+   get_serializer_classR   s    z!EmployeeView.get_serializer_classc                s(   t t|  }| jjj}|j|d}|S )N)
company_id)superrH   get_querysetrequestuserrQ   rD   )r3   rE   rQ   )	__class__r*   r+   rS   V   s    
zEmployeeView.get_querysetrO   T)methodsZdetailc       
      O   s  |j d}tj|d}|j d}tj|d}dddddd}t||gsltd}td|gitjdS t	j
j|j||d	jtttdtd
dtttdtd
dtttdtd
dtttdtd
dtttdtd
dd}| r|x||D ]t}	|d  t|	j7  < |d  t|	j7  < |d  t|	j7  < |d  t|	j7  < |d  t|	j7  < qW t|tjdS )N
start_datez%Y-%m-%dend_dater   )
late_countearly_leave_countabsent_countleave_countovertime_countz8start_date(%Y%m%d) end end(%Y%m%d) end_date is required.message)r   )empZatt_date__gteZatt_date__lte   )Zlate__gtthen)Zearly_leave__gtrb   )Z
absent__gtrb   )Z	leave__gtrb   )Zovertime__total_ot__gtrb   rZ   r[   r\   r]   r^   )Zquery_paramsrO   datetimestrptimeall_r   r   HTTP_400_BAD_REQUESTr   objectsrD   rU   Zannotater   r   r   r   existsintrZ   r[   r\   r]   r^   HTTP_200_OK)
r3   rT   argskwargsrX   rY   Z
attendancemsgZobj_querysetr4   r*   r*   r+   rJ   \   s.    

zEmployeeView.exception_summaryZpostc          
   O   s  | j |jd}| r|j}|d}|d}|d}|j}	|jjsX|	j}
|	j}n|	j	}
|	j
}tjj|
|d}| r|d j}||krtd}td}t|d	|t|
|f|dd
 td|gitjd
S tjjddt|
dt|dB }|r|jdd tjj|
||d}|rr|d }|jstd}td}t|d	|t|
|f|dd
 td|gitjd
S nt }||_
|
|_||_tj }d|
t t t!t"" d }t#$|%d& }d|_'||_(||_)||_*||_+||_|,  t-.d| | t-.d| t/|	j0 dtd}dtd}|jjsh|d|	j0|
d|||d}t|tj1d
S |	j2}t3jj|	j0|d4d}t5jj|d j6d!dd"}|d#kr|	j78 }|rd}|||	j0|
||	j9|||d$	}t|tj1d
S t|j:tj;d
S )%N)datadevice_tokenclient_category	client_id)usernamecompanyr   zClient Id not matchz>Client Id not match, Please contact the administrator to resetZLogin)r   r_   T)active)rs   )rp   F)rs   rr   rQ   Z)app_client_already_block_by_administratorZapp_client_blockedz	{0}{1}{2}i  zutf-8Zmobile_company_id_Zmobile_employee_id_z{0}u4   Copyright ©2020 ZKTECO CO.,LTD.All rights reserved.   Z	auth_user)tokenroleemployee_idpin	role_name	copyrightcopyright_encopyright_ar)r6   rQ   	flow_role)Zid__inr{   )Zflat)ra   )	rw   rx   ry   rz   r{   Zout_door_statusr|   r}   r~   )<get_serializerro   is_validvalidated_datarO   rU   is_employeers   current_companyr7   rQ   r   rh   rD   ri   rp   rf   r   jsondumpsr   r   &HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONSr   updateenablerr   rc   nowr1   osgetpidrj   roundtimehashlibZmd5encodeZ	hexdigestru   Z
login_timeZlast_activerw   rq   saver   setstrr6   rk   app_roler   valuesr   Zvalues_listr   re   Zenable_outdoor_managementerrorsrg   )r3   rT   rl   rm   
serializerr   rp   r-   clientr`   r7   rQ   Zapp_listZexisted_device_tokendescriber_   Zactive_appsappsZappr   keyrw   Zzk_copyright_enZzk_copyright_arZresponser   Zflow_role_nameZemployee_roler   r*   r*   r+   rK   w   s    





"


zEmployeeView.log_inc       
      O   s   | j |jd}| r||j}|d}|jjr8|jj}n|jj}t	j
j||d}|r`|jdd td}	td|	gitjdS t|jtjdS d S )	N)ro   rp   )rp   rQ   F)ru   Zapp_sign_out_successfulr_   )r   )r   ro   r   r   rO   rU   r   rQ   r   r   rh   rD   r   rf   r   r   rk   r   rg   )
r3   rT   rl   rm   r   r   rp   rQ   r   r_   r*   r*   r+   rL      s    

zEmployeeView.sign_outc             O   s  | j |jd}| r|j}|d}|d}|d}|d}	|d}
yL| }|dkrvtjjtj	d}ntjj|d	}|j
jstjj||jd
}n8|ddkrtjj|j|d}n|}tjj|j|d}| rX| }|j
js|j}|j}n|j}|j}tjj||	d|d}|ri }td}|j}td}|j
jsr|| |  d|j|j|}n$t||_ |  d|j|j|}yt!|||f|d W n2 t"k
r } zdd|i}W d d }~X Y nX d|d< t#|d|	t$%||
|	fd t&|t'j(dS td}td}t#|d|	t$%||
|	f|dd t&d|git'j)dS nDtd}td }t#|d|	t$%||
|	f|dd t&d|git'j)dS W nv t"k
r } zVdd l*}|+  d|}td!}t#|d|	t$%||
|	f|dd t&d|git'j)dS d }~X Y nX d S )"N)ro   search_itemis_cloudcompany_namer   languageZfalse)r6   )Zname__iexact)rs   r   @)rQ   r9   )rQ   r7   T)rs   rr   r   rQ      Zbiotime_app_password_resetz
                                Dear {0} {1},
                                    Please find your new password <font color="blue">{2}</font>
                                )rQ   Z
email_sendz{0}Z
successfulresultrM   )r   Z0reset_password_must_to_do_on_the_mobile_with_usez4illegal_operation, please_contact_with_administratorr   r_   Z"app_password_reset_user_not_existsZ!app_password_reset_user_not_foundZsystem_exception),r   ro   r   r   rO   stripr"   rh   r   ZDEFAULT_COMPANY_IDrU   r   r%   rD   r6   findr   ri   firstr   rs   rQ   r7   r   r   r9   rf   Zset_passwordr   r1   Znicknamer   Zself_passwordr#   	Exceptionr   r   r   r   r   rk   r   	traceback	print_exc)r3   rT   rl   rm   r   r   r   r   r   r   r   rt   Zempsrz   r`   rQ   r   r   Znew_passwordr9   titlecontexter_   r   r*   r*   r+   rM      s    










 

zEmployeeView.reset_password)r'   r(   r)   r?   Zfilter_classr   rh   re   rE   r,   Zserializer_classr   r   r   rN   rP   rS   r   rJ   rK   rL   rM   __classcell__r*   r*   )rV   r+   rH   E   s   
]rH   )Ir   r   r   rc   r   rG   Zdjango.db.modelsr   r   r   r   Zdjango.utils.translationr   rf   Zdjango.contrib.auth.hashersr   Zdjango.db.models.functionsr   r	   r
   r   Zdjango.core.cacher   Zdjango.confr   Zdjango_filters.rest_frameworkr   Zrest_frameworkr   r   Zrest_framework.decoratorsr   Zrest_framework.responser   r   Zmysite.workflow.modelsr   Zmysite.mobile.api_restr   Zmysite.personnel.modelsr   Zmysite.att.modelsr   Z"mysite.mobile.api_rest.serializersr   r   r   Zmysite.mobile.modelsr   Zmysite.mobile.utilsr   r   r   r   r    r!   Zmysite.cloud.modelsr"   Zmysite.admin.services.emailr#   Z(mysite.mobile.api_rest.app_customize_mixr$   Zmysite.accounts.modelsr%   Z
Serializerr&   ZModelSerializerr,   r?   ZListModelMixinZRetrieveModelMixinZGenericViewSetrH   r*   r*   r*   r+   <module>   sF    