
    g2i                         d dl mZ d dlmZ d dlmZ d dlmZ  e       Z G d dej                        Z
 G d dej                        Z G d	 d
ej                        Zy)    )serializers)get_user_model)UserProfileSerializer)PermissionSerializerc                       e Zd Z ed      Z ej                         Z ej                         Z G d d      Z	d Z
d Zy)UserListDetailSerializerFrequiredc                       e Zd ZeZg dZy)UserListDetailSerializer.Meta)idusernameemail	user_typeprofile	is_activeis_availableis_staff
last_logincompanies_countcompanies_with_rolesN__name__
__module____qualname__Usermodelfields     I/var/www/html/dp2/backend_v2.1/server/apps/core/serializers/userdetail.pyMetar      s    
r    r"   c                 L    t        |d|j                  j                               S )&Get count of companies user belongs tor   )getattr	companiescountselfobjs     r!   get_companies_countz,UserListDetailSerializer.get_companies_count   s    s-s}}/B/B/DEEr    c                    g }|j                   j                         D ]  }	 |j                  |      }|j
                  |j                  |j                  dd}|rdt        |j                  j                  d      d      j                  }|j
                  |j                  |j                  |j                  |d|d<   |j                  |        |S # t        t        f$ r d}Y w xY w)z1Get all companies and user's role in each companyN)r   namer   roleT)r   )many)r   r-   descriptionr   permissionsr.   )r&   allget_role_in_company	TypeErrorAttributeErrorr   r-   r   r   r1   filterdatar0   append)r)   r*   companies_datacompanyr.   company_infor1   s          r!   get_companies_with_rolesz1UserListDetailSerializer.get_companies_with_roles   s    }}((*G..w7 jj$..	L 243C3C3J3JUY3J3Zaefkk '' II#'#3#3!%#.(V$ !!,/5 +8 1 ~. s   CC&%C&N)r   r   r   r   r   r   SerializerMethodFieldr   r   r"   r+   r<   r   r    r!   r   r      sB    #U3G7k779O<;<<>
 
F r    r   c                       e Zd Z ed      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y	)
UserDetailSerializerFr	   c                       e Zd ZeZg dZy)UserDetailSerializer.Meta)r   r   r   r   r   r   r:   r   company_rolecompany_permissionsmust_change_passwordNr   r   r    r!   r"   rA   G   s     rr    r"   c                 6    |j                   j                         S )r$   )r&   r'   r(   s     r!   r+   z(UserDetailSerializer.get_companies_countO   s    }}""$$r    c                 Z    |j                         }|r|j                  |j                  dS y)zGet active company for the user)r   r-   N)get_active_companyr   r-   )r)   r*   r:   s      r!   get_companyz UserDetailSerializer.get_companyS   s0    ((*jj  r    c                     |j                   sy|j                         }|r/|j                  |j                  |j                  |j
                  dS y)z'Get user's role in their active companyN)r   r-   r0   r   )active_companyr3   r   r-   r0   r   )r)   r*   r.   s      r!   get_company_rolez%UserDetailSerializer.get_company_role]   sL    !!&&(gg		#//!^^	  r    c                     |j                   sg S |j                         }|D cg c]1  }|j                  |j                  |j                  |j
                  d3 c}S c c}w )z.Get user's permissions in their active company)r   r-   codenamer   )rJ   get_all_permissionsr   r-   rM   r   )r)   r*   r1   perms       r!   get_company_permissionsz,UserDetailSerializer.get_company_permissionsl   sg    !!I--/ $
 $ gg		 MM!^^	 $
 	
 
s   6AN)r   r   r   r   r   r   r=   r   rB   rC   r:   r"   r+   rH   rK   rP   r   r    r!   r?   r?   ?   sm    #U3G7k779O4;446L;+;;=/k//1Gr r%
r    r?   c                   2    e Zd Z ed      Z G d d      Zy)UserBasicInfoSerializerFr	   c                       e Zd ZeZddgZy)UserBasicInfoSerializer.Metar   r   Nr   r   r    r!   r"   rT      s    9%r    r"   N)r   r   r   r   r   r"   r   r    r!   rR   rR   }   s    #U3G& &r    rR   N)rest_frameworkr   django.contrib.authr   apps.userprofile.serializersr   %apps.core.serializers.companyuserlistr   r   ModelSerializerr   r?   rR   r   r    r!   <module>rZ      sS    & . > F 2{:: 2h;
;66 ;
|&k99 &r    