B
    b2B                 @   s   d dl Z d dl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mZ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ZG dd deZG dd deZ G dd deZ!e"ee  dS )    N)	ModelFormModelChoiceField)gettext_lazy)cache)forms)ActionHandleError)ZKModelAdmin)zk_site)db_constactionswidgets)
ImportData)
Department)AdminRuntimeWarning)settingsc                   sn   e Zd ZejedejdZejedej	dZ
eej eddejdZ fddZG d	d
 d
Z  ZS )DepartmentCreationFormdepartment_field_code)label
max_lengthdepartment_filed_name!department_field_parentDepartmentF)querysetr   requiredwidgetc                s   t t| j|| ddlm} | }tjj|j	d
d }|ry8t|j}tjj|d |j	d sz|d | jd _W n tk
r   Y nX d S )Nr   )threadlocals)
company_idid   )	dept_coder   r   )superr   __init__Zmysite.baser   Zget_current_userr   objectsfiltercurrent_companyZorder_byZlastintr   existsfieldsinitial	Exception)selfargskwargsr   userobjcode)	__class__ @G:\easytimepro\master/mysite/personnel\admin\department_admin.pyr       s    
zDepartmentCreationForm.__init__c               @   s   e Zd ZeZdZdS )zDepartmentCreationForm.Meta)r   	dept_nameparent_deptN)__name__
__module____qualname__r   modelsr&   r0   r0   r0   r1   Meta,   s   r8   )r4   r5   r6   r   	CharField_r
   MAX_DEPARTMENT_CODEr   MAX_DEPARTMENT_NAMEr2   r   r   r!   allr   DepartmentRadioSelectr3   r    r8   __classcell__r0   r0   )r/   r1   r      s   r   c                   sp   e Zd ZejedejddZejedej	dZ
eej eddejdZG d	d
 d
Z fddZ  ZS )DepartmentChangeFormr   T)r   r   r   r   )r   r   r   F)r   r   r   r   c               @   s   e Zd ZeZdZdS )zDepartmentChangeForm.Meta)r   r2   r3   N)r4   r5   r6   r   r7   r&   r0   r0   r0   r1   r8   9   s   r8   c                s>   t t| j|| t| dd }|r:|jr:d| jd jjd< d S )NinstanceTr   readonly)r   r@   r    getattrpkr&   r   Zattrs)r)   r*   r+   rA   )r/   r0   r1   r    =   s    
zDepartmentChangeForm.__init__)r4   r5   r6   r   r9   r:   r
   r;   r   r<   r2   r   r   r!   r=   r   r>   r3   r8   r    r?   r0   r0   )r/   r1   r@   1   s   r@   c                   s|   e Zd ZdZddiddiddiddidZdZdZeZe	Z
ejejgZdZd	Zd fdd	Zdd Zdd Zdd Z  ZS )DepartmentAdmin)r   r   r2   r3   Zemployee_countZresigned_countwidth      )r   r2   r3   Zget_employees)r   r2   )Zdepartment_tree_nodeszdepartment-parent_dept-treezdepartment-parent_dept-map)r   r2   r3   )r3   Nc                s6   i }|dkr| j |d< || tt| j||f|S )z7
        Use special form during user creation
        Nform)add_formupdater   rE   get_form)r)   requestr-   r+   defaults)r/   r0   r1   rL   \   s
    

zDepartmentAdmin.get_formc             C   s<   t |d| jj| jjd}|  |j}|r8td|d S )Nimport_data)req
input_name	app_label
model_name;)ImportDeptDataZoptsrR   rS   Zexe_import_data
error_infor   join)r)   rM   Z
obj_importZ	ret_errorr0   r0   r1   
dataimportf   s    zDepartmentAdmin.dataimportc             C   s>   |j j}|j js| s,tjj|j jd}n| }|	dS )N)r   r3   )
r,   Zget_auth_deptZis_superuserr%   r   r!   r"   r#   r=   Zselect_related)r)   rM   Z	auth_deptZqsr0   r0   r1   get_querysetn   s
    zDepartmentAdmin.get_querysetc             C   s\   ddl m} |jd}|jd}tjj||jjdj	|jj
d}| rX|tdd S )Nr   )r   r   r   )r   r   )r   z,Department with the same code already exist.)mysite.admin.exceptionsr   Zcleaned_datagetr   r!   r"   rA   r   Zexcluder   r%   r:   )r)   Z
model_formr   r   r   Zbkr0   r0   r1   check_domain_rulesw   s     z"DepartmentAdmin.check_domain_rules)N)r4   r5   r6   Zlist_displayZlist_display_paramsZsort_fields
cache_keysr   rJ   r@   rI   r   ZImportZSetDepartmentZlist_filterZlist_select_relatedrL   rX   rY   r\   r?   r0   r0   )r/   r1   rE   D   s    
	rE   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 )rU   rO   Nc       	         s   ddl m} |j}tj}|d|}|r0|| tt| |||| d | _	|jj
