
    si!A                        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
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 Z G d de      Z G d dej6                        Z G d dej6                        Zy)zm
This module contains Django models for managing biometric devices
and employee attendance within a company.
    N)ValidationError)MaxValueValidator)models)reversereverse_lazy)gettext_lazy)HorillaCompanyManager)Company)Employee)HorillaModel)render_templatec                    t        |       dkD  rt        t        d            	 | j                  d      \  }}t	        |      }t	        |      }t        t        |            dkD  s|t        d      vrt        t        d            |dk(  r|dk(  rt        t        d            y	y	# t        $ r}t        t        d            |d	}~ww xY w)
zM
    this method is used to validate the format of duration like fields.
       z)Invalid format, it should be HH:MM format:   <   zInvalid timer   z#Both hour and minute cannot be zeroN)lenr   _splitintstrrange
ValueError)valuehourminuteerrors       4/var/www/python-projects/worksol/biometric/models.pyvalidate_schedule_time_formatr      s     5zA~a KLMM	Y{{3'f4yVs4y>AuRy!8!!N"344191!!$I"JKK %9 Ya KLMSXXYs   B B& &	C	/CC	c            	           e Zd ZdZd ed      fd ed      fd ed      fd ed	      fd
 ed      fgZd ed      fd ed      fd ed      fd ed      fgZ ej                  e	j                  dd      Z ej                  d ed            Z ej                  ded ed            Z ej                  dddd ed             Z ej                   dd ed!      "      Z ej                  dddd# ed$             Z ej                  dddd ed%             Z ej                  ddd ed$      &      Z ej                  d'dd ed(      &      Z ej                  d'dd ed)      &      Z ej                  ddd ed*      &      Z ej                  ddd ed+      &      Z ej                  d,dd-      Z ej                  ddd-      Z ej6                  d ed.      /      Z ej6                  d ed0      /      Z ej                  dd1d2eg3      Z ej@                  dd4      Z! ejD                  dd4      Z# ej                  d5ed ed6      7      Z$ ejJ                  e&ddejN                   ed8      9      Z( e)       Z*d: Z+d; Z,d< Z-d= Z.d> Z/d? Z0d@ Z1dA Z2dB Z3dC Z4 fdDZ5 G dE dF      Z6 xZ7S )GBiometricDevicesai  
    Model: BiometricDevices

    Represents a biometric device used for attendance tracking within a
    company. Each device can be of different types such as ZKTeco Biometric,
    Anviz Biometric, or Matrix COSEC Biometric.The model includes fields for
    device details, authentication credentials, scheduling information, and
    company association.
    zkzZKTeco / eSSL BiometricanvizzAnviz BiometriccoseczMatrix COSEC BiometricdahuazDahua Biometricetimeofficeze-Time Officeinz	In Deviceoutz
Out Device	alternatezAlternate In/Out DevicesystemzSystem Direction(In/Out) DeviceTFdefaultprimary_keyeditabled   Name
max_lengthverbose_name   zDevice Type)r2   choicesnullr3       z
Machine IP)r2   r6   blankr,   r3   zPort No)r6   r9   r3   0PasswordUsername)r2   r6   r9   r3      z
Request IDzAPI UrlzAPI Keyz
API Secreti  r2   r6   r9   zIs Live)r,   r3   zIs Scheduledz00:00
   )r6   r,   r2   
validatorsr6   r9   2   zDevice Direction)r2   r5   r,   r3   r
   )r6   r.   	on_deleter3   c                 8    | j                    d| j                   S Nz - )namemachine_typeselfs    r   __str__zBiometricDevices.__str__{   s    ))C 1 1233    c                     | j                   dv rd| j                   d| j                   S | j                   dk(  rd| j                   S y)z;
        return card details based on machine type
        r"   r$   zMachine IP : z<br>Port No : r#   z
API Url : r8   )rG   
machine_ipportapi_urlrH   s    r   get_card_detailsz!BiometricDevices.get_card_details~   sP    
 /"4??"3>$))MM')~..rK   c                     | j                   rdnd}d}| j                   sdnd}| j                  dv rd| d| d	| d
}|S y)z%
        live capture button
        checkedr8   zActivate live capture modeActivate
