
    vg                        d dl 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m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 d dlmZmZmZmZmZmZmZ d dlm Z 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.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ d ZAd ZBd ZC G d de      ZD G d de      ZE G d de      ZF G d  d!e      ZG G d" d#e      ZH G d$ d%e      ZI G d& d'e      ZJ G d( d)e      ZK G d* d+e      ZL G d, d-e      ZM G d. d/e      ZN G d0 d1e      ZO G d2 d3e      ZP G d4 d5e      ZQ G d6 d7e      ZR G d8 d9e      ZS G d: d;e      ZT G d< d=e      ZU G d> d?e      ZVy@)A    )ProtectedErrorQ)Http404)method_decorator)DjangoFilterBackendstatus)PageNumberPagination)IsAuthenticated)Response)APIView)DisciplinaryActionFilterDocumentRequestFilterEmployeeFilter)
ActiontypeDisciplinaryActionEmployeeEmployeeBankDetailsEmployeeTypeEmployeeWorkInformationPolicy)work_info_exportwork_info_import)owner_can_enter)permission_required)get_next_badge_id)DocumentDocumentRequest)notify   )$manager_or_owner_permission_requiredmanager_permission_required)or_condition)groupby_querysetpermission_based_queryset)ActiontypeSerializerDisciplinaryActionSerializerDocumentRequestSerializerDocumentSerializerEmployeeBankDetailsSerializerEmployeeListSerializerEmployeeSelectorSerializerEmployeeSerializerEmployeeTypeSerializer!EmployeeWorkInformationSerializerPolicySerializerc                 8    | j                   j                  |      S N)userhas_perm)requestperms     8/var/www/horilla/horilla_api/api_views/employee/views.pypermission_checkr8   5   s    <<  &&    c                 j    	 | j                   j                  |      }|S # | j                  $ r Y y w xY w)Nid)objectsgetDoesNotExist)clspkobjs      r7   object_checkrC   9   s8    kkooo$
 s     22c                     	 | j                   j                  |      j                          y# t        $ r}dt	        |      idfcY d }~S d }~ww xY w)Nr;   )    error  )r=   r>   delete	Exceptionstr)r@   rA   es      r7   object_deleterM   A   sI    &2%%' &Q #%%&s   *- 	AA
A
Ac                       e Zd ZdZddZy)EmployeeTypeAPIViewz
    Retrieves employee types.

    Methods:
        get(request, pk=None): Returns a single employee type if pk is provided, otherwise returns all employee types.
    Nc                    |rBt         j                  j                  |      }t        |      }t	        |j
                  d      S t         j                  j                         }t        |d      }t	        |j
                  d      S )Nr;   rF   r   Tmany)r   r=   r>   r.   r   dataall)selfr5   rA   employee_type
serializers        r7   r>   zEmployeeTypeAPIView.getQ   sj    (00444;M/>JJOOC88$,,002+ME

44r9   r2   )__name__
__module____qualname____doc__r>    r9   r7   rO   rO   I   s    5r9   rO   c                   ~    e Zd ZdZegZeZegZ	d	dZ
 e ed            d        Zd Z e ed            d        Zy)
EmployeeAPIViewar  
    Handles CRUD operations for employees.

    Methods:
        get(request, pk=None):
            - Retrieves a single employee by pk if provided.
            - Retrieves and filters all employees if pk is not provided.

        post(request):
            - Creates a new employee if the user has the 'employee.change_employee' permission.

        put(request, pk):
            - Updates an existing employee if the user is the employee, a manager, or has 'employee.change_employee' permission.

        delete(request, pk):
            - Deletes an employee if the user has the 'employee.delete_employee' permission.
    Nc                 ~   |rA	 t         j                  j                  |      }t        |      }t	        |j                        S t               }t         j                  j                         }| j                  |j                  |      j                  }|j                  j                  dd       }|r|j                         }	t        ||	||      S |j!                  ||      }
t        |
d      }|j#                  |j                        S # t         j                  $ r  t	        ddit
        j                        cY S w xY w)	NrA   rG   Employee does not existr   querysetgroupby_fieldTrQ   )r   r=   r>   r?   r   r	   HTTP_404_NOT_FOUNDr-   rS   r
   rT   filterset_classGETqsbuild_absolute_urir$   paginate_querysetget_paginated_response)rU   r5   rA   employeerW   	paginatoremployees_querysetemployees_filter_queryset
field_nameurlpages              r7   r>   zEmployeeAPIView.getr   s%   #++//2/6 ,H5JJOO,,(*	%--113$($8$8KK"4 %9 %

