B
    ObN                 @   s<  d dl Z 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
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Zd d
lmZ d dlmZ d dlmZ G dd de	Zdd ZG dd de
Zdd Zeej eddd Z!eej"eddd Z#dd Z$e$e_%ej&d  dkr*e$e_'ne$e_(dej)_*dS )    N)auth)signals)receiver)BaseUserManagerAbstractUser_user_has_perm_user_has_module_perms)
Permission)
validators)PermissionDenied)models)timezone)gettext_lazy)settingsc               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )MyUserManagerc          	   K   sX   t  }|std| |}| jf |||d||d|}|| |j| jd |S )zW
        Creates and saves a User with the given username, email and password.
        zThe given username must be setT)usernameemailis_staff	is_active
last_logindate_joined)using)r   now
ValueErrornormalize_emailmodelset_passwordsave_db)selfr   r   passwordr   extra_fieldsr   user r#   7G:\easytimepro\master/mysite/accounts\models\my_user.py_create_user   s    


zMyUserManager._create_userc          	   K   sT   t  }|std| |}| jf |||d||d|}||_|j| jd |S )zW
        Creates and saves a User with the given username, email and password.
        zThe given username must be setT)r   r   r   r   r   r   )r   )r   r   r   r   r   r    r   r   )r   r   r   r    r   r!   r   r"   r#   r#   r$   _cloud_create_user)   s    

z MyUserManager._cloud_create_userc             K   s   | j |||df|}|S )NT)r%   )r   r   r   r    r!   r"   r#   r#   r$   create_user:   s    zMyUserManager.create_userc             K   s   | j |||df|}|S )NF)r%   )r   r   r   r    r!   r"   r#   r#   r$   cloud_create_userB   s    zMyUserManager.cloud_create_userc             K   s   | j |||df|}|S )NT)r%   )r   r   r   r    r!   r"   r#   r#   r$   create_superuserJ   s    zMyUserManager.create_superuserc             K   s   | j |||df|}|S )NT)r&   )r   r   r   r    r!   r"   r#   r#   r$   cloud_create_superuserP   s    z$MyUserManager.cloud_create_superuserN)	__name__
__module____qualname__r%   r&   r'   r(   r)   r*   r#   r#   r#   r$   r      s   r   c          	   C   sN   xHt  D ]<}t|dsq
y|| ||r.dS W q
 tk
rD   dS X q
W dS )zV
    A backend can raise `PermissionDenied` to short-circuit permission checking.
    has_model_op_permsTF)r   Zget_backendshasattrr.   r   )r"   	app_label
model_nameZbackendr#   r#   r$   _user_has_model_op_permsW   s    

r2   c            
       sz  e Zd ZejdejdddZejedddede	
ded	d
gdZejeddedde	
ded	d
gdZejeddddZejeddddZejeddddZejdddddZejedddZejddddZejdddddZejddddZejddddZejeddddddZejeddeddZejed ded!dZejdddd"ZdZejdddd"Zejddddd#Zej ed$e!j"d%Z#ej ed&ddd'Z$ejd(dddZ%e& Z'd)Z(ejed*ddd+Z)ej*ed,ddd-Z+ej*ed.ddd-Z,ej*ed/ddd-Z-ej*ed0ddd-Z.ejed1ddddZ/ejed2ddddZ0G d3d4 d4Z1e2d5d6 Z3e2d7d8 Z4e2d9d: Z5d;d< Z6ed=e6_7d>d? Z8ed@e8_7dAdB Z9dj fdDdE	Z:dFdG Z;e2dHdI Z<e2dJdK Z=e2dLdM Z>dNdO Z?dPdQ Z@dRdS ZAdTdU ZBdVdW ZCdXdY ZDdZd[ ZEd\d] ZFdkd^d_ZGd`da ZHdldbdcZIddde ZJdfdg ZKdhdi ZL  ZMS )mMyUser$   TF)
max_lengthdefaultZprimary_keyeditableZuser_field_username   Zuser_field_usernameHelpTxtz^[\w.@+-]+$Zuser_field_usernameValidatorZinvalid)r5   unique	help_textr
    )r5   r:   r6   r
   Zuser_field_password   )r5   r7   Zuser_field_firstName)r5   blankZuser_field_lastName)r5   nullr=   r7   Zuser_field_email)r=   )r5   r>   r=   r   )r>   r6   r=   r7   )r>   r=   r6   Zuser_field_loginTimes)r>   r=   r6   r7   Zuser_field_staffZuser_field_staffStatusHelpTxt)r6   r:   Zuser_field_activeZuser_field_activeStatusHelpTxt)r6   r=   r7   )r6   r7   r>   r=   Zuser_field_dateJoined)r6   Zuser_field_lastLogin)r=   r>      r   ZuserProfile_field_username)r5   r6   ZuserProfile_field_pinTabs)r6   r=   ZuserProfile_field_hideColumnsZuserProfile_field_orderZuserProfile_field_preferencesZuser_field_current_companyZuser_field_recent_companyc               @   s*   e Zd ZdgZedZeZdZdZdZ	dS )zMyUser.MetaidZbase_model_userZ	auth_user)addZchange))Zenter_system_moduleZcan_enter_menu_system_module)Zenter_personnel_moduleZcan_enter_personnel_module)Zenter_terminal_moduleZcan_enter_terminal_module)Zenter_attendance_moduleZcan_enter_attendance_module)Zenter_payroll_moduleZcan_enter_payroll_module)Zenter_access_moduleZcan_enter_access_moduleN)