DeactivaterM   zS
            <td>
                  <span class="oh-kanban-card__subtitle d-block">z</span>
                </td>
                <td>
                <div class="oh-switch">
                    <input type="checkbox"
                        class="style-widget oh-switch__checkbox is-live-activate"
                        title="z"
                        data-toggle="oh-modal-toggle"
                        data-target="#BiometricDeviceTestModal"
                        name="is_live"
                        a  
                        hx-trigger="change"
                        hx-get="/biometric/biometric-device-live-capture?deviceId=d9ab2bc7-01d5-4005-897f-01f35198d743&amp;search=&amp;machine_type=&amp;is_scheduler=unknown&amp;is_active=unknown&amp;is_live=unknown&amp;page=1&amp;view=card"
                        hx-target="#BiometricDeviceTestFormTarget" />
                </div>
                </td>
                )is_liverG   )rI   checked_attributeactivate_labelactivate_titlehtmls        r   render_live_capture_htmlz)BiometricDevices.render_live_capture_html   so     *.I25+/<<\/BBPAQ R  !// 0 ++ ,D& KrK   c           	      d   | j                   dv rdnd}t        d| j                  g      }t        d| j                  g      }t        d| j                  g      }t        d| j                  g      }d	| d
| d| j                  rd| dnd|z   dz    d| j                   dv rd|z   dz   nd d	}|S )z!
        actions buttons
        )r#   r$   zstyle='margin-top:0px;'r8   zbiometric-device-test)argszbiometric-device-unschedulezbiometric-device-schedulezbiometric-device-employeesz@
        <div class="d-block oh-kanban-card__biometric-actions" zE style="display: flex; gap: 10px; ">
            <a href="#" hx-get="aT  " data-toggle="oh-modal-toggle"
                data-target="#BiometricDeviceTestModal" hx-target="#BiometricDeviceTestFormTarget"
                class="oh-checkpoint-badge text-success mr-2" style="border: 2px solid #28a745; padding: 5px 10px; border-radius: 4px; display: inline-block; color: #28a745;">Test
            </a>
            zS<a hx-confirm='Do you want to unschedule the device attendance fetching?' hx-post='z' hx-target='#biometricDeviceList' class='oh-checkpoint-badge text-info ' style='border: 2px solid #17a2b8; padding: 5px 10px; border-radius: 4px; display: inline-block; color: #17a2b8;'>Unschedule</a>z:<a href='#' class='oh-checkpoint-badge text-info' hx-get='z' data-toggle='oh-modal-toggle' data-target='#BiometricDeviceModal' hx-target='#BiometricDeviceFormTarget' style='border: 2px solid #17a2b8; padding: 5px 10px; border-radius: 4px; display: inline-block; color: #17a2b8;'>Schedule</a>z
            rM   z	<a href='z' class='oh-checkpoint-badge text-secondary bio-user-list ml-4' style='border: 2px solid #6c757d; padding: 5px 10px; border-radius: 4px; display: inline-block; color: #6c757d;'>Employee</a>z
        </div>

        )rG   r   idis_scheduler)rI   margin_styletest_urlunschedule_urlschedule_urlemployees_urlrZ   s          r   render_actions_htmlz$BiometricDevices.render_actions_html   s    *.):):>P)P%VX 	 2$''C !>dggYO:$''K <DGG9M@@L~ N!!)
 + "")* +YY K\Y ]v v	ww""o5 =(  ,k  k;=??	& rK   c                     | j                   ryy)z 
        archive status
        Archivez
Un-Archive)	is_activerH   s    r   archive_statuszBiometricDevices.archive_status   s     >>rK   c                 8    t        dd| j                  i      }|S )z/
        This method to get update url
        zbiometric-device-edit	device_idkwargsr   pkrI   urls     r   get_update_urlzBiometricDevices.get_update_url   s     2K;QR
rK   c                 8    t        dd| j                  i      }|S )z0
        This method to get archive url
        zbiometric-device-archiverk   rl   rn   rp   s     r   get_archive_urlz BiometricDevices.get_archive_url   s     5{DGG>TU
rK   c                 8    t        dd| j                  i      }|S )z/
        This method to get delete url
        zbiometric-device-deleterk   rl   rn   rp   s     r   get_delete_urlzBiometricDevices.get_delete_url   s     4k477=ST