" 	" [[___d;
,,.C#GS*>WXX**+DgN'48
//
@@% (( 78!44 s    D	 	0D<;D<zemployee.add_employeec                    t        |j                        }|j                         r5|j                          t	        |j                  t
        j                        S t	        |j                  t
        j                        S NrS   r   )	r-   rS   is_validsaver   r	   HTTP_201_CREATEDerrorsHTTP_400_BAD_REQUESTrU   r5   rW   s      r7   postzEmployeeAPIView.post   sV    'W\\:
 OOJOOF4K4KLL
))&2M2MNNr9   c                    |j                   }t        j                  j                  |      }||j                  |j                   j                  j                         fv s|j                  d      rrt        ||j                  d      }|j                         r%|j                          t        |j                        S t        |j                  t        j                        S t        ddid      S )	Nr`   employee.change_employeeTrS   partialr   rG   zYou don't have permissionrH   )r3   r   r=   r>   employee_getget_reporting_managerr4   r-   rS   rv   rw   r   ry   r	   rz   )rU   r5   rA   r3   rl   rW   s         r7   putzEmployeeAPIView.put   s    ||##''2'.!!7<<#<#<#R#R#TUV]]56+H7<<QUVJ""$!
00J--f6Q6QRR"=>sKKr9   employee.delete_employeec                 r   	 t         j                  j                  |      }|j                          t        t        j                        S # t         j                  $ r  t        ddit        j                        cY S t        $ r0}t        dt        |      it        j                        cY d }~S d }~ww xY w)Nr`   rG   ra   r   )r   r=   r>   rI   r?   r   r	   re   r   rK   HTTP_204_NO_CONTENT)rU   r5   rA   rl   rL   s        r7   rI   zEmployeeAPIView.delete   s    	R''++r+2HOO v99:: $$ 	34V=V=V   	RWc!f-f6P6PQQ	Rs#   0A 0B6>B6%B1+B61B6r2   )rX   rY   rZ   r[   r   filter_backendsr   rf   r   permission_classesr>   r   r   r|   r   rI   r\   r9   r7   r^   r^   [   so    $ ++O$O)*A0 )*ABCO DOL )*DEF
; G
;r9   r^   c                       e Zd ZdZegZd Zy)EmployeeListAPIViewa  
    Retrieves a paginated list of employees with optional search functionality.

    Methods:
        get(request):
            - Returns a paginated list of employees.
            - Optionally filters employees based on a search query in the first or last name.
    c                 ~   t               }d|_        |j                  j                  dd       }|r7t        j
                  j                  t        |      t        |      z        }nt        j
                  j                         }|j                  ||      }t        |d      }|j                  |j                        S )N   search)employee_first_name__icontains)employee_last_name__icontainsTrQ   )r
   	page_sizequery_paramsr>   r   r=   filterr   rT   rj   r+   rk   rS   )rU   r5   rm   r   rn   rr   rW   s          r7   r>   zEmployeeListAPIView.get   s    (*	 	%%))(D9!)!1!1!8!88&9:"
 "*!1!1!5!5!7**+=wG+Dt<
