
    1b(ii                        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  e       Z G d dej                        Z G d d	ej                        Z G d
 d      Z G d dej                        Z G d dej$                        Zy)    )serializers)get_user_model)Company)UserCompanyRole
PermissionRole)UserProfileSerializerc                   $    e Zd ZdZ G d d      Zy)PermissionSerializerz Simplified permission serializerc                       e Zd ZeZg dZy)PermissionSerializer.Meta)idnamecodenameN)__name__
__module____qualname__r   modelfields     N/var/www/html/dp2/backend_v2.1/server/apps/core/serializers/companyuserlist.pyMetar      s    +r   r   N)r   r   r   __doc__r   r   r   r   r   r      s    *, ,r   r   c                   L    e Zd ZdZ ej
                         Z G d d      Zd Zy)RoleWithPermissionsSerializerz(Serializer for Role with its permissionsc                       e Zd ZeZg dZy)"RoleWithPermissionsSerializer.Meta)r   r   description	is_activepermissionsN)r   r   r   r   r   r   r   r   r   r   r      s    Jr   r   c                     t        |dd       }||j                  j                  d      }t        |d      j                  S )N_cached_permissionsTr    many)getattrr!   filterr   data)selfobjpermss      r   get_permissionsz-RoleWithPermissionsSerializer.get_permissions   s@    s148=OO**T*:E#E5:::r   N)	r   r   r   r   r   SerializerMethodFieldr!   r   r-   r   r   r   r   r      s&    23+335KK K;r   r   c                   P    e Zd ZdZed        Zed        Zed        Zed        Zy)RolePermissionHelperzK
    Central place to fetch user's roles and permissions with caching.
    c                    t        | dg       D ])  }|j                  |j                  k(  s|j                  c S  t        j
                  j                  | |d      j                  d      j                         }|r|j                  S dS )zR
        Uses pre-fetched role if available; otherwise queries only once.
        _cached_rolesT)usercompanyr    roleN)	r'   
company_idr   r5   r   objectsr(   select_relatedfirst)r3   r4   ucrs      r   get_user_role_in_companyz-RolePermissionHelper.get_user_role_in_company)   s~     4"5C~~+xx 6 %%,, - 
 .
 	 	 sxx(D(r   c                 l    | sg S t        | j                  j                  d      d      j                  S )zGet permissions for a roleTr$   r%   )r   r!   r(   r)   )r5   s    r   get_role_permissionsz)RolePermissionHelper.get_role_permissions;   s7     I###d#3$

$	r   c                 N    t         j                  | |      }|r|j                  S dS )z!Get user's role name in a companyN)r0   r;   r   r3   r4   r5   s      r   get_role_namez"RolePermissionHelper.get_role_nameE   s'     $<<T7K tyy*d*r   c                 X    t         j                  | |      }t         j                  |      S )z,Get user's permissions in a specific company)r0   r;   r=   r?   s      r   get_user_permissions_in_companyz4RolePermissionHelper.get_user_permissions_in_companyK   s'     $<<T7K#88>>r   N)	r   r   r   r   staticmethodr;   r=   r@   rB   r   r   r   r0   r0   $   sW     ) )"   + +
 ? ?r   r0   c                   v   e Zd ZdZ ed      Z ej                  dd      Z ej                  dd      Z	 ej                  dd      Z
 ej                         Z ej                         Z ej                         Z ej                         Z ej                         Z G d	 d
      Zd Zd Zd Zd Zd Zy)CompanyUserSerializerz>
    Full user serializer with company & role information
    F)requiredzprofile.first_nameT)source	read_onlyzprofile.last_namezprofile.phone_numberc                       e Zd ZeZg dZy)CompanyUserSerializer.Meta)r   usernameemail	user_typer    is_availableprofile
first_name	last_namephone_numberr6   company_namer5   companies_countcompanies_with_rolesN)r   r   r   Userr   r   r   r   r   r   rJ   c   s    
r   r   c                 X    | j                   j                  d      }|r|j                  S dS )zGet company ID from contextr4   N)contextgetr   r*   r+   r4   s      r   get_company_idz$CompanyUserSerializer.get_company_idm   s'    ,,""9-$wzz.$.r   c                 X    | j                   j                  d      }|r|j                  S dS )zGet company name from contextr4   N)rX   rY   r   rZ   s      r   get_company_namez&CompanyUserSerializer.get_company_namer   s'    ,,""9-&w||0D0r   c                 6    |j                   j                         S )z&Get count of companies user belongs to)	companiescount)r*   r+   s     r   get_companies_countz)CompanyUserSerializer.get_companies_countw   s    }}""$$r   c                     | j                   j                  d      }|syt        j                  ||      }|syt	        |      j
                  S )zZ
        Return the user's role in the company passed via query params (context).
        r4   N)rX   rY   r0   r;   r   r)   )r*   r+   r4   r5   s       r   get_rolezCompanyUserSerializer.get_role{   sG    
 ,,""9-#<<S'J,T2777r   c           	      |   g }t        |dg       D ci c]  }|j                  |j                   }}|j                  j	                         D ]h  }|j                  |j                        }|j                  |j                  |j                  |j                  |rt        |      j                  ndd       j |S c c}w )z1Get all companies and user's role in each companyr2   N)r   r   r    r5   )r'   r6   r5   r_   allrY   r   appendr   r    r   r)   )r*   r+   resultsr:   cached_rolesr4   r5   s          r   get_companies_with_rolesz.CompanyUserSerializer.get_companies_with_roles   s    <CCZ\<]^<]S0<]^}}((*G##GJJ/DNNjj$..DH5d;@@d	 	 +  _s   B9N)r   r   r   r   r	   rO   r   	CharFieldrP   rQ   rR   r.   r6   rS   rT   rU   r5   r   r[   r]   ra   rc   ri   r   r   r   rE   rE   R   s    
 $U3G&&&.BdSJ%%%-@DQI(;((0FRVWL2224J4;446L7k779O<;<<>,;,,.D
 
/
1
%8r   rE   c                       e Zd ZdZ ej
                  d      Z ej                         Z ej                         Z	d Z
d Zy)UserCompanyWithRoleSerializerz@Serializer for a single company with user's role and permissionsr   )rG   c                 l    | j                   j                  d      }|rt        j                  ||      S dS )z1Get user's role name in this company using helperr3   N)rX   rY   r0   r@   r*   r+   r3   s      r   rc   z&UserCompanyWithRoleSerializer.get_role   s1    ||'@D#11$<N$Nr   c                 l    | j                   j                  d      }|rt        j                  ||      S g S )z<Get permissions for user's role in this company using helperr3   )rX   rY   r0   rB   rn   s      r   r-   z-UserCompanyWithRoleSerializer.get_permissions   s2    ||'RV#CCD#N^\^^r   N)r   r   r   r   r   rj   rS   r.   r5   r!   rc   r-   r   r   r   rl   rl      sC    J(;((7L,;,,.D3+335KO
_r   rl   N)rest_frameworkr   django.contrib.authr   apps.companies.modelsr   apps.permissions.modelsr   r   r   apps.userprofile.serializersr	   rV   ModelSerializerr   r   r0   rE   
Serializerrl   r   r   r   <module>rw      sy    & . ) E E > ,;66 ,;K$?$? ;"+? +?\IK77 IX_K$:$: _r   