
    si<                     6   d Z 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	 ddl
mZmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZ ddlmZ ddlmZmZ ddl m!Z!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+m,Z, ddl-m.Z. e+d        Z/e+e* e,d      d                      Z0e+e*d               Z1e+e*d               Z2e+ e,d      d               Z3e+ e,d      d               Z4e+ e,d      d               Z5e+d        Z6e+d        Z7d Z8d  Z9d! Z8d" Z9d# Z:e+e* e,d$      d%                      Z;e+e* e,d&      d'                      Z<e+e* e,d&      d(                      Z=e+e* e,d)      d*                      Z>e+d+        Z?e+d,        Z@e+e*d-               ZAe+d.        ZBy)/zI
policies.py

This module is used to write operation related to policies
    N)	timedelta)parse_qs)messages)User)HttpResponseJsonResponse)get_object_or_404redirectrender)reverse)gettext_lazy)closest_numberseval_validatefiltersubordinatesget_key_instancespaginator_qry)r   )DisciplinaryActionFilterPolicyFilter)DisciplinaryActionForm
PolicyForm)
ActiontypeDisciplinaryActionEmployeePolicyPolicyMultipleFile)hx_request_requiredlogin_requiredpermission_required)notifyc                     t         j                  j                         }| j                  j	                  d      s|j                  d      }t        | ddt        || j                  j                  d            i      S )zG
    Method is used render template to view all the policy records
    employee.view_policyTis_visible_to_allzpolicies/view_policies.htmlpoliciespage)
r   objectsalluserhas_permfilterr   r   GETgetrequestr$   s     5/var/www/python-projects/worksol/employee/policies.pyview_policiesr0   (   sh    
 ~~!!#H<<  !78??T?:%	]8W[[__V-DEF     zemployee.add_policyc                    | j                   j                  d      }d}t        t        t	        |            t
              r.t        j                  j                  |      j                         }t        |      }| j                  dk(  rbt        | j                  | j                  |      }|j                         r0|j                          t!        j"                  | d       t               }t%        | dd|i      S )	z4
    Method is used to create/update new policy
    instance_idNidinstancePOSTzPolicy savedzpolicies/form.htmlform)r+   r,   
isinstancer   strintr   r&   r*   firstr   methodr8   FILESis_validsaver   successr   )r.   r3   r7   r9   s       r/   create_policyrC   7   s     ++//-0KH-K 0137>>((K(8>>@x(D~~',,I==?IIKWn5<D'/&$@@r1   c           
      0   t        | j                        j                  }| j                  j	                  d      s|j                  d      }t        | dt        || j                  j                  d            | j                  j                         d      S )z3
    This method is used to search in policies
    r!   Tr"   zpolicies/records.htmlr%   )r$   pd)
r   r+   qsr(   r)   r*   r   r   r,   	urlencoder-   s     r/   search_policiesrH   M   sz     GKK(++H<<  !78??T?:%h0GH++'')	
 r1   c                     | j                   d   }t        j                  j                  |      j	                         }t        | dd|i      S )z0
    This method is used to view the policy
    r3   r4   zpolicies/view_policy.htmlpolicy)r+   r   r&   r*   r=   r   )r.   r3   rJ   s      r/   view_policyrK   `   sO     ++m,K^^""k"288:F#f	
 r1   zemployee.delete_policyc                    	 | j                   j                  d      }t        j                  j	                  |      j                         \  }}|dk(  r t        j                  | t        d             nt        j                  | d       t        t              S # t        $ r0 t        j                  | t        d             Y t        t              S w xY w)z)
    This method is to delete policy
    idsid__inr   zPolicies Not FoundzPolicies deleted)r+   getlistr   r&   r*   deleter   error_rB   
ValueErrorr
   r0   )r.   rM   countdicts       r/   delete_policiesrW   q   s    9kk!!%(nn++3+7>>@tA:NN7A&:$;<W&89 M""  9w"6 78M""9s   BB (CCzemployee.add_policymultiplefilec                    | j                   j                  d      }| j                  d   }g }|D ]4  }t               }||_        |j                          |j                  |       6 t        j                  j                  |      } |j                  j                  |  t        j                  | d       t        | dd|i      S )z9
    This method is used to add attachment to policy
    files	policy_idr4   zAttachments addedpolicies/attachments.htmlrJ   )r?   rP   r+   r   
attachmentrA   appendr   r&   r,   attachmentsaddr   rB   r   )r.   rY   rZ   r^   filer\   rJ   s          r/   add_attachmentra      s     MM!!'*EK(IK '')
 $
:&	'
 ^^9-FFK(W12'668JKKr1   z"employee.delete_policymultiplefilec                    | j                   j                  d      }| j                   d   }t        j                  j	                  |      }t
        j                  j                  |      j                          t        | dd|i      S )z7
    This method is used to remove the attachments
    rM   rZ   r4   rN   r[   rJ   )	r+   rP   r   r&   r,   r   r*   rQ   r   )r.   rM   rZ   rJ   s       r/   remove_attachmentrc      sr     ++

e
$CK(I^^9-F%%S%188:'668JKKr1   c                 ~    | j                   d   }t        j                  j                  |      }t	        | dd|i      S )zK
    This method is used to view all the attachments inside the policy
    rZ   r4   r[   rJ   )r+   r   r&   r,   r   )r.   rJ   s     r/   get_attachmentsre      s>    
 [[%F^^6*F'668JKKr1   c                    t         j                  j                  | j                        j	                         }| j                  j                  d      rt        j                  j                         }nht        | t        j                  j                         d      j                         }|t        j                  j                  |      j                         z  }t        | j                  |      }| j                  j                  d      }t        |j                  |      }| j                  j                         }t!        | d|||d      S )	z<
    This method is used to view all Disciplinaryaction
    )employee_user_idz employee.view_disciplinaryactionzbase.add_disciplinaryaction)employee_id)querysetr%   z*disciplinary_actions/disciplinary_nav.html)datarE   f)r   r&   r*   r(   r=   r)   r   r'   r   distinctr   r+   r,   r   rF   rG   r   )r.   employeedis_actionsr9   page_numberpage_objprevious_datas          r/   disciplinary_actionsrr      s   
 &&&EKKMH||?@(00446('//3357T

(* 	  ((//H/ENNPQ 	
 $GKK+FD++//&)KTWWk2HKK))+M4	
 r1   c                 `    t         j                  j                  | d         }|j                  S ]
    This function is used to get the action type by the selection of title in the form.
    actiontitler   r&   r,   action_type	action_idrv   s     r/   get_action_typer}      .     ##)H*=#>Fr1   c                 Z    t         j                  j                  |       }|j                  S ru   rw   ry   r{   s     r/   get_action_type_deleter      )     ##)#4Fr1   c                 `    t         j                  j                  | d         }|j                  S rt   ry   r{   s     r/   r}   r}      r~   r1   c                 Z    t         j                  j                  |       }|j                  S r   ry   r{   s     r/   r   r      r   r1   c                     t        t        |       }|st        t              S t        t        |j
                  j                        }|st        t              S ||_        |j                          t        d      S )Nr4   )<script>window.location.reload()</script>)
