
    vgv                         d 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 ed
e
fd       Z ee
de        G d dej*                        Z ej.                  ed       y)aX  
models.py
=========

This module defines the abstract base model `HorillaModel` for the Horilla HRMS project.
The `HorillaModel` provides common fields and functionalities for other models within
the application, such as tracking creation and modification timestamps and user
information, audit logging, and active/inactive status management.
    )AuditlogHistoryField)auditlog)User)models)	FieldFile)reverse)gettext)_thread_localsselfc                     	 | j                          | j                  j	                  | j
                        S # t        $ r}t        d      cY d}~S d}~ww xY w)z'
    Custom url attribute/property
    404N)_require_file	Exceptionr   storageurlname)r   es     "/var/www/horilla/horilla/models.pyr   r      sJ    
 <<DII&&  u~s   7 	A 
A
AAr   c            
           e Zd ZdZ ej
                  ddd ed            Z ej                  e	ej                  ddd ed            Z ej                  e	ej                  ddd ed      d	
      Z e       Z ej                         Z ej"                  d ed            Z G d d      Z fdZed        Zed        Z xZS )HorillaModelz
    An abstract base model that includes common fields and functionalities
    for models within the Horilla application.
    Tz
Created At)auto_now_addnullblankverbose_nameFz
Created By)	on_deleter   r   editabler   zModified Byz%(class)s_modified_by)r   r   r   r   r   related_namez	Is Active)defaultr   c                       e Zd ZdZdZy)HorillaModel.Metaz-
        Meta class for HorillaModel
        TN)__name__
__module____qualname____doc__abstract     r   Metar    G   s    	 r'   r(   c                    t        t        dd      }|r|j                  }t        | d      rrt        | j                  j                  d      d      rM| j                  j                  d      j                  t        k(  r!|r| j                  s|j                  r|| _
        |r|j                  j                  s|| _        t        t        | >  |i | y)z
        Override the save method to automatically set the created_by and
        modified_by fields based on the current request user.
        requestN
created_byrelated_model)getattrr
   userhasattr_meta	get_fieldr,   r   pkis_authenticatedr+   is_anonymousmodified_bysuperr   save)r   argskwargsr*   r.   	__class__s        r   r7   zHorillaModel.saveN   s    
 .)T:<<D l+DJJ00>PJJ((6DDL477,,*.w||88#' lD&77r'   c                     	 | j                   j                  |      j                         }|S # t        $ r
}Y d}~yd}~ww xY w)z9
        Find an object of this class by its ID.
        )idN)objectsfilterfirstr   )cls	object_idobjr   s       r   findzHorillaModel.findf   s?    
	++$$	$288:CJ 		s   +. 	AAc                 p    | j                  |      }|r#|j                   |_        |j                          yy)zI
        Toggle the is_active status of an object of this class.
        N)rC   	is_activer7   )r@   rA   rB   s      r   activate_deactivatez HorillaModel.activate_deactivater   s1    
 hhy! #-CMHHJ r'   )r!   r"   r#   r$   r   DateTimeField_
created_at
ForeignKeyr   SET_NULLr+   r5   r   horilla_historyManagerr=   BooleanFieldrE   r(   r7   classmethodrC   rF   __classcell__)r:   s   @r   r   r   %   s    
 &%%|_	J #""//|_J $&##//}%,K +,OfnnG###Dq~NI 80 	 	  r'   r   T)serialize_dataN)r$   auditlog.modelsr   auditlog.registryr   django.contrib.auth.modelsr   	django.dbr   django.db.models.fields.filesr   django.urlsr   django.utils.translationr	   rH   horilla.horilla_middlewaresr
   propertyr   setattrModelr   registerr&   r'   r   <module>r^      st    1 & +  3  1 6 
'i ' 
' 	5# U6<< Up   ,t 4r'   