//
@@r9   N)rX   rY   rZ   r[   r   r   r>   r\   r9   r7   r   r      s     **Ar9   r   c                   |    e Zd ZdZegZd Zd
dZ ee	d      d        Z
 ee	d      d        Z ed      d	        Zy)EmployeeBankDetailsAPIViewa?  
    Manage employee bank details with CRUD operations.

    Methods:
        get(request, pk=None):
            - Retrieves bank details for a specific employee if `pk` is provided.
            - Returns a paginated list of all employee bank details if `pk` is not provided.

        post(request):
            - Creates a new bank detail entry for an employee.

        put(request, pk):
            - Updates existing bank details for an employee identified by `pk`.

        delete(request, pk):
            - Deletes bank details for an employee identified by `pk`.
    c                     t         j                  j                         }| j                  j                  }d}t        |||      }|S )Nzbase.view_employeebankdetails)r   r=   rT   r5   r3   r%   )rU   rc   r3   r6   s       r7   get_querysetz'EmployeeBankDetailsAPIView.get_queryset   s=    &..224||  .,T4Br9   Nc                 N   t         j                  j                  |      }|j                  j                  |j
                  |j
                  j                         fv s|j                  j                  d      r t        |      }t        |j                        S t        ddid      S )Nr`   z!employee.view_employeebankdetailsmessageNo permissionrH   r   )r   r=   r>   r3   r   employee_idr   r4   r*   r   rS   rU   r5   rA   bank_detailrW   s        r7   r>   zEmployeeBankDetailsAPIView.get   s    )11555<LL%%''''==?
 \\""#FG6{CJJOO,,O4SAAr9   z employee.add_employeebankdetailsc                    t        |j                        }|j                         r5|j                          t	        |j                  t
        j                        S t	        |j                  t
        j                        S rt   )	r*   rS   rv   rw   r   r	   rx   ry   rz   r{   s      r7   r|   zEmployeeBankDetailsAPIView.post   sX     3E
 OOJOOF4K4KLL
))&2M2MNNr9   c                    	 t         j                  j                  |      }t        ||j                        }|j                         r%|j                          t	        |j                        S t	        |j                  t
        j                        S # t         j                  $ r  t	        ddit
        j                        cY S w xY w)Nr`   rG   Bank details do not existr   ru   )r   r=   r>   r?   r   r	   re   r*   rS   rv   rw   ry   rz   r   s        r7   r   zEmployeeBankDetailsAPIView.put   s    	-5599R9@K 3;W\\R
 OOJOO,,
))&2M2MNN #// 	56v?X?X 	s    B 0CCz#employee.change_employeebankdetailsc                 V   	 t         j                  j                  |      }|j                          t        t        j                        S # t         j                  $ r  t        ddit        j                        cY S t        $ r"}t        dt        |      id      cY d }~S d }~ww xY w)Nr`   rG   r   r   rH   )r   r=   r>   rI   r?   r   r	   re   rJ   rK   r   )rU   r5   rA   r   Es        r7   rI   z!EmployeeBankDetailsAPIView.delete  s    	;-5599R9@K  v99:: #// 	56v?X?X   	;Wc!f-c::	;s#   0A 0B(>B(B#B(#B(r2   )rX   rY   rZ   r[   r   r   r   r>   r!   r   r|   r   r"   rI   r\   r9   r7   r   r      sz    $ **B *?OO *?OO !!FG; H;r9   r   c                       e Zd ZdZegZd Z ed      d        Z ed      d        Z	 e
 ed      d	      d
        Zy)EmployeeWorkInformationAPIViewa  
    Manage employee work information with CRUD operations.

    Methods:
        get(request, pk):
            - Retrieves work information for a specific employee identified by `pk`.

        post(request):
            - Creates a new work information entry for an employee.

        put(request, pk):
            - Updates existing work information for an employee identified by `pk`.

        delete(request, pk):
            - Deletes work information for an employee identified by `pk`.
    c                 6   t         j                  j                  |      }|j                  j                  |j
                  |j                  fv s|j                  j                  d      r"t        |      }t        |j                  d      S t        ddid      S )Nr`   z%employee.view_employeeworkinformationrF   r   r   r   rH   )r   r=   r>   r3   r   r   reporting_manager_idr4   r/   r   rS   rU   r5   rA   	work_inforW   s        r7   r>   z"EmployeeWorkInformationAPIView.get3  s    +337727>	LL%%%%y'E'EFG\\""#JK:9EJJOOC88O4SAAr9   $employee.add_employeeworkinformationc                    t        |j                        }|j                         r5|j                          t	        |j                  t
        j                        S t	        |j                  t
        j                        S rt   )	r/   rS   rv   rw   r   r	   rx   ry   rz   r{   s      r7   r|   z#EmployeeWorkInformationAPIView.post=  sV    6GLLI
 OOJOOF4K4KLL
))&2M2MNNr9   'employee.change_employeeworkinformationc                    t         j                  j                  |      }|j                  j                  |j
                  k(  s|j                  j                  d      rrt        ||j                  d      }|j                         r%|j                          t        |j                        S t        |j                  t        j                        S t        ddid      S )	Nr`   r   Tr   r   r   r   rH   )r   r=   r>   r3   r   r   r4   r/   rS   rv   rw   r   ry   r	   rz   r   s        r7   r   z"EmployeeWorkInformationAPIView.putE  s    +337727>	LL%%)G)GG||$$%NO:dJ ""$!
00J--f6Q6QRRO4SAAr9   z'employee.delete_employeeworkinformationdispatchnamec                     	 t         j                  j                  |      }|j                          t        t        j                        S # t         j                  $ r t        w xY w)Nr`   r   )	r   r=   r>   r?   r   rI   r   r	   r   )rU   r5   rA   r   s       r7   rI   z%EmployeeWorkInformationAPIView.deleteU  s[    	/77;;r;BI 	v99:: '33 	M	s    A A&N)rX   rY   rZ   r[   r   r   r>   r"   r|   r   r   r   rI   r\   r9   r7   r   r     sv    " **B !!GHO IO !!JKB LB EFZ;;r9   r   c                   2    e Zd ZdZegZ ed      d        Zy)EmployeeWorkInfoExportViewz
    Endpoint for exporting employee work information.

    Methods:
        get(request):
            - Exports work information data based on user permissions.
    r   c                     t        |      S r2   )r   rU   r5   s     r7   r>   zEmployeeWorkInfoExportView.getl      ((r9   NrX   rY   rZ   r[   r   r   r"   r>   r\   r9   r7   r   r   a  +     ** !GH) I)r9   r   c                   2    e Zd ZdZegZ ed      d        Zy)EmployeeWorkInfoImportViewz
    Endpoint for importing employee work information.

    Methods:
        get(request):
            - Handles the importing of work information data based on user permissions.
    r   c                     t        |      S r2   )r   r   s     r7   r>   zEmployeeWorkInfoImportView.get|  r   r9   Nr   r\   r9   r7   r   r   q  r   r9   r   c                   B    e Zd ZdZegZ e ed      d      d        Zy)EmployeeBulkUpdateViewa  
        Endpoint for bulk updating employee and work information.

        Permissions:
            - Requires authentication and "change_employee" permission.
    0
        Methods:
            put(request):
                - Updates multiple employees and their work information.
    r~   r   r   c                 L   |j                   j                  dg       }t        j                  j	                  |      }t
        j                  j	                  |      }|j                   j                  di       }|j                   j                  di       }g d}|D ]&  }|j                  |d        |j                  |d        ( 	  |j                  di |  |j                  di | t        d
did	      S # t        $ r"}	t        dt        |	      id	      cY d }	~	S d }	~	ww xY w)Nidsid__inemployee_id__inemployee_dataemployee_work_info)badge_idemployee_first_nameemployee_last_name	is_activeemailphone%employee_bank_details__account_numberrG   rH   r   r	   successrF   r\   )rS   r>   r   r=   r   r   popupdaterJ   r   rK   )
rU   r5   employee_ids	employeesr   r   work_info_datafields_to_removefieldrL   s
             r7   r   zEmployeeBulkUpdateView.put  s!   ||''r2$$++<+@	4<<CC% D 
  (("= ))*>C
 & 	,EeT*ud+	,	;I-}-%%%77 9-c::  	;Wc!f-c::	;s   $C8 8	D#DD#D#N)	rX   rY   rZ   r[   r   r   r   r   r   r\   r9   r7   r   r     s3    	 **)*DEJW; X;r9   r   c                   0    e Zd ZeZegZddZd Zd Z	d Z
y)ActiontypeViewNc                 h   |rIt        t        |      }|t        ddid      S | j                  |      }t        |j                  d      S t        j
                  j                         }t               }|j                  ||      }| j                  |d      }|j                  |j                        S )NrG   Actiontype not found  r   rF   TrQ   )
