B
    bA                 @   s  d dl Z d dl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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 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)cache)	ModelFormModelChoiceField)gettext_lazy)forms)ActionHandleError)ZKModelAdmin)zk_site)actionsdb_constwidgets)
ImportData)get_num_from_list)Position)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ejddZ fddZG d	d
 d
Z  ZS )PositionCreationFormposition_field_code)label
max_lengthposition_field_nameposition_field_parentPositionF)querysetr   widgetrequiredc                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   )position_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\position_admin.pyr!       s    
zPositionCreationForm.__init__c               @   s   e Zd ZeZdZdS )zPositionCreationForm.Meta)r   position_nameparent_positionN)__name__
__module____qualname__r   modelr'   r1   r1   r1   r2   Meta-   s   r9   )r5   r6   r7   r   	CharField_r   MAX_POSITION_CODEr   MAX_POSITION_NAMEr3   r   r   r"   allr   PositionRadioSelectr4   r!   r9   __classcell__r1   r1   )r0   r2   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ejddZG d	d
 d
Z fddZ  ZS )PositionChangeFormr   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PositionChangeForm.Meta)r   r3   r4   N)r5   r6   r7   r   r8   r'   r1   r1   r1   r2   r9   :   s   r9   c                s>   t t| j|| t| dd }|r:|jr:d| jd jjd< d S )NinstanceTr   readonly)r    rA   r!   getattrpkr'   r   Zattrs)r*   r+   r,   rB   )r0   r1   r2   r!   >   s    
zPositionChangeForm.__init__)r5   r6   r7   r   r:   r;   r   r<   r   r=   r3   r   r   r"   r>   r   r?   r4   r9   r!   r@   r1   r1   )r0   r2   rA   2   s   rA   c                   s^   e Zd ZejejgZdZdZdZe	Z
eZdZdZd fdd	Zdd	 Zd
d Zdd Z  ZS )PositionAdmin)r   r   r3   r4   Zemployee_countZresigned_count)r   r3   r4   )Zposition_tree_nodeszposition-parent_position-treezposition-parent_position-map)r   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    rF   get_form)r*   requestr.   r,   defaults)r0   r1   r2   rJ   O   s
    

zPositionAdmin.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;)ImportPositionDataZoptsrP   rQ   Zexe_import_data
error_infor   join)r*   rK   Z
obj_importZ	ret_errorr1   r1   r2   
dataimportY   s    zPositionAdmin.dataimportc             C   s>   |j j}|j js| s,tjj|j jd}n| }|	dS )N)r   r4   )
r-   Zget_auth_posZis_superuserr&   r   r"   r#   r$   r>   Zselect_related)r*   rK   Zauth_posZqsr1   r1   r2   get_queryseta   s
    zPositionAdmin.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duplicate_position_code)mysite.admin.exceptionsr   Zcleaned_datagetr   r"   r#   rB   r   Zexcluder   r&   r;   )r*   Z
model_formr   r   r   Zbkr1   r1   r2   check_domain_rulesj   s     z PositionAdmin.check_domain_rules)N)r5   r6   r7   r
   ZImportZSetPositionZlist_displayZsort_fieldsZlist_filterr   rH   rA   rG   
cache_keysZorderingrJ   rV   rW   rZ   r@   r1   r1   )r0   r2   rF   E   s   
	rF   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 )rS   rM   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pos_import_%s_%s   z%sr   r   r   )django.utils.translationr\   r-   r   ZLANGUAGE_CODEZget_preferencesr    rS   r!   stampr$   r   r   setdatetimenowr;   Zcalculate_fields_verboseZmust_fieldsexist_codespCodepNameppCode)	r*   rN   rO   rP   rQ   r\   ZprofileZdefault_language_codeZlng)r0   r1   r2   r!   u   s$    
 

zImportPositionData.__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insertrf   striprd   append)	r*   elemrecordsstoreZ
check_listZ
check_itemeparent_codep_coder1   r1   r2   is_valid_record   s"    


