
    'iG!                     j    d dl mZ d dlmZ d dlmZmZ d dlmZ  G d de      Z	dZ
 G d d	ee      Zy
)    )models)AbstractTimeStampModel)BaseUserManagerAbstractBaseUser)Rolec                       e Zd ZddZddZy)UserManagerNc                    |st        d      |s|j                  d      d   }|}d}t        j                  j	                  |      j                         r:| | }|dz  }t        j                  j	                  |      j                         r:| j                  | j                  |      ||      }d|_        d|_	        |j                  |       d|_        |j                          |r|j                  j                  |       |j                  sn|D ]M  }	t         j                  j	                  |	      j                         }
|
s4|	|_        |j                          |c S  |r|d   |_        |j                          |S )	zb
        Creates and saves a User with the given email, username, password and companies.
        z Users must have an email address@r      )username)emailr   	user_typeT)company)
ValueErrorsplitUserobjectsfilterexistsmodelnormalize_emailis_staff	is_activeset_passwordmust_change_passwordsave	companiessetactive_companyr   )selfr   r   passwordr   r   base_usernamecounteruserr   company_has_roles              >/var/www/html/dp2/backend_v2.1/server/apps/core/models/user.pycreate_userzUserManager.create_user
   sn    ?@@ {{3'*H %MG,,%%x%8??A+_WI61 ,,%%x%8??A zz&&u-  

 (#$(!		 NNy)&&(G'+||':': ' (; (fh % (.5+		#  ) *3A,D'IIK    c                     | j                  ||||      }d|_        d|_        d|_        d|_        d|_        d|_        |j                          |S )zi
        Creates and saves a superuser with the given email, date of
        birth and password.
        )r"   r   r   TFsuper_admin)r(   is_adminr   r   is_superuserr   r   r   )r!   r   r   r"   r   r%   s         r'   create_superuserzUserManager.create_superuser;   sa    
 	   
  "'!&		r)   )NNr%   N)NNr+   )__name__
__module____qualname__r(   r.    r)   r'   r	   r	   	   s    /br)   r	   ))r%   r   )adminAdmin)r+   zSuper Adminc                   n    e Zd ZdZ ej
                  ddd      Z ej                  dde      Z	 ej                  dddd      Z
 ej                  d	
      Z ej                  d	
      Z ej                  d	
      Z ej                  d	
      Z ej                  dd      Z ej                  dd      Z ej$                  dddd      Z ej(                  dej*                  dddd      Z e       ZdZdgZd Zd%dZd Zd Zd Zd Z d%dZ!d%dZ"d%dZ#d%d Z$d! Z%d" Z& fd#Z' fd$Z( xZ)S )&r   zB
    Our custom user model that extends Django's AbstractBaseUser.zemail address   T)verbose_name
max_lengthuniquer%   )r8   defaultchoices)r8   r9   blanknullF)r:   z<Whether the user is currently available for work/assignments)r:   	help_textz+User must change password after first loginzcompanies.CompanyuserszCompanies this user belongs to)related_namer<   r>   active_usersz&Currently active company for this user)	on_deleter@   r=   r<   r>   r   r"   c                     | j                   S N)r   r!   s    r'   __str__zUser.__str__   s    zzr)   c                      y)z)Does the user have a specific permission?Tr2   )r!   permobjs      r'   has_permzUser.has_perm        r)   c                      y)z;Does the user have permissions to view the app `app_label`?Tr2   )r!   	app_labels     r'   has_module_permszUser.has_module_perms   rK   r)   c                 j    | j                   j                  |j                        j                         S )z+Check if user belongs to a specific company)id)r   r   rP   r   r!   r   s     r'   belongs_to_companyzUser.belongs_to_company   s'    ~~$$