rC   r   r   serializer_classrS   r=   rT   r
   rj   rk   )rU   r5   rA   action_typerW   action_types	paginaterrr   s           r7   r>   zActiontypeView.get  s    &z26K"*@ A#NN..{;JJOOC88!))--/(*	**<A**4d*;
//
@@r9   c                    t        |d      du rt        ddid      S | j                  |j                        }|j	                         r'|j                          t        |j                  d      S t        |j                  d	      S )
Nzemployee.add_actiontypeFrG   r     r   ru      rH   )r8   r   r   rS   rv   rw   ry   r{   s      r7   r|   zActiontypeView.post  su    G%>?5HWo6sCC***=
 OOJOOC88
))#66r9   c                 T   t        |d      du rt        ddid      S t        t        |      }|t        ddid      S | j	                  ||j
                  	      }|j                         r'|j                          t        |j
                  d
      S t        |j                  d      S )Nzemployee.change_actiontypeFrG   r   r   r   r   r   ru   rF   rH   )	r8   r   rC   r   r   rS   rv   rw   ry   )rU   r5   rA   r   rW   s        r7   r   zActiontypeView.put  s    G%ABeKWo6sCC":r2W&<=cJJ**;W\\*J
 OOJOOC88
))#66r9   c                     t        |d      du rt        ddid      S t        t        |      }|t        ddid      S t	        t        |      \  }}t        ||      S )	Nzemployee.delete_actiontypeFrG   r   r   r   r   r   )r8   r   rC   r   rM   )rU   r5   rA   r   responsestatus_codes         r7   rI   zActiontypeView.delete  sg    G%ABeKWo6sCC":r2W&<=cJJ -j" =+55r9   r2   )rX   rY   rZ   r&   r   r   r   r>   r|   r   rI   r\   r9   r7   r   r     s&    +)*A7
76r9   r   c                   :    e Zd ZdZeZegZd ZddZ	d Z
d Zd Zy)	DisciplinaryActionAPIViewa  
    Endpoint for managing disciplinary actions.

    Permissions:
        - Requires authentication.

    Methods:
        get(request, pk=None):
            - Retrieves a specific disciplinary action by `pk` or lists all disciplinary actions with optional filtering.

        post(request):
            - Creates a new disciplinary action.

        put(request, pk):
            - Updates an existing disciplinary action by `pk`.

        delete(request, pk):
            - Deletes a specific disciplinary action by `pk`.
    c                 ~    	 t         j                  j                  |      S # t         j                  $ r t        w xY wNr`   )r   r=   r>   r?   r   rU   rA   s     r7   
