B
    gc=                 @   s   d dl 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mZmZ d d
lmZ d dlmZmZmZ G dd dejZG dd de
jZG dd deZG dd deZ e!ee dS )    N)cache)ModelChoiceField)gettext_lazy)configadmin)ZKModelAdmin)zk_site)forms)actionsdb_constwidgets)
ImportData)EmployeeCertificationEmployeeCertificationc               @   st   e Zd ZejedddZejedejj	dZ
ejeddejdZejed	d
dZeej edddZdS )AddEmployeeCertFormemployeeCert_field_employeeF)labelrequiredemployeeCert_field_expireOn)r   initialemployeeCert_field_emailAlertT)r   r   choicesemployeeCert_field_beforer   employeeCert_field_cert)Zquerysetr   r   N)__name__
__module____qualname__r	   ZEmployeeOneToOneField_employeeZ	DateFielddatetimenow	expire_onZChoiceFieldr   ZBOOLEANSemail_alertZIntegerFieldbeforer   r   objectsallcertification r(   r(   KG:\easytimepro\master/mysite/personnel\admin\employeecertification_admin.pyr      s   r   c               @   s0   e Zd ZedZedZedZeZdd Z	dS )AddEmployeeCertZempCert_action_addEmployeeCertZ%empCert_action_addEmployeeCertHelpTxtZ)empCert_action_addEmployeeCertDescriptionc             O   s   | j jd}|r| j jdtj }| j jdd}| j jd}| j jdd}xr|D ]P}tjj||drdd	l	m
}	 |	td
||d t|||||d}
|
  q\W ndd	l	m
}	 |	tdd S )Nr   r"   r#      r'   r$   r   )employee_idcertification_id)ActionHandleErrorz>the_employee_%(emp)s_is_already_has_document_%(certification)s)empr'   )r,   r"   r#   r$   r-   Zselect_none_employee)requestZPOSTZgetlistgetr    r!   r   r%   filtermysite.admin.actionr.   r   save)selfargskwargsZempsr"   r#   r'   r$   r/   r.   objr(   r(   r)   action$   s"    

zAddEmployeeCert.actionN)
r   r   r   r   Zverbose_nameZhelp_txtZshort_descriptionr   Zaction_formr9   r(   r(   r(   r)   r*      s
   r*   c                   sD   e Zd ZdZejd Zeej	gZdd Z
 fddZdd Z  ZS )	EmployeeCertificationAdmin)idr'   r   r"   r#   r$   )r'   r"   r#   c             C   s   dS )NFr(   )r5   r0   r(   r(   r)   has_add_permission>   s    z-EmployeeCertificationAdmin.has_add_permissionc       	         s   |j jr&tt| |j|j jd}nTddlm} |j j	}|j j
}|j j}tt| ||| | | d }yLddlm} |jj|jd d}|r|r|j|d jd}n|jdd}|S    |S d S )	N)Zemployee__company_idr   )Q)Zemployee__department__inZemployee__area__inZemployee__position__in)r   emp_code)r>   )r,   )userZis_superusersuperr:   get_querysetr2   current_companyZdjango.db.modelsr=   Zget_auth_deptZget_auth_areaZget_auth_posr&   ZdistinctZ&mysite.personnel.models.model_employeer   r%   ZGETr;   )	r5   r0   Zqsr=   Z	auth_deptZ	auth_areaZauth_posr   Zemployee_obj)	__class__r(   r)   rA   A   s(    z'EmployeeCertificationAdmin.get_querysetc             C   sH   t |d| jj| jjd}|  |j}|rDddlm} |d|d S )Nimport_data)req
input_name	app_label
model_namer   )r.   ;)	ImportEmployeecertificationDataZoptsrG   rH   Zexe_import_data
error_infor3   r.   join)r5   r0   Z
obj_importZ	ret_errorr.   r(   r(   r)   
dataimportZ   s    z%EmployeeCertificationAdmin.dataimport)r   r   r   Zlist_displayr   ZEMPLOYEE_LIST_FILTERZlist_filterr*   r
   ZImportr<   rA   rM   __classcell__r(   r(   )rC   r)   r:   9   s   