| _td| j	| jf tj d dtd g| _dtd dtd	 dtd g| _g | _d | _d | _d | _d S )
Nr   )activateZlanguagezdept_import_%s_%s   z%sr   r   r   )django.utils.translationr^   r,   r   ZLANGUAGE_CODEZget_preferencesr   rU   r    stampr#   r   r   setdatetimenowr:   Zcalculate_fields_verboseZmust_fieldsexist_codesdCodedNamedpCode)	r)   rP   rQ   rR   rS   r^   ZprofileZdefault_language_codeZlng)r/   r0   r1   r       s$    
 

zImportDeptData.__init__c       	      C   s   |g}x|r|  }|d| xx|D ]p}y|| j }W n   d}Y nX |rV| pXd}|| j }|rp| prd}|r&||kr&||krdS || q&W qW dS )uK   判断是否设置了一个部门的上级部门为其自身或其子部门r    FT)popinsertrh   striprf   append)	r)   elemrecordsstoreZ
check_listZ
check_itemeZd_pcoded_coder0   r0   r1   is_valid_record   s"    


zImportDeptData.is_valid_recordc          	      s  dd l }dd l}t j}d jp&d j}dd|dddd}t||	|d t
d	t
d
t
dgdd  jD }tfdd|D }|stt
dxbtt jD ]P} j| }	|	dt
d kr| _|	dt
d kr| _|	dt
d kr| _qW t j j j}
| j}tjj jdddd} fdd|D }dd |D  _xxt jD ]j\}}||krn|| }dg|
d  }|d | j< |d pd| j< |d pd| j< || qnW i }xZt jD ]J\}}|d7 }| j }y| j }W n tk
r2   d}Y nX y| j }W n tk
r\   d}Y nX |s~ jt
dd|i  n,t|tjkr jt
d|tjd  |r||krtt
d tjj| jd!} js|r|rtt
d"|s jt
d#d|i  n,t|tj krD jt
d$|tj d  |rvt|tjkrv jt
d%|tjd  g } !|||s jt
d&||d'  d(S xFt|D ]:\}}d| }|"|g }||kr|| |||< qW ||d)< t#|t#| |d*< d+$ jd d, |d-< t||	|d qW |% }t&j'rpdd.l(m)} t*||d/d0 d1}n|+d2d0 }| _d3S )4Nr   zdept_import_processor_{0}_{1}ri   z{0}zValidating...r   )statustotalfinishtpfpi,  Zdepartment_header_codeZdepartment_header_nameZdepartment_header_parentc             S   s   g | ]}|  qS r0   )lower).0itemr0   r0   r1   
<listcomp>   s    z0ImportDeptData.before_insert.<locals>.<listcomp>c             3   s   | ]}| kV  qd S )Nr0   )rz   rn   )valid_headersr0   r1   	<genexpr>   s    z/ImportDeptData.before_insert.<locals>.<genexpr>z9Invalid import data,Please check the headers or file dataz%sr   r   r   )r   r   r2   Zparent_dept__dept_codec                s   g | ]}| j  qS r0   )rf   )rz   rq   )r)   r0   r1   r|      s    c             S   s   g | ]}|d  qS )r   r0   )rz   rq   r0   r0   r1   r|      s       z5error_data_on_row(%(index)s)_department_code_is_emptyindexzPerror_data_on_row(%(index)s)_the_length_of_department_code_maximum_is_%(length)s)r   lengthz(Self cant be tagged as parent department)r   r   Z,department_with_the_same_code_already_existsz@error_data_on_row(%(index)s_the_department_name_can_not_be_emptyzPerror_data_on_row(%(index)s)_the_length_of_department_name_maximum_is_%(length)szWerror_data_on_row(%(index)s)_the_length_of_parent_department_code_maximum_is_%(length)sznthe_%(index)s_rows_of_data_can_not_be_set_higher_authorities_to_%(index_name)s_sector_or_sub-sector_of_its_own)r   Z
index_nameFrv   rx   rT      rV   )
cmp_to_keyc             S   s   t | d t |d  S )Nr   )r$   )x1x2r0   r0   r1   <lambda>      z.ImportDeptData.before_insert.<locals>.<lambda>)keyc             S   s   t | d t |d  S )Nr   )r$   )r   r   r0   r0   r1   r     r   T),jsoncopylenro   formatra   r   r   rb   dumpsr:   headr=   r   rangerf   rg   rh   maxZdeepcopyr   r!   r"   Zvalues_listre   	enumeraterm   
IndexErrorrV   r
   r;   need_update_old_recordr<   rs   r[   floatrW   itemssixZPY3	functoolsr   sortedsort)r)   r   r   _lenprocessor_keyprocessor_dataZheadersresultr   rq   Zmax_lenZall_recordsZ
