B
    Obn                 @   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	 d dl
mZmZ d dlmZ d dlmZ dd	d
ZdddZdddZdd ZG dd deZdS )    N)models)gettext_lazy)AdminRuntimeWarning)db_const)STATUS_VALIDSTATUS_RESIGN)AbstractCloud)cachec             C   s^   | j s| j s4|dkr |  n|}tjj|d}n
| j }|ddd}dd |D }|S )z)
    generate for zTree
    :return:
    N)companyidposition_nameparent_position_idc          	   S   s0   g | ](}t td |d |d |d p$dfqS ))r   nameZpIdr         )dictzip).0obj r   ?G:\easytimepro\master/mysite/personnel\models\model_position.py
<listcomp>   s    z*get_tree_nodes_from_db.<locals>.<listcomp>)	is_superuserget_auth_posexistsZget_current_companyPositionobjectsfilterallvalues_list)userr
   current_companyZquerysetobjs	tree_datar   r   r   get_tree_nodes_from_db   s    
r$   Fc       	      C   s   ddl m} | dkrxddlm} | } | dkrxddlm} | } | dkrxddlm} ddlm} |j	}|j
j|d} | r| jsdt| j }ndt| j }||}|r|s|S t| }|dt| j | |S )	z&
    Cache tree data
    :return:
    r   )r	   N)threadlocals)settings)MyUser)usernameZposition_tree_nodes_)django.core.cacher	   mysite.baser%   get_current_userZdjango.confr&   Zmysite.accounts.modelsr'   ZDEFAULT_ADMIN_NAMEr   getr   strr   r!   r$   set)	r    refreshr	   r%   r&   r'   r(   cache_tree_namer#   r   r   r   get_tree_nodes    s*    

r1   c             C   sr   | dkrddl m} | } | r6| js6dt| j }ndt| j }t|}|rZ|sZ|S t	| }t
|| |S )z&
    Cache tree data
    :return:
    Nr   )r%   Z$position_tree_nodes_without_default_)r*   r%   r+   r   r-   r   r!   r	   r,   r$   r.   )r    r/   r%   r0   r#   r   r   r   get_tree_nodes_without_default=   s    

r2   c              C   sN   t j dddd} i }| r*t| }td| tdt|	  |S )Nr   r   Zsys_position_cacheZsuperuser_position)
r   r   r   Zonlyr   r   r	   r.   listkeys)r"   Z	positionsr   r   r   set_position_cacheQ   s    r5   c                   s   e Zd ZejddZejedej	dZ
ejedejdZejdedddejdZejed	d
dZdd ZG dd dZedd Zdd Zd fdd	Zdd Zede_dd Zede_d  fdd	Z  ZS )!r   T)Zprimary_keyZposition_field_code)Z
max_lengthZposition_field_nameselfZposition_field_parentPosition)verbose_nameZnullZblankZ	on_deleteZposition_field_isDefaultF)defaultc             C   s
   d| j  S )Nz%s)r   )r6   r   r   r   __str__d   s    zPosition.__str__c               @   s(   e Zd ZdgZejZedZeZ	dZ
dS )zPosition.Metar   Zpsnl_model_position))position_coder
   N)__name__
__module____qualname__Zorderingr   Z	APP_LABELZ	app_label_r7   Zverbose_name_pluralZunique_togetherr   r   r   r   Metag   s
   r?   c             C   s   t | jS )N)sixZ	text_typer   )r6   r   r   r   tree_node_namen   s    zPosition.tree_node_namec             C   s   t jj| dS )N)parent_position)r   r   r   )r6   r   r   r   childrenr   s    zPosition.childrenNc       	         s   ddl m} ddlm} | jd k	r:| j| jkr:ttd| jsDdnd}tt	| 
|||| | }|r|rt||s|jr|js|j r|j|  |
  ytdd tdd t  W n tk
r   Y nX d S )Nr   )r%   )AnonymousUserzcan_not_set_parent_to_its_own.TF)r/   )r*   r%   Zdjango.contrib.auth.modelsrD   r   r   r   r>   superr   saver+   
isinstancer   r   r   addr1   r2   r5   	Exception)	r6   Zforce_insertZforce_updateusingZupdate_fieldsr%   rD   Z
action_newZcur_user)	__class__r   r   rF   u   s"    


zPosition.savec             C   s   | j jtd S )N)status)employee_setr   r   count)r6   r   r   r   employee_count   s    zPosition.employee_countZposition_field_employeeCountc             C   s   | j jt| jd S )N)rL   Zposition_id)rM   r   r   r   rN   )r6   r   r   r   resigned_count   s    zPosition.resigned_countZposition_field_resignedCountc                s   g }|    dkr*tddt| ji }| j dkrNtddt| ji }| jr\td}|rht|tt	| j
||d tdd d S )	Nr   z8%(name)s_still_has_sub-positions_and_cannot_be_cancelledr   z4%(name)s_still_has_personnel_and_cannot_be_cancelledz$default_position_can_not_be_removed.)rJ   keep_parentsT)r/   )rC   rN   r>   r-   r   rM   
is_defaultr   rE   r   deleter1   )r6   rJ   rQ   message)rK   r   r   rS      s    zPosition.delete)FFNN)NF)r;   r<   r=   r   Z	AutoFieldr   Z	CharFieldr>   r   ZMAX_POSITION_CODEr:   ZMAX_POSITION_NAMEr   Z
ForeignKeyZCASCADErB   ZBooleanFieldrR   r9   r?   propertyrA   rC   rF   rO   Zshort_descriptionrP   rS   __classcell__r   r   )rK   r   r   [   s"   
 

r   )NN)NF)NF)r@   Z	django.dbr   Zdjango.utils.translationr   r>   Zmysite.admin.exceptionsr   Zmysite.personnelr   Zmysite.admin.modelsr   r   Z!mysite.admin.abstract_cloud_modelr   r)   r	   r$   r1   r2   r5   r   r   r   r   r   <module>   s   