$3::<<r)   c                     | j                   S )z#Get the active company for the user)r    rE   s    r'   get_active_companyzUser.get_active_company   s    """r)   c                 J    | j                   r| j                   j                  S dS )z%Get the name of user's active companyN)r    namerE   s    r'   get_active_company_namezUser.get_active_company_name   s!    +/+>+>t""''HDHr)   c                 J    |xs | j                   }| j                  dv xr |duS )z'Check if user is admin of their company)r3   r+   N)r    r   )r!   r   target_companys      r'   is_company_adminzUser.is_company_admin   s-     7D$7$7~~!99XnTX>XXr)   c                     ddl m} |xs | j                  }|sy|j                  j	                  | |d      j                         }|r|j                  S dS )z%Get user's role in a specific companyr   )UserCompanyRoleNT)r%   r   r   )apps.permissions.modelsr\   r    r   r   firstrole)r!   r   r\   rY   	user_roles        r'   get_role_in_companyzUser.get_role_in_company   s^    ; 7D$7$7#++22" 3 
 %'	 	
 "+y~~44r)   c                     | j                   ry|xs | j                  }|sy| j                  |      }|r:|j                  |k(  r+|j                  j                  |d      j                         S y)z>Check if user has specific permission in their company contextTF)codenamer   )r-   r    ra   r   permissionsr   r   )r!   permission_codenamer   rY   r_   s        r'   has_company_permissionzUser.has_company_permission   st     7D$7$7''7DLLN2##**, +  fh
 r)   c                    | j                   r"ddlm} |j                  j	                  d      S |xs | j
                  }|sg S | j                  |      }|r+|j                  |k(  r|j                  j	                  d      S g S )z>Get all permissions for the user in a specific company contextr   )
PermissionT)r   )	r-   r]   rh   r   r   r    ra   r   rd   )r!   r   rh   rY   r_   s        r'   get_all_permissionszUser.get_all_permissions   s}    :%%,,t,<< 7D$7$7I''7DLLN2##**T*::	r)   c                 V    | j                  |      r|| _        | j                          yy)zSwitch user's active companyTF)rR   r    r   rQ   s     r'   switch_companyzUser.switch_company   s'    ""7+")DIIKr)   c                     | j                  |      r}t        | j                  j                               }||v r|j	                  |       |j                  d|       | j                  j                  |       || _        | j                          yy)z*Set user's default company (first in list)r   TF)	rR   listr   allremoveinsertr   r    r   )r!   r   companies_lists      r'   set_default_companyzUser.set_default_company   sv    ""7+!$.."4"4"67N.(%%g.!!!W-NN~.")DIIKr)   c                     t         |           | j                  rD| j                  | j                        s(ddlm}  |dd| j                  j                   di      yy)z7Validate that active_company is one of user's companiesr   )ValidationErrorr    zActive company "z!" is not in user's companies listN)supercleanr    rR   django.core.exceptionsrt   rV   )r!   rt   	__class__s     r'   rv   z
User.clean   sc    t'>'>t?R?R'S>! $4T5H5H5M5M4NNp"q#   (Tr)   c                 D    | j                          t        |   |i | y)z5Override save to validate active_company relationshipN)rv   ru   r   )r!   argskwargsrx   s      r'   r   z	User.save   s    

d%f%r)   rD   )*r/   r0   r1   __doc__r   
EmailFieldr   	CharFieldUSER_TYPE_CHOICESr   r   BooleanFieldr   r-   r,   r   is_availabler   ManyToManyFieldr   
ForeignKeySET_NULLr    r	   r   USERNAME_FIELDREQUIRED_FIELDSrF   rJ   rN   rR   rT   rW   rZ   ra   rf   ri   rk   rr   rv   r   __classcell__)rx   s   @r'   r   r   W   s   EF$E
 !  0ACIv3t4dSH###E2I&6&&u5L"v""51H"v""51H&6&&PL
 /6..?
 '&&2	I 'V&&//#:N mGN!lO
=#IY
5$ & &r)   r   N)	django.dbr   coresite.mixinr   django.contrib.auth.modelsr   r   r]   r   r	   r   r   r2   r)   r'   <module>r      s>     1 )D/ DN `&3 `&r)   