r+   r,   r-   ordering_Zverbose_nameZverbose_name_pluralZdb_tableZdefault_permissionsZpermissionsr#   r#   r#   r$   Meta   s   rD   c             C   s4   ddl m} |jj| j| d}|jdkr,dnd}|S )Nr   )
Membership)
company_idr"      TF)mysite.base.modelsrE   objectsgetcurrent_companyrole)r   rE   
membership	superuserr#   r#   r$   is_superuser   s    zMyUser.is_superuserc             C   s\   ddl m} ddlm} |jj| j| d}|dt| j d}|j	dkrT|sTd}nd}|S )	zHuse for dashboard check when click current company, need display 3 cardsr   )cache)rE   )rF   r"   Zcurrent_company_btn_clicked_rG   TF)
django.core.cacherP   rH   rE   rI   rJ   rK   strZpkrL   )r   rP   rE   rM   Zcurrent_company_btn_clickedrN   r#   r#   r$   is_superuser_extra   s    zMyUser.is_superuser_extrac             C   s   | j dkrd}nd}|S )NZadminTF)r   )r   Zsuper_adminr#   r#   r$   is_super_admin   s    
zMyUser.is_super_adminc             C   sH   ddl m} ddlm} | }|jj|j| d}|jdkr@dnd}|S )Nr   )rE   )threadlocals)rF   r"   rG   TF)	rH   rE   mysite.baserU   get_current_userrI   rJ   rK   rL   )r   rE   rU   r"   rM   rN   r#   r#   r$   is_superuser_display   s    zMyUser.is_superuser_displayzIs superuserc             C   sD   ddl m} ddlm} | }|jj|j| d}|jr<dnd}|S )Nr   )rE   )rU   )rF   r"   TF)	rH   rE   rV   rU   rW   rI   rJ   rK   vendor_company)r   rE   rU   r"   rM   rN   r#   r#   r$   is_vendor_display   s    zMyUser.is_vendor_displayz	Is vendorc             C   sJ   ddl m} ddlm} | }|jj|j| d}|jrB|jj	}nd }|S )Nr   )rE   )rU   )rF   r"   )
rH   rE   rV   rU   rW   rI   rJ   rK   rY   r@   )r   rE   rU   r"   rM   vendor_company_idr#   r#   r$   r[      s    
zMyUser.vendor_company_idNc                s   ddl m} ddlm} | jsz| }|d ks8t||rnddlm} |j	
 }	|	 }