exist_dataZ	new_codesicZdb_datadataZd_levelrn   rr   Zd_nameZdp_codeZ
check_deptrp   vr   Zdeptsr   r   Z
list_itemsr0   )r)   r}   r1   before_insert   s    









zImportDeptData.before_insertc             C   s   d S )Nr0   )r)   Zinsert_deptr0   r0   r1   records_analysis#  s    zImportDeptData.records_analysisc          
      s  dd l }t| j}d| jpd| j}dd|dddd}t|||d t| j	}i }| j
}d}x@| jD ]4\}	}
x|
D ]}t|d	kr|d td
| j| jf d }tj }|r||k rt||  dkrP nP |d7 }i }|| j }|| jks|rRxt|D ]}x.| j D ] \}}||kr*|| ||< q*W || jkr||  | j|}| j| }|jr fdd|jD }|r|d  |j}| | }|||< qW | ||}y\tjj|| jd}|r|d }x.| D ]\}}t ||| qW n
tf |}|!  W n& t"k
rP   dd l#}|$  Y nX ||d< t%|t%| |d< t|||d qW x t&t'dg D ]}t(| qW qrW d S )Nr   zdept_import_processor_{0}_{1}ri   z{0}zProcessing...r   )rt   ru   rv   rw   rx   i,  r   zdept_import_%s_%s
   c                s    g | ]}|d   kr|d qS )r   r   r0   )rz   rq   )	tmp_valuer0   r1   r|   P  s    z.ImportDeptData.data_insert.<locals>.<listcomp>)r   r   rv   rx   r]   ))r   r   ro   r   ra   r   r   rb   r   r   r   rm   r[   rc   rd   absZtotal_secondsrf   re   r   Zcalculate_fields_indexr   Zvalid_head_indexsr   Zvalid_model_fieldschoicesZattnameZget_db_valueprocess_rowr   r!   r"   setattrsaver(   	traceback	print_excr   rC   rE   delete)r)   r   r   r   r   Zhead_lencalculate_dictZ	overwritecountlevelZdsdZimport_heartrd   Zrow_fields_selectr.   r   kr   Zf_indexZ	tmp_fieldZtvr   valuerow_dataZobjsr-   r   r0   )r   r1   data_insert&  sx    







zImportDeptData.data_insertc             C   s   |    d S )N)r   )r)   r0   r0   r1   sqlserver_insertj  s    zImportDeptData.sqlserver_insertc             C   s   |    d S )N)r   )r)   r0   r0   r1   mysql_insertm  s    zImportDeptData.mysql_insertc             C   s   |    d S )N)r   )r)   r0   r0   r1   oracle_insertp  s    zImportDeptData.oracle_insertc             C   s   |    d S )N)r   )r)   r0   r0   r1   postgresql_inserts  s    z ImportDeptData.postgresql_insertc             C   s   t d| j| jf  d S )Nzdept_import_%s_%s)r   r   ra   r   )r)   r0   r0   r1   after_insertv  s    zImportDeptData.after_insertc             C   sf   dt d }||d}|rbtjj|| jd}|sP|}t||| jd}|  n|d }|j|d< |S )Nz%sr   ri   )r   r   )r   r2   r   r   Zparent_dept_id)r:   r[   r   r!   r"   r   r   rD   )r)   r   r   r   rh   parentZpNamer-   r0   r0   r1   r   y  s    

zImportDeptData.process_row)rO   NN)r4   r5   r6   r    rs   r   r   r   r   r   r   r   r   r   r?   r0   r0   )r/   r1   rU      s   uDrU   )#r   rc   Zdjango.formsr   r   r`   r   r:   Zdjango.core.cacher   Zmysite.adminr   Zmysite.admin.actionr   Zmysite.admin.kernelr   Zmysite.admin.sitesr	   Zmysite.personnelr
   r   r   Zmysite.personnel.import_datar   Zmysite.personnel.modelsr   rZ   r   Zdjango.confr   r   r@   rE   rU   registerr0   r0   r0   r1   <module>   s(   <  