
    si0                        d 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 ddlmZ dd	lmZ d
dlmZmZ  G d de
      Z G d de
      Z G d de	      Z G d de	      Z G d de	      Z G d de	      Z G d de
      Zy)z
Module containing forms for managing biometric devices and associated data.

This module provides Django forms for creating and managing biometric devices,
employee biometric data, COSEC users, and related configurations.
    )forms)Q)gettext_lazy)Form	ModelForm)reload_queryset)Employee)_thread_locals)default_select_option_template   )BiometricDevicesBiometricEmployeesc                   P     e Zd ZdZddddddddddd
Z G d d      Z fdZ xZS )BiometricDeviceFormas  
    Form for creating and updating biometric device configurations.

    This form is used to create and update biometric device configurations.
    It includes fields for specifying the device name, IP address, TCP communication port,
    and other relevant settings. Additionally, it excludes fields related to scheduler
    settings and device activation status.
       )
namemachine_type
machine_ipportcosec_usernamecosec_passwordanviz_request_idapi_urlapi_key
api_secretc                   z    e Zd ZdZeZdZg dZ ej                  ddd       ej                  dd	d
      dZy)BiometricDeviceForm.Meta6
        Meta class to add additional options
        __all__)is_schedulerscheduler_durationlast_fetch_datelast_fetch_time	is_activemachineTypeInputzmachineTypeChange($(this)))idonchangeattrsz!oh-input oh-input--password w-100passwordclasstype)r   bio_passwordN)__name__
__module____qualname____doc__r   modelfieldsexcluder   Select	TextInputwidgets     3/var/www/python-projects/worksol/biometric/forms.pyMetar   -   sW    	 !
 )ELL, < ,EOO@&
r:   r<   c                     t        |   |i | | j                  d   j                  }t	        |t
        j                        rt        |_        y y )N
company_id)	super__init__r4   widget
isinstancer   r6   r   option_template_name)selfargskwargscompany_widget	__class__s       r;   r@   zBiometricDeviceForm.__init__J   sD    $)&)\299nell32PN/ 4r:   )r/   r0   r1   r2   colsr<   r@   __classcell__rH   s   @r;   r   r      sE     D
 
:Q Qr:   r   c                   ,    e Zd ZdZddiZ G d d      Zy)BiometricDeviceSchedulerForma  
    Form for updating the scheduler duration of a biometric device.

    This form is used to update the scheduler duration of a biometric
    device to fetch attendance data.
    It includes a field for entering the scheduler duration in the format HH:MM.
    r!   r   c                   .    e Zd ZdZeZdgZd ed      iZy)!BiometricDeviceSchedulerForm.Metar   r!   zInterval durationN)	r/   r0   r1   r2   r   r3   r4   _labelsr9   r:   r;   r<   rO   \   s(    	 !&' !$7"8
r:   r<   N)r/   r0   r1   r2   rI   r<   r9   r:   r;   rM   rM   Q   s     !"%D	
 	
r:   rM   c                        e Zd ZdZ ej
                  ej                  j                          ej                          e
d            Z fdZ xZS )EmployeeBiometricAddFormai  
    Form for adding employees to a biometric device.

    This form allows administrators to add employees to a biometric device
    for biometric authentication. It includes a field for selecting employees from
    a queryset and ensures that only active employees not already associated with
    a 'zk' type biometric device are available for selection.
    	EmployeesquerysetrA   labelc                 *   t        |   |i | t        t        d      | _        | j                  j
                  r0| j                  j
                  j                  j                  dd       nd | _        t        j                  | j                        | _        t        j                  j                  | j                        j                  dd      }t         j                  j                  d      j#                  |      | j$                  d	   _        y )
Nrequest	device_idrZ   employee_idTflatr$   id__inemployee_ids)r?   r@   getattrr
   rY   resolver_matchrF   getrZ   r   finddevicer   objectsfiltervalues_listr	   r5   r4   rV   )rD   rE   rF   zk_employee_idsrH   s       r;   r@   z!EmployeeBiometricAddForm.__init__x   s    $)&)~y9 ||** LL''..22;E 	
 '++DNN;,44;;kk < 

+m$+
/ 	 08/?/?/F/F 0G 0