r	   r   r
   rr   r   rg   r5   	is_activerA   r   )emp_idresultrm   r(   s       r/   employee_account_block_unblockr      s`     f5H,--Th&?&?&B&BCD,--DNIIKCDDr1   zemployee.add_disciplinaryactionc                    t               }g }| j                  j                  d      dk7  r| j                  j                  d      nd}| j                  rt        | j                        }| j                  dk(  rt        | j                  | j
                        }|j                         r|j                  d   }|D ]  }|j                  }|j                  |       ! |j                          t        j                  | t        d             t        j                  | j                   j"                  |ddd	d
ddd	       t$        j&                  j)                         }t+        |      dk(  rt-        d      S t/        | d||d      S )z5
    Method is used to create Disciplinaryaction
    dynamicNoneNr8   rh   zDisciplinary action taken.$Disciplinary action is taken on you.2   تم اتخاذ إجراء disziplinarisch ضدك.7   Disziplinarische Maßnahmen wurden gegen Sie ergriffen.0   Se ha tomado acción disciplinaria en tu contra.>   Des mesures disciplinaires ont été prises à votre encontre./employee/disciplinary-actions/chatbox-ellipses	recipientverbverb_arverb_deverb_esverb_frr
   icon   r   zdisciplinary_actions/form.html)r9   r   )r   r+   r,   r>   r8   r?   r@   cleaned_datarg   r]   rA   r   rB   rS   r   sendr(   employee_getr   r&   r'   lenr   r   )r.   r9   	employeesr   employee_idsrm   r(   diss           r/   create_actionsr      sN    "#DI&-kkooi&@F&J	"PT  {{%gkk2~~%gllGMMB==?,,];L( '00  &' IIKWa(D&EFKK))#;LQJX:'
 !((,,.s8q= KLL1DW3U r1   z"employee.change_disciplinaryactionc                 &   t         j                  j                  |      }t        |      }g }| j                  dk(  rt        | j
                  | j                  |      }|j                         r|j                  d   }|D ]  }|j                  }|j                  |       ! |j                          t        j                  | t        d             t        j                   | j"                  j$                  |dddd	d
dd	       t'        | dd|i      S )z5
    Method is used to update Disciplinaryaction
    r4   r6   r8   rh   zDisciplinary action updated.r   r   r   r   r   r   r   r   z%disciplinary_actions/update_form.htmlr9   )r   r&   r,   r   r>   r8   r?   r@   r   rg   r]   rA   r   rB   rS   r   r   r(   r   r   )r.   r|   rv   r9   r   r   rm   names           r/   update_actionsr   *  s      ''++y+9F!62DI~~%gllGMMFS==?,,];L( '00  &' IIKWa(F&GHKK))#;LQJX:'
 'BVTNSSr1   c                    t         j                  j                  |      }t        j                  j                  |      }t	        |j
                        }|dk(  s|dk(  ryt        t        |      }t        t        |j                  j                        }|j                  rn6t        j                  | t        d             d|_
        |j                          |j                  j!                  |       t#        |j                  j%                               }|dk(  r|j'                          t        j(                  | t        d             t+        d|j                         S )	Nr4   	dismissal
suspension.Employees login credentials will be unblocked.Tr   z7Employee removed from disciplinary action successfully.z-/employee/disciplinary-actions-list?click_id=)r   r&   r,   r   r   rv   r	   r   rg   r5   r   r   warningrS   rA   rh   remover   r'   rQ   rB   r
   )	r.   r|   r   
dis_actionrm   rz   empr(   r   s	            r/   #remove_employee_disciplinary_actionr   P  s     $++//9/=J##v#.H():):;Kk![L%@V4 #*>*>*A*AB>>KL "DNIIK!!(+J**..01IA~LM CJMM?STTr1   z"employee.delete_disciplinaryactionc           	      r   | j                   j                         }|j                  dd       |j                         }t        j
                  j                  |      }t        |j                        }|j                  j                         D ]  }|dk(  s|dk(  st        t        |j                        }t        t        |j                  j                        }|j                   r[t#        j$                  | t'        d             d|_        |j)                           |j+                          t#        j,                  | t'        d             t        j
                  j                         }	| j.                  j                  d	      }
|
r|
d
k(  r|| j                   j                  d      }t1        j2                  |      }||v r3|j5                  |       t7        t1        j2                  |      |      \  }}t9        d d| d| d      S |	j;                         rt9        t=        d            S t?        d      S )z;
    This method is used to delete Disciplinary action
    instances_idsNr4   r   r   r   TzDisciplinary action deleted.HTTP_HX_TARGETgenericModalBodyz+/employee/disciplinary-actions-detail-view/z/?z&instance_ids=z&deleted=truezdisciplinary-actions-listr   ) r+   copypoprG   r   r&   r,   r   rv   rh   r'   r	   r   r5   r   rg   r   r   r   rS   rA   rQ   rB   METAjsonloadsr   r   r
   existsr   r   )r.   r|   request_copyrq   r   rz   dis_emprm   r(   rn   	hx_targetr   instances_listprevious_instancenext_instances                  r/   delete_actionsr   r  s    ;;##%L_d+ **,M

$
$
(
(I
(
6C(4K??&&( +%)D(gjjAH$Th.G.G.J.JKD~~  QOP "&		 JJLWa >?@$,,002K  !12IY"448M2&!!),/>

=)90,} 9-=/Yghvgw  xE  F
 	
  ;<==CDDr1   c                     | j                   d   }t        j                  j                  |      }|j                  }t        d|i      S )z[
    This method is used to get the action type by the selection of title in the form.
    rz   r4   )r8   r   r&   r,   rz   r   )r.   r|   rv   rz   s       r/   action_type_detailsr     sF    
 ]+I##y#1F$$K455r1   c                 :    | j                   d   }t        d|i      S )z_
    This method is used to get the action type name by the selection of type in the form.
    rz   )r8   r   )r.   rz   s     r/   action_type_namer     s"    
 ,,}-K455r1   c           
         | j                   j                         }| j                   j                  d      r| j                   j                  d      nd}t        | j                         j                  }| j                   j                  d      }t        ||      }t        |      }t        t        |       t        | d|||| j                   j                  d      |d      S )z<
    This method is used to filter Disciplinary Action.
    click_idNr%   .disciplinary_actions/disciplinary_records.html	dashboard)rj   rE   filter_dictr   r|   )
r+   rG   r,   r   rF   r   r   r   r   r   )r.   rq   r|   
dis_filterro   rp   	data_dicts          r/   disciplinary_filter_viewr     s     KK))+M/6{{z/J
+PTI)'++699J++//&)KZ5H'I()48$ 5"	

 
r1   c           
          t        | j                        j                  }t        | dt	        || j                  j                  d            | j                  j                         d      S )z?
    This method is used to search in Disciplinary Actions
    r   r%   )rj   rE   )r   r+   rF   r   r   r,   rG   )r.   disciplinarys     r/   search_disciplinaryr     sW    
 ,GKK8;;L8!,0GH++'')	
 r1   )C__doc__datetimer   r   urllib.parser   django.contribr   django.contrib.auth.modelsr   django.httpr   r   django.shortcutsr	   r
   r   django.urlsr   django.utils.translationr   rS   base.methodsr   r   r   r   r   
base.viewsemployee.filtersr   r   employee.formsr   r   employee.modelsr   r   r   r   r   horilla.decoratorsr   r   r   notifications.signalsr   r0   rC   rH   rK   rW   ra   rc   re   rr   r}   r   r   r   r   r   r   r   r   r   r    r1   r/   <module>r      s      ! # + 2 @ @  6  % C =  X W (   *+A ,  A&   "    -.# / #  67L 8 L$ 9:L ; L L L  @
E 67( 8  (V 9: T ;   TF 9:U ;  U> 9:-E ;  -E` 6 6 6 6   2  r1   