r:   c                   sn   e Zd Zd fdd	Zdd Zdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Z  ZS )rJ   rD   Nc                s   ddl m} |r|| tt| |||| d | _td| j tj	 d dt
d dt
d g| _dt
d dt
d dt
d dt
d	 dt
d
 g| _d | _d | _d | _d | _d | _d S )Nr   )activatezemployeecertification_import_%s   z%sr   r   r   r   r   )django.utils.translationrO   r@   rJ   __init__stampr   setr    r!   r   Zcalculate_fields_verboseZmust_fieldsemployee_codecertification_coder"   r#   r$   )r5   rE   rF   rG   rH   ZlngrO   )rC   r(   r)   rR   g   s"    



z(ImportEmployeecertificationData.__init__c          	   C   s  dd l }dd l}t| j}d| jp&d}dd|dddd}t|||d xt	t| j
D ]|}| j
| }|d	td
 kr|| _|d	td kr|| _|d	td kr|| _|d	td kr|| _|d	td krd|| _qdW xt| jD ]\}}|d7 }d }	 }
 } }}| jd k	r.|| j }	| jd k	rD|| j }
| jd k	rZ|| j }| jd k	rp|| j }| jd k	r|| j }|	s| jtdd|i  n0tjj|	| jjjds| jtdd|i  |
s| jtdd|i  n0tjj|
| jjjds&| jtdd|i  |sF| jtdd|i  ndy>|j|d}| |j k r| jtdd|i  W n$   | jtdd|i  Y nX |tdtdfkr| jtd|tdtdd  | dkr`|s| jtdd|i  nLy*t|dk r:| jtd d|i  W n    | jt td! Y nX n(| d"kr|r|d#krd|| j< ||d$< t!|t!| |d%< t|||d qW d&S )'Nr   z*employeecertification_import_processor_{0} z{0}zValidating...r+   )statustotalfinishtpfpi,  z%sr   r   r   r   r   z7error_data_on_row(%(index)s)_the_employee_code_is_emptyindex)r>   
company_idz>error_data_on_row(%(index)s)_the_employee_code_does_not_existsz<error_data_on_row(%(index)s)_the_certification_code_is_empty)	cert_coder^   zCerror_data_on_row(%(index)s)_the_certification_code_does_not_existsz6error_data_on_row(%(index)s)_expire_date_cant_be_emptyz%Y-%m-%dzEerror_data_on_row(%(index)s)_expire_date_should_be_greater_than_todayz3error_data_on_row(%(index)s)_the_expire_on_is_wrongZboolean_option_noZboolean_option_yeszYerror_data_on_row(%(index)s)_the_email_alert_is_wrong_the_format_should_be_%(no)s/%(yes)s)r]   noyesra   zLerror_data_on_row(%(index)s)_alert_before_cant_be_empty_when_email_alert_yeszEerror_data_on_row(%(index)s)_alert_before_should_be_greater_than_zeroZ,employee_certificate_alert_before_validationr`   0rZ   r\   T)"jsonr    lenrecordsformatrS   r   rT   dumpsrangeheadr   rU   rV   r"   r#   r$   	enumeraterK   appendr   r%   r2   r0   r?   rB   r   strptimedateZtodaylowerintstrfloat)r5   rc   r    _lenprocessor_keyprocessor_datar]   eelemrU   rV   r"   r#   r$   r(   r(   r)   before_insert   s    