rK   c                 ^    t        | j                        j                  | j                        S )z2
        return machine type from choices
        )dictBIO_DEVICE_TYPEgetrG   rH   s    r   get_machine_typez!BiometricDevices.get_machine_type   s&    
 D(()--d.?.?@@rK   c                 &    d| j                    d}|S )zV
        Method will retun the api to the avatar or path to the profile image
        z!https://ui-avatars.com/api/?name=z&background=random)rF   rp   s     r   
get_avatarzBiometricDevices.get_avatar   s     2$))<NO
rK   c                 "   t        |   |i | i }| j                  dv r4| j                  st	        d      |d<   | j
                  st	        d      |d<   | j                  dk(  r1| j                  st	        d      |d<   n	 t        | j                         | j                  d
v r4| j                  st	        d      |d<   | j                  st	        d      |d<   | j                  dk(  rV| j                  st	        d      |d<   | j                  st	        d      |d<   | j                  st	        d      |d<   | j                  st	        d      |d<   | j                  r| j                  r| j                  rddd| j                  dd| j                  | j                  dd}dd d!d"d"d#d}	 t        j                   | j                  |d$%      }|j"                  d&k7  rt%        d'|j"                   h      |j'                         }||k(  rt%        dt	        d(      i      |d)   }|d   }|d*   }	|| _        |	| _        |rt%        |      y # t        $ r t	        d	      |d<   Y w xY w# t,        $ r}
t%        dt	        d+      i      |
d }
~
ww xY w),N)r"   r$   r%   z=The Machine IP is required for the selected biometric device.rN   z>The Port Number is required for the selected biometric device.rO   r"   z5The password is required for ZKTeco Biometric Device.zk_passwordzgThe password must be an integer (numeric) value for                            ZKTeco Biometric Device.)r$   r%   z;The Username is required for the selected biometric device.bio_usernamez;The Password is required for the selected biometric device.bio_passwordr#   z:The Request ID is required for the Anviz Biometric Device.anviz_request_idz3The API URL is required for Anviz Biometric Device.rP   z3The API Key is required for Anviz Biometric Device.api_keyz6The API Secret is required for Anviz Biometric Device.
api_secretzauthorize.tokentokenz1.0z2022-10-21T07:39:07+00:00)	nameSpace
nameActionversion	requestId	timestamp)r   r   )headerpayloadSystem	Exception)r   rF   
AUTH_ERROR)typemessager?   )jsontimeoutr=   z!API call failed with status code zmAuthentication failed. Please check your API Url                                    , API Key and API Secret.r   expiresziAuthentication failed. Please check your API Url , API Key                                and API Secret.)supercleanrG   rN   r   rO   r   r   r   r   r   r   rP   r   r   requestspoststatus_coder   r   	api_tokenapi_expiresr   )rI   r]   rm   required_fieldsr   r   responseapi_responser   r   exc	__class__s              r   r   zBiometricDevices.clean   s   t&v& 88??01S1- 99*+T+' $##12K2.(()  22$$23Q3/ $$23Q3/ '((67P7 23 <<-.I.	* <<-.I.	* ??01L1- $$$// &7&-#(%)%:%:%@ ,0<<tW	 -5kJ(4N#'}}$ " H
  ++s2-@AUAU@VWX  $,==?L#u,- )1%?,"  +95G ' 0I"))"4K%.DN'2D$ !/22 i " 5666OM2V ! )%q!1( s+    I +BI) I&%I&)	J2J		Jc                   0    e Zd ZdZ ed      Z ed      Zy)BiometricDevices.Meta6
        Meta class to add additional options
        zBiometric DevicezBiometric DevicesN__name__
__module____qualname____doc__r   r3   verbose_name_plural rK   r   Metar   f  s     	 +, 34rK   r   )8r   r   r   r   r   ry   BIO_DEVICE_DIRECTIONr   	UUIDFielduuiduuid4r^   	CharFieldrF   rG   rN   IntegerFieldrO   r   r   r   r   rP   r   r   r   r   BooleanFieldrV   r_   r   scheduler_duration	DateFieldlast_fetch_date	TimeFieldlast_fetch_timedevice_direction
ForeignKeyr
   PROTECT
company_idr	   objectsrJ   rQ   r[   re   ri   rr   rt   rv   r{   r}   r   r   __classcell__)r   s   @r   r!   r!   )   s\    
q*+,	!%&'	!,-.	!%&'	/*+O 
q~	, 	a123	1678	 
		$**$	OB6s6CD#6##T-HXL "!!Tr,J 6D1Y<PD"&""Ts:K $6##Tr*L $6##TAjML (v''TAlO fTAiLG fTAiLG "!!TAlOJ !  Cd$GI"&""cDIK!f!!%a	lKG&6&&u1^CTUL)))12	 'f&&D=O&f&&D=O'v''$)*	 #""..y\J $%G4
@!F Ak3Z5 5rK   r!   c                      e Zd ZdZ ej
                  ej                  dd      Z ej                  dd      Z
 ej                  dd ed      g      Z ej                  d ed	      
      Z ej                  ddd      Z ej"                  eej&                   ed            Z ej"                  eej&                  dd      Z ej.                         Zd Z G d d      Zy)BiometricEmployeesz
    Model: BiometricEmployees

    Description:
    Represents the association between employees and biometric devices for
    attendance tracking within a company.Each entry in this model maps an
    employee to a specific biometric device.
    TFr+   rA   i)r6   r9   r@   r/   zUser IDr1   r>   r   )rC   r3   rC   r6   r9   c                 R    | j                    d| j                   d| j                   S rE   )employee_iduser_idrk   rH   s    r   rJ   zBiometricEmployees.__str__  s)    ""#3t||nC7GHHrK   c                   0    e Zd ZdZ ed      Z ed      Zy)BiometricEmployees.Metar   zEmployee in Biometric DevicezEmployees in Biometric DeviceNr   r   rK   r   r   r     s     	 78 ?@rK   r   N)r   r   r   r   r   r   r   r   r^   r   uidr   ref_user_idr   r   r   dahua_card_nor   r   CASCADEr   r!   rk   Managerr   rJ   r   r   rK   r   r   r   o  s     
		$**$	OB
&

4t
4C%&%%+<X+F*GK f#AiLIG$F$$$dKM#&##FNN:K "!!FNNTI fnnGIA ArK   r   c                      e Zd ZdZ ej
                  ej                  dd      Z ej                  dd      Z
 ej                  dd      Z ej                  eej                  dd      Z ej                          Z G d d	      Zd
 Zy)COSECAttendanceArgumentsa  
    Model: COSECAttendanceArguments

    Description:
    Represents arguments related to attendance fetching for COSEC biometric
    devices within a company.This model stores information such as the last
    fetched roll-over count and sequence number for COSEC devices.
    TFr+   r/   )r2   r6   r   c                   ,    e Zd Z ed      Z ed      Zy)COSECAttendanceArguments.MetazCOSEC Attendance ArgumentsN)r   r   r   r   r3   r   r   rK   r   r   r     s    56 <=rK   r   c                 R    | j                    d| j                   d| j                   S rE   )rk   last_fetch_roll_ovr_countlast_fetch_seq_numberrH   s    r   rJ   z COSECAttendanceArguments.__str__  s,    ..!T%C%C$DCHbHbGcddrK   N)r   r   r   r   r   r   r   r   r^   r   r   r   r   r!   r   rk   r   r   r   rJ   r   rK   r   r   r     s     
		$**$	OB 0 0 0Cd K,F,,$G!!!FNNTI fnnG> >erK   r   )r   r   r   django.core.exceptionsr   django.core.validatorsr   	django.dbr   django.urlsr   r   django.utils.translationr   r   base.horilla_company_managerr	   base.modelsr
   employee.modelsr   horilla.modelsr   horilla_views.cbv_methodsr   r   r!   Modelr   r   r   rK   r   <module>r      sj   
   2 4  - 6 >  $ ' 5Y$C5| C5L
"A "AJev|| erK   