get_objectz$DisciplinaryActionAPIView.get_object  s;    	%--11R188!.. 	M	   " <Nc                 2   |r|j                   j                  }| j                  |      }|j                         |j                  j                         z  rdnd}||j                  k(  s|s|j                   j                  d      r"t        |      }t        |j                  d      S t        ddid      S |j                   j                  }t        j                  j                  |	      j                         }|j                         }|j                   j                  d      rt        j                  j                         }nh|rFt        j                  j                  |
      }	t        j                  j                  |      }
|	|
z  }n t        j                  j                  |      }t               }|}| j!                  |j"                  |      j$                  }|j'                  ||      }t        |d      }|j)                  |j                        S )NTFz employee.view_disciplinaryactionrF   r   rG   r   rH   r   r   )r   rb   rQ   )r3   r   r   get_subordinate_employeesr   rT   r4   r'   r   rS   r   r=   r   existsr   r
   rf   rg   rh   rj   rk   )rU   r5   rA   rl   disciplinary_action
is_managerrW   subordinatesrc   queryset_subordinatesqueryset_employeerm   disciplinary_actions#disciplinary_action_filter_querysetrr   s                  r7   r>   zDisciplinaryActionAPIView.get  s   ||00H"&//""5 557%115578  	  0<<<<<(()KL9:MN

<<Wo6sCC||00H088??%- @ fh  $==?L||$$%GH-5599;(:(B(B(I(I$0 )J )% %7$>$>$E$E ( %F %! 13DD-55<<<R,.I#+ 262F2F&: 3G 3b 0 ..3WD 6dFJ33JOODDr9   c                 <   t        |d      du rt        ddid      S t        |j                        }|j	                         r5|j                          t        |j                  t        j                        S t        |j                  t        j                        S Nemployee.add_disciplinaryactionFrG   r   r   r   ru   )
r8   r   r'   rS   rv   rw   r	   rx   ry   rz   r{   s      r7   r|   zDisciplinaryActionAPIView.post,  sx    G%FG5PWo6sCC1w||D
 OOJOOF4K4KLL
))&2M2MNNr9   c                 @   t        |d      du rt        ddid      S | j                  |      }t        ||j                        }|j                         r%|j                          t        |j                        S t        |j                  t        j                        S r   )
r8   r   r   r'   rS   rv   rw   ry   r	   rz   )rU   r5   rA   r   rW   s        r7   r   zDisciplinaryActionAPIView.put5  s    G%FG5PWo6sCC"oob11gll

  OOJOO,,
))&2M2MNNr9   c                     t        |d      du rt        ddid      S | j                  |      }|j                          t        t        j
                        S )Nr   FrG   r   r   r   )r8   r   r   rI   r	   r   )rU   r5   rA   r   s       r7   rI   z DisciplinaryActionAPIView.deleteA  sP    G%FG5PWo6sCC"oob1""$v99::r9   r2   )rX   rY   rZ   r[   r   rf   r   r   r   r>   r|   r   rI   r\   r9   r7   r   r     s4    ( /O)*/EbO
O;r9   r   c                   6    e Zd ZdZegZd ZddZd Zd Z	d Z
y)	PolicyAPIViewa  
    Endpoint for managing policies.

    Permissions:
        - Requires authentication.

    Methods:
        get(request, pk=None):
            - Retrieves a specific policy by `pk` or lists all policies with optional search functionality.

        post(request):
            - Creates a new policy.

        put(request, pk):
            - Updates an existing policy by `pk`.

        delete(request, pk):
            - Deletes a specific policy by `pk`.
    c                 ~    	 t         j                  j                  |      S # t         j                  $ r t        w xY wr   )r   r=   r>   r?   r   r   s     r7   r   zPolicyAPIView.get_object`  s9    	>>%%%,,"" 	M	r   Nc                    |r1| j                  |      }t        |      }t        |j                        S |j                  j                  dd       }|r!t        j                  j                  |      }nt        j                  j                         }t        |d      }t               }|j                  ||      }t        |d      }|j                  |j                        S )Nr   )title__icontainsTrQ   )r   r0   r   rS   rg   r>   r   r=   r   rT   r
   rj   rk   )	rU   r5   rA   policyrW   r   policiesrm   rr   s	            r7   r>   zPolicyAPIView.getf  s    __R(F)&1JJOO,,[[__Xt4F!>>00&0I!>>--/)(>J,.I..xAD)$T:J33JOODDr9   c                    t        |d      du rt        ddid      S t        |j                        }|j	                         r'|j                          t        |j                  d      S t        |j                  d	      S )