|
dkrh|	d jnd }n|j}|| _tt| |||| |   d S )Nr   )AnonymousUser)rU   )CompanyrG   )django.contrib.auth.modelsr\   rV   rU   rK   rW   
isinstance!mysite.cloud.models.model_companyr]   rI   allcountr@   superr3   r   _update_cache_key)r   Zforce_insertZforce_updater   update_fieldsr\   rU   Z
admin_userr]   Zcompany_queryrb   rK   )	__class__r#   r$   r      s    
zMyUser.savec             C   sh   ddl m} |dt| j  |dt| j  d}d}| jrH|}n|}||j| jd| d d	S )
z3
        update some keys for refresh data
        r   )rP   Zdepartment_tree_nodes_Zarea_tree_nodes_zuser_id_{obj}zemp_id_{obj})objiQ N)rQ   rP   deleterR   r@   is_employeesetformat)r   rP   ZUSER_CACHE_KEYZEMP_CACHE_KEYZ	cache_keyr#   r#   r$   rd      s    zMyUser._update_cache_keyc             C   s6   ddl m} |  }|jj|| d}|r2|d jS d S )Nr   )rE   )companyr"   )rH   rE   get_current_companyrI   filterZ	auth_area)r   rE   rl   rM   r#   r#   r$   get_auth_area  s    