z"ImportPositionData.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pos_import_processor_{0}_{1}rg   z{0}zValidating...r   )statustotalfinishtpfpi,  Zposition_header_codeZposition_header_nameZposition_header_parentc             S   s   g | ]}|  qS r1   )lower).0itemr1   r1   r2   
<listcomp>   s    z4ImportPositionData.before_insert.<locals>.<listcomp>c             3   s   | ]}| kV  qd S )Nr1   )ry   rl   )valid_headersr1   r2   	<genexpr>   s    z3ImportPositionData.before_insert.<locals>.<genexpr>z,Invalid import data,Please check the headersz%sr   r   r   )r   r   r3   Zparent_position__position_codec                s   g | ]}| j  qS r1   )rd   )ry   ro   )r*   r1   r2   r{      s    c             S   s   g | ]}|d  qS )r   r1   )ry   ro   r1   r1   r2   r{      s       z7error_data_on_row(%(index)s)_the_position_code_is_emptyindexzNerror_data_on_row(%(index)s)_the_length_of_position_code_maximum_is_%(length)s)r   lengthz(Self cant be tagged as parent department)r   r   Z*position_with_the_same_code_already_existsz7error_data_on_row(%(index)s)_the_position_name_is_emptyzNerror_data_on_row(%(index)s)_the_length_of_position_name_maximum_is_%(length)szUerror_data_on_row(%(index)s)_the_length_of_parent_position_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_nameFru   rw   rR      rT   )
cmp_to_keyc             S   s   t | d t |d  S )Nr   )r%   )x1x2r1   r1   r2   <lambda>      z2ImportPositionData.before_insert.<locals>.<lambda>)keyc             S   s   t | d t |d  S )Nr   )r%   )r   r   r1   r1   r2   r     r   T),jsoncopylenrm   formatr_   r   r   r`   dumpsr;   headr>   r   rangerd   re   rf   maxZdeepcopyr   r"   r#   Zvalues_listrc   	enumeraterk   
IndexErrorrT   r   r<   need_update_old_recordr=   rr   rY   floatrU   itemssixZPY3	functoolsr   sortedsort)r*   r   r   _lenprocessor_keyprocessor_dataZheadersresultr   ro   Zmax_lenZall_recordsZ
exist_dataZ	new_codesicZdb_datadataZp_levelrl   rq   Zp_namerp   Z	check_posrn   vr   Zpositonsr   r   Z
list_itemsr1   )r*   r|   r2   before_insert   s    









z ImportPositionData.before_insertc             C   s   d S )Nr1   )r*   Zinsert_positionr1   r1   r2   records_analysis  s    z#ImportPositionData.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}xN| jD ]B\}	}
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|r`xt|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 n4 t"k
r^ } zdd l#}|$  W d d }~X Y nX ||d< t%|t%| |d< t|||d qW x t&t'dg D ]}t(| qW qrW d S )Nr   zpos_import_processor_{0}_{1}rg   z{0}zProcessing...r   )rs   rt   ru   rv   rw   i,  r~   zpos_import_%s_%s
   c                s    g | ]}|d   kr|d qS )r   r   r1   )ry   ro   )	tmp_valuer1   r2   r{   A  s    z2ImportPositionData.data_insert.<locals>.<listcomp>)r   r   ru   rw   r[   ))r   r   rm   r   r_   r   r   r`   r   r   r   rk   rY   ra   rb   absZtotal_secondsrd   rc   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   rD   rF   delete)r*   r   r   r   r   Zhead_lencalculate_dictZ	overwritecountlevelZpspZimport_heartrb   Zrow_fields_selectr/   r   kr   Zf_indexZ	tmp_fieldZtvr   valuerow_dataZobjsr.   ro   r   r1   )r   r2   data_insert  sx    







zImportPositionData.data_insertc             C   s   |    d S )N)r   )r*   r1   r1   r2   sqlserver_insertZ  s    z#ImportPositionData.sqlserver_insertc             C   s   |    d S )N)r   )r*   r1   r1   r2   mysql_insert]  s    zImportPositionData.mysql_insertc             C   s   |    d S )N)r   )r*   r1   r1   r2   oracle_insert`  s    z ImportPositionData.oracle_insertc             C   s   |    d S )N)r   )r*   r1   r1   r2   postgresql_insertc  s    z$ImportPositionData.postgresql_insertc             C   s   t d| j| jf  d S )Nzpos_import_%s_%s)r   r   r_   r   )r*   r1   r1   r2   after_insertf  s    zImportPositionData.after_insertc             C   sb   dt d }||d}|r^tjj|| jd}|sLt||| jd}|  n|d }|j|d< |S )Nz%sr   rg   )r   r   )r   r3   r   r   Zparent_position_id)r;   rY   r   r"   r#   r   r   rE   )r*   r   r   r   rp   parentr.   r1   r1   r2   r   i  s    

zImportPositionData.process_row)rM   NN)r5   r6   r7   r!   rr   r   r   r   r   r   r   r   r   r   r@   r1   r1   )r0   r2   rS   s   s   sCrS   )%r   ra   Zdjango.core.cacher   Zdjango.formsr   r   r^   r   r;   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.utilsr   Zmysite.personnel.modelsr   rX   r   Zdjango.confr   r   rA   rF   rS   registerr1   r1   r1   r2   <module>   s*   .  