Nzemployee.add_policyFrG   r   r   r   ru   r   rH   )r8   r   r0   rS   rv   rw   ry   r{   s      r7   r|   zPolicyAPIView.postw  sl    G%:;uDWo6sCC%7<<8
 OOJOOC88
))#66r9   c                 $   t        |d      du rt        ddid      S | j                  |      }t        ||j                        }|j                         r%|j                          t        |j                        S t        |j                  d      S )	Nzemployee.change_policyFrG   r   r   r   ru   rH   )r8   r   r   r0   rS   rv   rw   ry   )rU   r5   rA   r   rW   s        r7   r   zPolicyAPIView.put  sz    G%=>%GWo6sCC$%f7<<@
 OOJOO,,
))#66r9   c                     t        |d      du rt        ddid      S | j                  |      }|j                          t        d      S )Nzemployee.delete_policyFrG   r   r   r      )r8   r   r   rI   )rU   r5   rA   r   s       r7   rI   zPolicyAPIView.delete  sF    G%=>%GWo6sCC$s##r9   r2   )rX   rY   rZ   r[   r   r   r   r>   r|   r   rI   r\   r9   r7   r   r   I  s,    ( **E"77$r9   r   c                       e Zd ZdZegZd ZddZ ed      d        Z	 ed      d        Z
 e ed	            d
        Zy)DocumentRequestAPIViewaT  
    Endpoint for managing document requests.

    Permissions:
        - Requires authentication.
        - Specific actions require manager-level permissions.

    Methods:
        get(request, pk=None):
            - Retrieves a specific document request by `pk` or lists all document requests with pagination.

        post(request):
            - Creates a new document request and notifies relevant employees.

        put(request, pk):
            - Updates an existing document request by `pk`.

        delete(request, pk):
            - Deletes a specific document request by `pk`.
    c                 ~    	 t         j                  j                  |      S # t         j                  $ r t        w xY wr   )r   r=   r>   r?   r   r   s     r7   r   z!DocumentRequestAPIView.get_object  s;    	"**..".55++ 	M	r   Nc                 ,   |r1| j                  |      }t        |      }t        |j                        S t        j
                  j                         }t               }|j                  ||      }t        |d      }|j                  |j                        S )NTrQ   )
r   r(   r   rS   r   r=   rT   r
   rj   rk   )rU   r5   rA   document_requestrW   document_requests