zMyUser.get_auth_areac             C   s6   ddl m} |  }|jj|| d}|r2|d jS d S )Nr   )rE   )rl   r"   )rH   rE   rm   rI   rn   Z	auth_dept)r   rE   rl   rM   r#   r#   r$   get_auth_dept  s    
zMyUser.get_auth_deptc             C   s6   ddl m} |  }|jj|| d}|r2|d jS d S )Nr   )rE   )rl   r"   )rH   rE   rm   rI   rn   Zauth_pos)r   rE   rl   rM   r#   r#   r$   get_auth_pos  s    
zMyUser.get_auth_posc             C   s,   ddl m} |jj| jd}|r(|d S d S )Nr   )r]   )r@   )r`   r]   rI   rn   rK   )r   r]   rK   r#   r#   r$   rm     s
    zMyUser.get_current_companyc             C   s   |   }|j S )N)rm   nameupper)r   rK   r#   r#   r$   get_current_company_name$  s    zMyUser.get_current_company_namec             C   s   d| j | jf }| S )zU
        Returns the first_name plus the last_name, with a space in between.
        z%s %s)
first_name	last_namestrip)r   Z	full_namer#   r#   r$   get_full_name)  s    zMyUser.get_full_namec             C   s   | j S )N)ru   )r   r#   r#   r$   get_short_name0  s    zMyUser.get_short_namec             C   s"   t js| jst| | jS t| dS )Nnickname)r   CLOUD_VERSIONrz   getattrUSERNAME_FIELD)r   r#   r#   r$   get_username4  s    zMyUser.get_usernamec             C   s   |   S )N)r~   )r   r#   r#   r$   __unicode__:  s    zMyUser.__unicode__c             C   s   |   S )N)r~   )r   r#   r#   r$   __str__=  s    zMyUser.__str__c             C   s   | j r| jrdS t| ||S )z
        Returns True if the user has any operation permissions in the given app label.
        Uses pretty much the same logic as has_perm, above.
        T)r   rO   r2   )r   r0   r1   r#   r#   r$   r.   C  s    zMyUser.has_model_op_permsc             C   s    | j r| jrt|S t| ||S )N)r   rO   check_perms_availabler   )r   permrg   r#   r#   r$   has_permN  s    zMyUser.has_permc             C   s   | j r| jrdS t| |S )z
        Returns True if the user has any permissions in the given app label.
        Uses pretty much the same logic as has_perm, above.
        T)r   rO   r   )r   r0   r#   r#   r$   has_module_permsV  s    zMyUser.has_module_permsc             C   sN   ddl m} t| dd s.| j}||t}|| _|d k	rD| j||S | jS d S )Nr   )load_from_json_saved_preferences)Zmysite.admin.utilsr   r|   preferencesdictr   rJ   )r   keyr6   r   ZsavedZdecode_savedr#   r#   r$   get_preferencesb  s    
zMyUser.get_preferencesc             C   s*   dd l }|  }|| ||| _d S )Nr   )jsonr   updatedumpsr   )r   Z	new_attrsr   Zsaved_preferencer#   r#   r$   set_preferencesn  s    
zMyUser.set_preferencesc                s&   ddl m}  fdd}|| j|S )z~
        Return a boolean of whether the raw_password was correct. Handles
        hashing formats behind the scenes.
        r   )check_passwordc                s"     |  d  _ jdgd d S )Nr    )re   )r   	_passwordr   )raw_password)r   r#   r$   setter{  s    
z%MyUser.check_password.<locals>.setter)django.contrib.auth.hashersr   r    )r   r   r   r   r#   )r   r$   r   t  s    zMyUser.check_passwordc             C   s    ddl m} ||| _|| _d S )Nr   )make_password)r   r   r    r   )r   r   r   r#   r#   r$   r     s    
zMyUser.set_password)FFNN)N)NN)Nr+   r,   r-   r   Z	CharFielduuidZuuid1r@   rC   r
   ZRegexValidatorr   rz   r    ru   rv   Zemp_pinZ
EmailFieldr   Z	tel_phoneZIntegerFieldZauth_time_deptZlogin_idZ
login_typeZlogin_countZBooleanFieldr   r   Z	is_publicri   Zcan_manage_all_deptZdel_flagZDateTimeFieldr   r   r   r   mobiler   rI   r}   Z
login_nameZ	TextFieldZpin_tabsZdisabled_fieldsZcolumn_orderr   rK   Zrecent_companyrD   propertyrO   rS   rT   rX   Zshort_descriptionrZ   r[   r   rd   ro   rp   rq   rm   rt   rx   ry   r~   r   r   r.   r   r   r   r   r   r   __classcell__r#   r#   )rf   r$   r3   f   s   
	

 			

r3   c             C   sb   ddl m} |dd}d}|d kr(d}n| |kr4d}|rN|dd}|sNtd| |krZdS dS d S )	Nr   )maintain_perms_cacheZfetch)opFTr   z@Please ensure `Memcached`(or other cache back-end) is running!!1)Zmysite.base.managementr   AssertionError)r   r   Z_cached_permsZreload_cachedr#   r#   r$   r     s    

r   )senderc             K   s   t jrddlm} |rJ|jd|j|j|jdd}d}|||d}| }	n|j}
d}|
dd	}||d |d
}| }	d|	krd|	d kr|	d d d d }|j	|j
|j|j|jdd}d|}|||d}| }	d S )Nr   )!HttpRequestInterfaceForIoTServicer;   )Z	firstNameZlastNamer   Zphoner    ZprofilePhotoz6 https://authorization.minervaiotdev.com/api/v2.0/user)bodyz= https://authorization.minervaiotdev.com/api/v2.0/user/searchrG   )r   
pageNumber)r   paramsdatar"   r@   z9 https://authorization.minervaiotdev.com/api/v2.0/user/{})r   r{   "mysite.cloud.minerva_iot_interfacer   r   r   r    ZpostrJ   ru   rv   rk   Zput)r   instanceZcreatedrawkwargsr   r   urlinterface_objectcontentr   r   user_idr#   r#   r$   ,save_or_update_user_to_authorization_service  s:    


r   c       
      K   sz   t jrvddlm} |j}d}|dd}||d |d}| }|d rv|d d d d	 }	|	rvd
|	}||}| }d S )Nr   )r   z= https://authorization.minervaiotdev.com/api/v2.0/user/searchrG   )r   r   )r   r   r   r"   r@   z9 https://authorization.minervaiotdev.com/api/v2.0/user/{})r   r{   r   r   r   rJ   rk   rh   )
r   r   r   r   r   r   r   r   r   r   r#   r#   r$   $delete_user_to_authorization_service  s    

r   c             C   s*   d t| jjt| jjt| jS )Nz{}|{}|{})rk   sixZ	text_typeZcontent_typer0   r   Zcodename)r   r#   r#   r$   __new_permission_repr__  s    r   3)Zcontent_type__app_labelZcontent_type__modelr@   )+r   sysZdjango.contribr   Zdjango.db.modelsr   Zdjango.dispatch.dispatcherr   r^   r   r   r   r   r	   Zdjango.corer
   Zdjango.core.exceptionsr   Z	django.dbr   r   Zdjango.utilsr   Zdjango.utils.translationr   rC   Zdjango.confr   r   r2   r3   r   Z	post_saver   Zpost_deleter   r   __repr__versionr   r   Z_metarB   r#   r#   r#   r$   <module>   s6   ?  %)