U
    	h!                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ G dd dejZ	G dd	 d	ejZ
G d
d dejZG dd dejZG dd dejZdS )a"  
Core Models for Stream Processor Application

This module contains abstract base models and shared functionality
that is used across multiple applications in the Stream Processor project.
These models provide common fields and methods that promote consistency
and reduce code duplication.
    N)models)timezone)Userc                       sJ   e Zd ZdZejdddZejdddZG dd dZ fd	d
Z	  Z
S )TimestampedModela  
    Abstract base model that provides timestamp fields.
    
    This model adds created_at and updated_at fields to any model
    that inherits from it. These fields are automatically managed
    and provide audit trail capabilities.
    
    Attributes:
        created_at (DateTimeField): Timestamp when the record was created
        updated_at (DateTimeField): Timestamp when the record was last modified
    Tz&Timestamp when this record was created)auto_now_add	help_textz,Timestamp when this record was last modified)auto_nowr   c                   @   s   e Zd ZdZdS )zTimestampedModel.MetaTN__name__
__module____qualname__abstract r   r   4/var/www/html/JingleDetector/src/apps/core/models.pyMeta)   s   r   c                    s   t  | _t j|| dS )a  
        Override save method to ensure updated_at is always current.
        
        This method ensures that the updated_at field is always set
        to the current timestamp when a record is saved, regardless
        of whether it's a new record or an update.
        
        Args:
            *args: Variable length argument list for parent save method
            **kwargs: Arbitrary keyword arguments for parent save method
        N)r   now
updated_atsupersave)selfargskwargs	__class__r   r   r   -   s    
zTimestampedModel.save)r
   r   r   __doc__r   DateTimeFieldZ
created_atr   r   r   __classcell__r   r   r   r   r      s   r   c                   @   s2   e Zd ZdZejdejdddZG dd dZ	dS )		UUIDModelaU  
    Abstract base model that provides UUID primary key.
    
    This model uses UUID as the primary key instead of the default
    auto-incrementing integer. UUIDs are useful for distributed systems
    and provide better security by making IDs unpredictable.
    
    Attributes:
        id (UUIDField): Primary key using UUID format
    TFz!Unique identifier for this record)primary_keydefaulteditabler   c                   @   s   e Zd ZdZdS )zUUIDModel.MetaTNr	   r   r   r   r   r   S   s   r   N)
r
   r   r   r   r   	UUIDFielduuiduuid4idr   r   r   r   r   r   ?   s   r   c                   @   s:   e Zd ZdZejeejdddZG dd dZ	dd Z
d	S )
UserOwnedModelad  
    Abstract base model for records that belong to a specific user.
    
    This model adds a foreign key relationship to the User model,
    allowing records to be associated with specific users. It also
    provides utility methods for checking ownership.
    
    Attributes:
        owner (ForeignKey): Reference to the User who owns this record
    z%(class)s_setzUser who owns this record)	on_deleterelated_namer   c                   @   s   e Zd ZdZdS )zUserOwnedModel.MetaTNr	   r   r   r   r   r   l   s   r   c                 C   s
   | j |kS )a  
        Check if this record is owned by the specified user.
        
        This method provides a convenient way to check ownership
        of a record, which is useful for permission checking.
        
        Args:
            user (User): The user to check ownership against
            
        Returns:
            bool: True if the user owns this record, False otherwise
        )owner)r   userr   r   r   is_owned_byp   s    zUserOwnedModel.is_owned_byN)r
   r   r   r   r   
ForeignKeyr   CASCADEr(   r   r*   r   r   r   r   r%   X   s   r%   c                   @   sd   e Zd ZdZdddddddgZejd	ed
dddZG dd dZdd Z	dd Z
dd Zdd ZdS )StatusModela  
    Abstract base model that provides status tracking.
    
    This model adds status field with common status choices
    that can be used across different types of records.
    
    Attributes:
        status (CharField): Current status of the record
    )activeZActive)ZinactiveZInactive)pendingZPending)
processing
Processing)	completedZ	Completed)failedZFailed)	cancelledZ	Cancelled   r/   TzCurrent status of this record)
max_lengthchoicesr   db_indexr   c                   @   s   e Zd ZdZdS )zStatusModel.MetaTNr	   r   r   r   r   r      s   r   c                 C   s
   | j dkS )z
        Check if this record has an active status.
        
        Returns:
            bool: True if status is 'active', False otherwise
        r.   statusr   r   r   r   	is_active   s    zStatusModel.is_activec                 C   s
   | j dkS )z
        Check if this record is currently being processed.
        
        Returns:
            bool: True if status is 'processing', False otherwise
        r0   r9   r;   r   r   r   is_processing   s    zStatusModel.is_processingc                 C   s
   | j dkS )z
        Check if this record has completed processing.
        
        Returns:
            bool: True if status is 'completed', False otherwise
        r2   r9   r;   r   r   r   is_completed   s    zStatusModel.is_completedc                 C   s
   | j dkS )z
        Check if this record has failed processing.
        
        Returns:
            bool: True if status is 'failed', False otherwise
        r3   r9   r;   r   r   r   	is_failed   s    zStatusModel.is_failedN)r
   r   r   r   ZSTATUS_CHOICESr   	CharFieldr:   r   r<   r=   r>   r?   r   r   r   r   r-      s*   			r-   c                   @   sp   e Zd ZdZejdddddZejddZejddd	Z	ej
ddd
dZG dd dZdd ZedddZdS )ConfigurationModela  
    Abstract base model for configuration records.
    
    This model provides a structure for storing configuration
    key-value pairs with metadata about when they were created
    and last modified.
    
    Attributes:
        key (CharField): Configuration key identifier
        value (TextField): Configuration value (can store JSON)
        description (TextField): Human-readable description of the setting
        is_active (BooleanField): Whether this configuration is active
    d   Tz0Unique identifier for this configuration setting)r6   uniquer8   r   z2Configuration value (can be JSON for complex data))r   z3Description of what this configuration setting does)blankr   z,Whether this configuration setting is active)r   r8   r   c                   @   s   e Zd ZdZdgZdS )zConfigurationModel.MetaTkeyN)r
   r   r   r   orderingr   r   r   r   r      s   r   c                 C   s   | j S )z
        String representation of the configuration.
        
        Returns:
            str: Configuration key for easy identification
        )rE   r;   r   r   r   __str__   s    zConfigurationModel.__str__Nc                 C   s8   z| j j|dd}|jW S  | jk
r2   | Y S X dS )a  
        Get configuration value by key with optional default.
        
        This class method provides a convenient way to retrieve
        configuration values with fallback to default values.
        
        Args:
            key (str): Configuration key to look up
            default: Default value if key is not found
            
        Returns:
            str: Configuration value or default if not found
        T)rE   r<   N)objectsgetvalueDoesNotExist)clsrE   r   configr   r   r   get_config_value   s
    z#ConfigurationModel.get_config_value)N)r
   r   r   r   r   r@   rE   	TextFieldrJ   descriptionBooleanFieldr<   r   rG   classmethodrN   r   r   r   r   rA      s.   	rA   )r   r"   	django.dbr   django.utilsr   django.contrib.auth.modelsr   Modelr   r   r%   r-   rA   r   r   r   r   <module>   s   	/(H