paginationrr   s           r7   r>   zDocumentRequestAPIView.get  s    #r223CDJJOO,, / 7 7 ; ; =-/J//0A7KD24dCJ44Z__EEr9   z&horilla_documents.add_documentrequestsc                    t        |j                        }|j                         r|j                         }	 |j                  j                         D cg c]  }|j                   }}t        j                  |j                  j                  ||j                  j                   dd|j                  j                   d|j                  j                   d|j                  j                   d|j                  j                   ddd	d
|j                   
       t        |j                  t        j                        S t        |j                  t        j                         S c c}w #  Y SxY w)Nru   z requested a document.u   طلب u    مستنداً.z hat ein Dokument angefordert.u    solicitó un documento.u    a demandé un document.z/employee/employee-profilechatbox-ellipsesz/api/employee/document-request/		recipientverbverb_arverb_deverb_esverb_frredirecticonapi_redirectr   )r(   rS   rv   rw   r   rT   employee_user_idr   sendr3   r   r<   r   r	   rx   ry   rz   )rU   r5   rW   rB   r3   r   s         r7   r|   zDocumentRequestAPIView.post  s5   .GLLA
 //#C?B?R?R?TUtT22U	ULL--'#LL5566LM%gll&?&?%@@PQ&||8899WX&||8899QR&||8899QR9+#B366(!K JOOF4K4KLL
))&2M2MNN# Vs   E- E((B6E- (E- -E1z)horilla_documents.change_documentrequestsc                    | j                  |      }t        ||j                        }|j                         r%|j	                          t        |j                        S t        |j                  t        j                        S rt   )	r   r(   rS   rv   rw   r   ry   r	   rz   )rU   r5   rA   r	  rW   s        r7   r   zDocumentRequestAPIView.put  s`    ??2../?gllS
 OOJOO,,
))&2M2MNNr9   r   c                 x    | j                  |      }|j                          t        t        j                        S Nr   r   rI   r   r	   r   )rU   r5   rA   r	  s       r7   rI   zDocumentRequestAPIView.delete  s.    ??2.!v99::r9   r2   )rX   rY   rZ   r[   r   r   r   r>   r"   r|   r   r   r   rI   r\   r9   r7   r  r    sx    * **
F !!IJO KO0 !!LMO NO )*DEF; G;r9   r  c                       e Zd ZeZegZd Zd
dZ e	e
d      d        Z e ede            d        Z e ede            d	        Zy)DocumentAPIViewc                 ~    	 t         j                  j                  |      S # t         j                  $ r t        w xY wr   )r   r=   r>   r?   r   r   s     r7   r   zDocumentAPIView.get_object  s;    	##''2'..$$ 	M	r   Nc                 z   |r1| j                  |      }t        |      }t        |j                        S t        j
                  j                         }| j                  |j                  |      j                  }t               }|j                  ||      }t        |d      }|j                  |j                        S )Nrb   TrQ   )r   r)   r   rS   r   r=   rT   rf   rg   rh   r
   rj   rk   )	rU   r5   rA   documentrW   	documentsdocument_requests_filteredrm   rr   s	            r7   r>   zDocumentAPIView.get  s    r*H+H5JJOO,, ((,,.I)-)=)=i *> *b ' -.I../I7SD+Dt<J33JOODDr9   horilla_documents.add_documentc                    t        |j                        }|j                         r+|j                          	 t	        j
                  |j                  j                  |j                  j                  j                         j                  |j                  j                   dd|j                  j                   d|j                  j                   d|j                  j                   d|j                  j                   dd|j                  j                  j                   d	d
