a
    gh2                     @   sV   d dl Z d dlmZ d dlmZ G dd dejZG dd deZG dd	 d	eZdS )
    N)datetime)dbc                       sX   e Zd ZdZdddgdZejee	 ddddZ
dd Z fd	d
Zdd Z  ZS )UniqueIDMixina  
    A mixin class that provides a unique identifier for the document.

    Base class for MongoDB documents with an automatically generated unique public ID.

    Attributes:
        public_id (str): A unique identifier for the document.

    Methods:
        generate_public_id(): Generates a random UUID and assigns it to the public_id field.
        to_dict(): Converts the document to a dictionary for serialization.

    T	public_id)allow_inheritanceabstractindexesz#Unique identifier for the document.)defaultuniquerequired	help_textc                 C   s   t t | _dS )zQ
        Generates a random UUID and assigns it to the public_id field.

        N)struuiduuid4r   self r   /app/app/models/base.pygenerate_public_id#   s    z UniqueIDMixin.generate_public_idc                    s(   t t| j|i | | js$|   dS )z
        Initialize a new ExtendedModel instance.

        Args:
            *args: Additional positional arguments.
            **kwargs: Additional keyword arguments.

        N)superr   __init__r   r   r   argskwargs	__class__r   r   r   *   s    	zUniqueIDMixin.__init__c                 C   s6   |    }t| j|d< |dd |dd |S )
        Converts the document to a dictionary for serialization.

        Returns:
            dict: A dictionary representation of the document.

        id_idN_cls)to_mongoto_dictr   r   popr   Zdocument_dictr   r   r   r!   8   s
    zUniqueIDMixin.to_dict)__name__
__module____qualname____doc__metar   StringFieldr   r   r   r   r   r   r!   __classcell__r   r   r   r   r      s   
r   c                       s   e Zd ZdZejdddZejej	ddZ
ejddZejdd	dZ fd
dZdd Zdd Zdd Z fddZdd Zedd ZdddgdZ  ZS )TimestampedMixinzN
    A mixin class that adds timestamps for document creation and update.
    Fz%Indicates if the document is deleted.)r	   r   z Timestamp for document creation.z'Timestamp for the last document update.)r   Nz Timestamp for document deletion.c                    s   t  j|i | dS )z%
        Saves the document.
        N)r   saver   r   r   r   r,   ^   s    zTimestampedMixin.savec                 C   s   t  | _|   dS )zh
        Updates the document and the document's `updated_at` field with the current timestamp.
        N)r   now
updated_atr,   r   r   r   r   updated   s    
zTimestampedMixin.updatec                 C   s   d| _ t | _|   dS )z
        Soft delete the document by setting the 'deleted' flag and timestamp. 
        Archives the document by setting the `deleted` field to True and updating the `deleted_at` field with the current timestamp.
        TN)deletedr   r-   
deleted_atr,   r   r   r   r   archivek   s    
zTimestampedMixin.archivec                 C   s   d| _ d| _|   dS )z
        Restore a soft-deleted document.
        Restores the document by setting the `deleted` field to False and clearing the `deleted_at` field.
        FN)r0   r1   r,   r   r   r   r   restoret   s    zTimestampedMixin.restorec                    sZ   t t|  }| |d|d< | |d|d< | |d|d< |dd |S )z7
        Convert the document to a dictionary.
        
created_atr.   r1   r   N)r   r+   r!   _format_datetimegetr"   r#   r   r   r   r!   }   s    zTimestampedMixin.to_dictc                 C   s   |rt |tr| S dS )zO
        Helper method to format a datetime object to ISO 8601 string.
        N)
isinstancer   	isoformat)r   dtr   r   r   r5      s    z!TimestampedMixin._format_datetimec                 C   sb   t d|}tt d|d}| jdd }| jdd|d | |}|||dd |D dS )z4
        Get a paginated list of documents.
           d   F)r0   c                 S   s   g | ]}|  qS r   )r!   ).0docr   r   r   
<listcomp>       z2TimestampedMixin.get_paginated.<locals>.<listcomp>)totalpageper_pageitems)maxminobjectscountskiplimit)clsrA   rB   r@   	documentsr   r   r   get_paginated   s    
 zTimestampedMixin.get_paginatedTz-created_atz-updated_at)r   ordering)r$   r%   r&   r'   r   BooleanFieldr0   DateTimeFieldr   r-   r4   r.   r1   r,   r/   r2   r3   r!   r5   classmethodrL   r(   r*   r   r   r   r   r+   H   s6   		
r+   c                       s>   e Zd ZdZee Zdd Zdd Z	 fddZ
  ZS )TaggedMixinax  
    Base class for MongoDB documents with tags.

    Attributes:
        tags (list): A list of tags associated with the document.

    Methods:
        add_tag(tag): Adds a tag to the document if it doesn't already exist.
        remove_tag(tag): Removes a tag from the document if it exists.
        to_dict(): Converts the document to a dictionary for serialization.

    c                 C   s   || j vr| j | dS )z
        Adds a tag to the document if it doesn't already exist.

        Args:
            tag (str): The tag to add.

        N)tagsappendr   tagr   r   r   add_tag   s    
zTaggedMixin.add_tagc                 C   s   || j v r| j | dS )zy
        Removes a tag from the document if it exists.

        Args:
            tag (str): The tag to remove.

        N)rR   removerT   r   r   r   
remove_tag   s    
zTaggedMixin.remove_tagc                    s   t t|  }|dd |S )r   r   N)r   rQ   r!   r"   r#   r   r   r   r!      s    zTaggedMixin.to_dict)r$   r%   r&   r'   r   	ListFieldr)   rR   rV   rX   r!   r*   r   r   r   r   rQ      s
   rQ   )r   r   app.config.extensionsr   DynamicDocumentr   r+   rQ   r   r   r   r   <module>   s
   A`