
    2Yh6                         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	m
Z
 d dlmZ d dlmZ  e	        G d d	             Z G d
 de      Zy)    )datetime)AnyDict)UUID)inspect)as_declarativedeclared_attr)Query)symbolc                       e Zd ZU dZeed<   eed<   edefd       Zde	fdZ
de	fdZdedefd	Zdd
e	deeef   fdZy)BasezORM base classid__name__returnc                 r    d}| j                   r| j                   }|S | j                  j                         }|S )z/Generate __tablename__ for the SQLAlchemy model )__tablename__r   lower)clsnames     N/var/www/html/wine-match-dev/backend/winematch-backend/src/core/models/base.pyr   zBase.__tablename__   s=     $$D  <<%%'D    c                 2    |j                   t        d      k(  S )z0Returns if a model property is defined peropertyHYBRID_PROPERTYextension_typer   selform_descriptors     r   _is_hybrid_propertyzBase._is_hybrid_property   s    ,,7H0IIIr   c                 2    |j                   t        d      k(  S )z.Returns if a model property is hybrid propertyHYBRID_METHODr   r   s     r   _is_hybrid_methodzBase._is_hybrid_method    s    ,,0GGGr   inpc                    |j                         D ]u  }t        ||   t              rt        ||         ||<   t        ||   t              rt        ||         ||<   t        ||   t
              s_| j                  ||         ||<   w |S )z%Convert a given dict to jsonable dict)keys
isinstancer   strr   r   _prepare_json)r   r$   keys      r   r)   zBase._prepare_json$   s    88: 	8C#c(D)s3x=C#c(H-s3x=C#c(D)--c#h7C	8 
r   include_propertiesc                    t        |       j                  }|j                  }|j                         D cg c]  \  }}|	 }}}t	        |      D ci c],  }| j
                  j                  |d      s |t        | |      . }}|rc|j                         D cg c])  \  }}| j                  |      s| j                  |      r|+ }	}}t	        |	      D ]  }t        | |      ||<    | j                  |      S c c}}w c c}w c c}}w )a_  Convert incoming SQLAlchemy model entity to a Python Dictionary

        If a SQLAlchemy model entity has joined attributes, they will be transformed to nested dictionaries

        If include_properties is set to True, the returning Dict will include
        the hybrid_properties as nested dictionaries for the given SQLAlchemy model entity
        F)r   mapperall_orm_descriptorsitemsset__dict__getgetattrr    r#   r)   )
r   r+   inspect_mapperorm_descriptorsr*   itemdefined_namescresultshybrid_namess
             r   _asdictzBase._asdict/   s    !--(<<.=.C.C.EFdFF03M0Bb1dmmFWFWXY[`Fa1gdA&&bb "1!6!6!8C++D1T5K5KD5Q L 
 & .$T1-
. !!'** Gbs   C:!D 3D .DN)T)r   
__module____qualname____doc__r   __annotations__r(   r	   r   boolr    r#   r   r)   r;    r   r   r   r      sr    GMc  JT JH4 H	 	$ 	+$ +$sCx. +r   r   c                       e Zd ZdZd Zd Zy)UseSoftDeleteFc                 |    t        | d      r| j                  j                  gS | j                         j                  S )Nattr)hasattrrE   target_mapper_mapper_zeroclass_)r   s    r   _get_modelszUseSoftDelete._get_modelsJ   s4    4 II++,,$$&---r   c                 |    | j                         d   }| j                  |j                  j                  d             S )Nr   )rJ   filter
deleted_atis_)r   model_classs     r   allzUseSoftDelete.allP   s5    &&(+{{;1155d;<<r   N)r   r<   r=   _with_deletedrJ   rP   rA   r   r   rC   rC   G   s    M.=r   rC   N)r   typingr   r   uuidr   
sqlalchemyr   sqlalchemy.ext.declarativer   r	   sqlalchemy.ormr
   	BaseQuerysqlalchemy.utilr   r   rC   rA   r   r   <module>rY      sC        D - " 8+ 8+ 8+v=I =r   