d
       t        |j                  t        j                        S t        |j                  t        j                        S #  Y NxY w)Nru   z uploaded a documentu   قام u    بتحميل مستندz hat ein Dokument hochgeladenu    subió un documentou    a téléchargé un documentz/employee/employee-view//r  z/api/employee/documents/r  r   )r)   rS   rv   rw   r   r  r3   r   r   r  r<   r   r	   rx   ry   rz   r{   s      r7   r|   zDocumentAPIView.post  s2    (W\\:
 OOLL--%ll77MMO``#LL5566JK%gll&?&?%@@XY&||8899VW&||8899MN&||8899UV78Q8Q8T8T7UUVW+#; JOOF4K4KLL
))&2M2MNNs   C5E8 8E<z!horilla_documents.change_documentc                    | j                  |      }t        ||j                        }|j                         r%|j	                          t        |j                        S t        |j                  t        j                        S rt   )	r   r)   rS   rv   rw   r   ry   r	   rz   )rU   r5   rA   r"  rW   s        r7   r   zDocumentAPIView.put  s^    ??2&'w||D
 OOJOO,,
))&2M2MNNr9   z!horilla_documents.delete_documentc                 x    | j                  |      }|j                          t        t        j                        S r  r  )rU   r5   rA   r"  s       r7   rI   zDocumentAPIView.delete!  s+    ??2&v99::r9   r2   )rX   rY   rZ   r   rf   r   r   r   r>   r!   r   r|   r   r   r   r   rI   r\   r9   r7   r  r    s    +O)*E *9OO, o&I8TUO VO o&I8TU; V;r9   r  c                   .    e Zd ZegZ ed      d        Zy) DocumentRequestApproveRejectViewr%  c                     t         j                  j                  |      j                         }||_        |j                          t        ddid      S )Nr;   r	   r   rF   r   )r   r=   r   firstr	   rw   r   )rU   r5   r<   r	   r"  s        r7   r|   z%DocumentRequestApproveRejectView.post+  sF    ##**b*1779 9-c::r9   N)rX   rY   rZ   r   r   r"   r|   r\   r9   r7   r+  r+  (  s$    )* !AB; C;r9   r+  c                   .    e Zd ZegZ ed      d        Zy) DocumentBulkApproveRejectAPIViewr%  c                    |j                   j                  dd       }|j                   j                  dd       }d}|rt        j                  j	                  |      }g }|D ]d  }|j
                  s!d}|j                  |j                  dd       0|j                  |j                  dd	       ||_        |j                          f t        |
      S )Nr   r	   rF   r   rH   zNo documents)r<   rG   r   )r<   r	   r   )rS   r>   r   r=   r   r"  appendr<   r	   rw   r   )rU   r5   r   r	   r   r#  r   r"  s           r7   r   z$DocumentBulkApproveRejectAPIView.put6  s    llud+!!(D1 ((//s/;IH%  (("%KOO8;;$PQx{{i HI"(  55r9   N)rX   rY   rZ   r   r   r"   r   r\   r9   r7   r/  r/  3  s$    )* !AB6 C6r9   r/  c                   :    e Zd ZegZ e ed            d        Zy)EmployeeBulkArchiveViewr   c                 R   |j                   j                  d      }g }|D ]y  }t        j                  j                  |      }||_        ||j
                  _        |j                         du r|j                          |j                  t        |      dd       { t        |d      S )Nr   r;   Fz'Related model found for this employee. rl   rG   rF   r   )rS   r>   r   r=   r   r  get_archive_conditionrw   r1  rK   r   )rU   r5   r   r   rG   r   rl   s          r7   r|   zEmployeeBulkArchiveView.postM  s    llu% 	K''++{+;H!*H2;H%%/--/58LL #HF	 c**r9   NrX   rY   rZ   r   r   r   r   r|   r\   r9   r7   r3  r3  J  s*    )*)*DEF+ G+r9   r3  c                   :    e Zd ZegZ e ed            d        Zy)EmployeeArchiveViewr   c                    t         j                  j                  |      }||_        ||j                  _        d }|j                         du r|j                          nt        |      |j                         d}t        |d      S )Nr;   Fr5  rF   r   )	r   r=   r>   r   r  r6  rw   rK   r   )rU   r5   r<   r   rl   r   s         r7   r|   zEmployeeArchiveView.postc  s{    ##''2'.&.7!!+))+u4MMO  M!779H --r9   Nr7  r\   r9   r7   r9  r9  `  s*    )*)*DEF. G.r9   r9  c                       e Zd ZegZd Zy)EmployeeSelectorViewc                 \   |j                   j                  }t        j                  j	                  |j                         }t
        j                  j	                  |      j                         }|r@t        j                  j	                  t        |j                        t        |      z        }|j                   j                  d      rt        j                  j                         }t               }|j                  ||      }t        |d      }|j                  |j                        S )N)r  r   r`   (employee_work_info__reporting_manager_idzemployee.view_employeeTrQ   )r3   r   r   r=   r   r   r   r   rA   r4   rT   r
   rj   r,   rk   rS   )rU   r5   rl   r   r   rm   rr   rW   s           r7   r>   zEmployeeSelectorView.getv  s    <<,,$$++W\\+J	,44;;!) < 

&( 	  ((//X[[!Ax$XXI <<  !9: ((,,.I(*	**9g>/4@
//
@@r9   NrX   rY   rZ   r   r   r>   r\   r9   r7   r<  r<  s  s    )*Ar9   r<  c                       e Zd ZegZd Zy)ReportingManagerCheckc                     t         j                  j                  |j                  j                        rt        d      S t        d      S )Nr>  rF   r   r   )r   r=   r   r3   r   r   r   s     r7   r>   zReportingManagerCheck.get  s@    ""5<\\5N5N # 
 3''s##r9   Nr@  r\   r9   r7   rB  rB    s    )*$r9   rB  N)Wdjango.db.modelsr   r   django.httpr   django.utils.decoratorsr   django_filters.rest_frameworkr   rest_frameworkr	   rest_framework.paginationr
   rest_framework.permissionsr   rest_framework.responser   rest_framework.viewsr   employee.filtersr   r   r   employee.modelsr   r   r   r   r   r   r   employee.viewsr   r   horilla.decoratorsr   *horilla_api.api_decorators.base.decoratorsr   (horilla_api.api_methods.employee.methodsr   horilla_documents.modelsr   r   notifications.signalsr   api_decorators.base.decoratorsr!   r"   "api_decorators.employee.decoratorsr#   api_methods.base.methodsr$   r%   $api_serializers.employee.serializersr&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r8   rC   rM   rO   r^   r   r   r   r   r   r   r   r   r   r  r  r+  r/  r3  r9  r<  rB  r\   r9   r7   <module>rY     sx   .  4 = ! : 6 , ( 
   > . J F > ( ? S   '&5' 5$P;g P;fA' A8R; R;j?;W ?;D) ) ) ) );W );X-6W -6`i; i;XG$G G$TP;W P;f?;g ?;D;w ;6w 6.+g +,.' .&A7 A0$G $r9   