''
) 	N#,r:   r/   r0   r1   r2   r   ModelMultipleChoiceFieldr	   rh   allSelectMultiplerP   rb   r@   rJ   rK   s   @r;   rS   rS   h   sM     2511!!%%'#u##%nL* *r:   rS   c                        e Zd ZdZ ej
                  ej                  j                          ej                          e
d            Zdd fd
Z xZS )CosecUserAddFormaq  
    Form for adding users to a COSEC biometric device.

    This form allows administrators to add multiple users to a COSEC biometric device
    for biometric authentication. It includes a field for selecting users from
    a queryset and ensures that only active users not already associated with
    a 'cosec' type biometric device are available for selection.
    rT   rU   Nr[   c                
   t        |   |i | t        j                  j	                  |      j                  dd      }t        j                  j	                  d      j                  |      | j                  d   _	        y )Nr[   r\   Tr]   r_   r`   rb   )
r?   r@   r   rh   ri   rj   r	   r5   r4   rV   )rD   rZ   rE   rF   cosec_employee_idsrH   s        r;   r@   zCosecUserAddForm.__init__   s    $)&)/77>> ? 

+m$+
/ 	 08/?/?/F/F 0G 0

'+'
, 	N#,r:   rl   rK   s   @r;   rq   rq      sR     2511!!%%'#u##%nL )- - -r:   rq   c                       e Zd ZdZ ej
                   ed       ed       ej                  ddi            Z ej                  dd ej                         	      Z ej                  dd ej                         	      Z ej                  dd ej                         	      Z ej                  d ej                  d
dd            Z ej                  dd ej                         	      Z fdZ xZS )COSECUserForma;  
    Form for adding or updating users in a COSEC biometric device.

    This form allows administrators to add or update users in a COSEC biometric
    device. It includes fields for specifying the user's name, whether the user
    is active, whether the user is a VIP user, whether validity is enabled for
    the user, the validity end date, and whether to bypass finger-based
    authentication for the user. It provides validation to ensure that the
    name does not exceed 15 characters and that the validity end date is
    provided when validity is enabled.
    zEmployee Namez15 characters max.r,   oh-input w-100r(   )rW   	help_textrA   F)initialrequiredrA   datezoh-input w-100 form-control)r-   r,   )ry   rA   c                     t         |          }t        |d         dkD  rt        j                  d      |d   r'|j                  d      t        j                  d      y y )Nr      z@Maximum 15 characters allowed for Name in COSEC Biometric Devicevalidity_enablevalidity_end_datezDWhen the Validity field is enabled, a Validity End Date is required.)r?   cleanlenr   ValidationErrorre   )rD   cleaned_datarH   s     r;   r   zCOSECUserForm.clean   ss    w}|F#$r)''R  )* 34<++Z  = +r:   )r/   r0   r1   r2   r   	CharFieldrP   r7   r   BooleanFieldCheckboxInputuser_activevipr}   	DateField	DateInputr~   by_pass_fingerr   rJ   rK   s   @r;   ru   ru      s   
 5?? ()ug/?%@AD
 %%$$"u""$K
 %

"u""$C
 )e(("u""$O
 (u!,IJ
 (U''"u""$N
 
r:   ru   c            	           e Zd ZdZg dZg dZ ej                  ej                  j                          ej                          ed            Z ej                  dd ed      	      Z ej                  dd ed
      	      Z ej"                  ed ed      d      Z ej"                  ed ed            Z ej                  dd ed      	      Z ej*                   ej,                  ddd              ej*                  d ed       ej,                  ddd            Z ej*                  d ed       ej,                  ddd            Z fdZ fdZ xZS )DahuaUserFormzb
    This form is used to map a Worksol employee to a user entry on a Dahua biometric device.
    ))r   Normal)r   zReported for loss)   Canceled)   Frozen)   	Arrearage)   Overdue)    zBPre-arrearage (The door still can be unlocked with a voice prompt)))r   zOrdinary card)r   zVIP card)r   z
Guest card)   zPatrol card)r   zBlocklist card)   zDuress cardr	   rU   2   TzCard Number)
max_lengthry   rW   User IDFzCard Statusr   )choicesry   rW   rx   z	Card Type)r   ry   rW   Passwordrv   zdatetime-localr+   r(   )rA   zValid Date Start)ry   rW   rA   zValid Date Endc                    t        t        d      | _        | j                  j                  r0| j                  j                  j                  j                  dd       nd | _        t        |    |i | t        | j                         | j                  d   j                  j                  j                  dddddd	       y )
NrY   rZ   employeez#dahuaBiometricUserFormz#id_user_id	outerHTMLchangez!/biometric/find-employee-badge-id)z
hx-includez	hx-targetzhx-swapz
hx-triggerzhx-get)rc   r
   rY   rd   rF   re   rZ   r?   r@   r   r4   rA   r)   update)rD   rE   rF   rH   s      r;   r@   zDahuaUserForm.__init__  s    ~y9 ||** LL''..22;E 	
 	$)&)$J&&,,337*&&=	
r:   c                    t         |          }d }i }|j                  d      }|j                  d      }| j                  rt	        j
                  | j                        }|r?|r=t        j                  j                  ||      j                         rt        d      |d<   |r?|r=t        j                  j                  ||      j                         rt        d      |d<   |rt        j                  |      |S )Ncard_nouser_id)dahua_card_norZ   z This Card Number already exists.r   rZ   zThis User ID already exists.)r?   r   re   rZ   r   rf   r   rh   ri   existsrP   r   r   )rD   r   rg   error_fieldsr   r   rH   s         r;   r   zDahuaUserForm.clean+  s    w}""9-""9->>%**4>>:Fv!))00% 1 fh +,,N*OY'v!))006 1 fh +,,J*KY'''55r:   )r/   r0   r1   r2   CARD_STATUS_CHOICESCARD_TYPE_CHOICESr   ModelChoiceFieldr	   rh   rn   r6   rP   r   r   r   r   ChoiceFieldcard_status	card_typer*   DateTimeFieldDateTimeInputvalid_date_startvalid_date_endr@   r   rJ   rK   s   @r;   r   r      s    &u%%!!%%'u||~
mH
 eood!MBRSGeood!I,OG#%###	K "!!!E;I u"uAjMRHE"u"",6FG

 +u**"#"u"",6FG
 )U(( !"u"",6FG
N
& r:   r   c                   @     e Zd ZdZ G d d      Z fdZ fdZ xZS )MapBioUsersz
    Form for mapping biometric users to Worksol employees.

    This form is used to associate a biometric user (from a biometric device) with
    an employee in the Worksol system.
    c                       e Zd ZdZeZddgZy)MapBioUsers.Metar   r\   r   N)r/   r0   r1   r2   r   r3   r4   r9   r:   r;   r<   r   O  s    	 #+r:   r<   c                 .   t        t        d      | _        | j                  j                  r0| j                  j                  j                  j                  dd       nd | _        t        |    |i | | j                  rt        j                  j                  | j                        j                  dd      }t        j                  j                  t        |      t        d	      z        | j                   d   _        d| j                   d
   _        y )NrY   rZ   r[   r\   Tr]   r`   Fr_   r   )rc   r
   rY   rd   rF   re   rZ   r?   r@   r   rh   ri   rj   r	   r5   r   r4   rV   ry   )rD   rE   rF   already_mapped_employeesrH   s       r;   r@   zMapBioUsers.__init__W  s    ~y9 ||** LL''..22;E 	
 	$)&)>>'9'A'A'H'H.. (I (k-dk3 % 3;2B2B2J2J12Q5GG3DKK&/ +/I'r:   c                 X   t         |          }|j                  d      }| j                  d   j                  xs d}| j
                  r`|r^t        j                  j                  || j
                        j                         r%t        j                  dt        d      d|iz  i      |S )Nr   r   r   z;This biometric %(label)s is already mapped with an employeerW   )r?   r   re   r4   rW   rZ   r   rh   ri   r   r   r   rP   )rD   r   r   user_id_labelrH   s       r;   r   zMapBioUsers.cleanh  s    w}""9-I.44A	>>g!))004>> 1 fh ++!1Y$ #M2$3  r:   )r/   r0   r1   r2   r<   r@   r   rJ   rK   s   @r;   r   r   G  s!    , ,/" r:   r   N)r2   djangor   django.db.modelsr   django.utils.translationr   rP   
base.formsr   r   base.methodsr   employee.modelsr	   horilla.horilla_middlewaresr
   horilla_widgets.formsr   modelsr   r   r   rM   rS   rq   ru   r   r   r9   r:   r;   <module>r      s      6 & ( $ 6 @ 88Q) 8Qv
9 
.*t *B-t -47D 7tgD gT2) 2r:   