z-ImportEmployeecertificationData.before_insertc                s  ddl m} dd l}t| j}d| jp*d}dd|dddd}t||	|d	 | j
}t| j}i }x"t| jD ]\}	}
i }xt|D ]}x*| j D ]\}}||kr|
| ||< qW || jkr|
|  | j|}| j| }|jr d
krq fdd|jD }|r|d  t||js|j}| | }|||< qW | |||	 |	d |d< t|	d t| |d< t||	|d	 qzW d S )Nr   )modelsz*employeecertification_import_processor_{0}rW   z{0}zProcessing...r+   )rX   rY   rZ   r[   r\   i,  )rW   NNonec                s    g | ]}|d   kr|d qS )r+   r   r(   ).0ru   )	tmp_valuer(   r)   
<listcomp>   s    z?ImportEmployeecertificationData.data_insert.<locals>.<listcomp>rZ   r\   )Z	django.dbrx   rc   rd   re   rf   rS   r   rT   rg   need_update_old_recordri   rj   rh   Zcalculate_fields_indexitemsZvalid_head_indexsr]   Zvalid_model_fieldsr   
isinstanceZ
ForeignKeyZattnameZget_db_valueprocess_rowrq   )r5   rx   rc   rr   rs   rt   Z	overwriteZhead_lencalculate_dictr]   r8   Zobj_dataikvZf_indexZ	tmp_fieldZtvkeyvaluer(   )r{   r)   data_insert   sJ    



z+ImportEmployeecertificationData.data_insertc             C   s,   |j j|| jjjd}|sdS |d jS d S )N)r>   r^   Fr   )r%   r2   r0   r?   rB   r;   )r5   class_modelcodenameclass_model_Objr(   r(   r)   	emp_valid   s    z)ImportEmployeecertificationData.emp_validc             C   s,   |j j|| jjjd}|sdS |d jS d S )N)r_   r^   Fr   )r%   r2   r0   r?   rB   r;   )r5   r   r   r   r   r(   r(   r)   	cer_valid  s    z)ImportEmployeecertificationData.cer_validc          
   C   sT  ddl m}m}m} | ||td d |d< | ||td d |d< y|d r|d r|jj|d |d d}|r| j	s| j
tdd	|i  d
S |r|d }x,| D ]\}	}
t||	|
 qW n
|f |}|  n<|d s | j
tdd	|i  n| j
tdd	|i  W n4 tk
rN } zdd l}|  W d d }~X Y nX d S )Nr   )r   r   r   r   r,   r   r-   )r,   r-   z-Error data on row(%(index)s) duplicate recordr]   Fz>error_data_on_row(%(index)s)_the_employee_code_does_not_existszCerror_data_on_row(%(index)s)_the_certification_code_does_not_exists)mysite.personnel.modelsr   r   r   r   r   r   r%   r2   r}   rK   rk   r~   setattrr4   	Exception	traceback	print_exc)r5   Zrow_datar   r]   r   r   r   Zobjsr8   r   r   ru   r   r(   r(   r)   r     s2    



z+ImportEmployeecertificationData.process_rowc             C   s   |    dS )u(   
        sqlserver 数据插入
        N)r   )r5   r(   r(   r)   sqlserver_insert-  s    z0ImportEmployeecertificationData.sqlserver_insertc             C   s   |    dS )u$   
        mysql 数据插入
        N)r   )r5   r(   r(   r)   mysql_insert3  s    z,ImportEmployeecertificationData.mysql_insertc             C   s   |    dS )u%   
        oracle 数据插入
        N)r   )r5   r(   r(   r)   oracle_insert9  s    z-ImportEmployeecertificationData.oracle_insertc             C   s   |    dS )u)   
        postgresql 数据插入
        N)r   )r5   r(   r(   r)   postgresql_insert?  s    z1ImportEmployeecertificationData.postgresql_insertc             C   s   t d| j  d S )Nzemp_import_%s)r   deleterS   )r5   r(   r(   r)   after_insertE  s    z,ImportEmployeecertificationData.after_insert)rD   NNN)r   r   r   rR   rw   r   r   r   r   r   r   r   r   r   rN   r(   r(   )rC   r)   rJ   e   s   U,rJ   )"r    Zdjango.core.cacher   Zdjango.formsr   rQ   r   r   mysiter   r   Zmysite.admin.kernelr   Zmysite.admin.sitesr   Zmysite.adminr	   Zmysite.personnelr
   r   r   Zmysite.personnel.import_datar   r   r   r   r   ZZKActionFormr   ZZKModelActionr*   r:   rJ   registerr(   r(   r(   r)   <module>   s    	, e