
    D6i                       d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ erd dlmZ d d	lmZ  G d
 dej&                        Z G d dej*                        Z G d dej.                        ZddZddZy)    )annotationsN)TYPE_CHECKINGAnycast)fields)is_iterable_but_not_string)inspect)NoResultFound)DeclarativeMeta)MapperPropertyc                        e Zd Zd fd	Z xZS )RelatedListc                D    t         t        j                  |   |||      S )N)accessor)superr   List	get_value)selfobjattrr   	__class__s       `/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/marshmallow_sqlalchemy/fields.pyr   zRelatedList.get_value   s"    
 V[[$1#th1OO    N)__name__
__module____qualname__r   __classcell__r   s   @r   r   r      s    P Pr   r   c                       e Zd ZdZddiZ	 	 d	 	 	 d fdZedd       Zedd       Zed        Z	ed        Z
ed	        Zd
 Zd Zd Z xZS )Relateda~  Related data represented by a SQLAlchemy `relationship`. Must be attached
    to a `Schema <marshmallow.Schema>` class whose options includes a SQLAlchemy `model`, such
    as `SQLAlchemySchema <marshmallow_sqlalchemy.SQLAlchemySchema>`.

    :param columns: Optional column names on related model. If not provided,
        the primary key(s) of the related model will be used.
    invalidzWCould not deserialize related value {value!r}; expected a dictionary with keys {keys!r}c                    | t        j                  dt        d       ||}t        |   di | t        |xs g       | _        y )Nz_`column` parameter is deprecated and will be removed in future releases. Use `columns` instead.   )
stacklevel )warningswarnDeprecationWarningr   __init__ensure_listcolumns)r   r,   columnkwargsr   s       r   r*   zRelated.__init__'   sN     MM)"	  "6""-gm"<r   c                p    | j                   t        d      | j                   j                  j                  S )Nz4Cannot access model before field is bound to schema.)rootRuntimeErroroptsmodelr   s    r   r3   zRelated.model9   s,    99UVVyy~~###r   c                   | j                   t        d      t        | j                   t        t        | j
                  xs | j                              }t        |d      r|j                  }|j                  j                  j                  S )Nz<Cannot access related_model if schema does not have a model.remote_attr)r3   r1   getattrr   str	attributenamehasattrr6   propertymapperclass_)r   
model_attrs     r   related_modelzRelated.related_model?   so    ::N  TZZc4>>3NTYY)OP
:}-#//J""))000r   c                    | j                   r<t        | j                        }| j                   D cg c]  }|j                  |    c}S t	        | j                        S c c}w r   )r,   r	   r@   attrsget_primary_keys)r   inspr-   s      r   related_keyszRelated.related_keysJ   sN    <<4--.D59\\B6DJJv&BB 2 233 Cs   Ac                .    | j                   j                  S r   )r0   sessionr4   s    r   rG   zRelated.sessionQ   s    yy   r   c                .    | j                   j                  S r   )r0   	transientr4   s    r   rI   zRelated.transientU   s    yy"""r   c           	         | j                   D ci c]$  }|j                  t        ||j                  d       & }}t        |      dkD  r|S t	        t        |j                                     S c c}w )N   )rE   keyr7   lennextitervalues)r   valuer   r   proprets         r   
_serializezRelated._serializeY   s\    DHDUDUVDtxx$77VV#hls@T#**,-?(@@ Ws   )A,c                   t        |t              sjt        | j                        dk7  r7| j                  D cg c]  }|j                   }}| j                  d||      | j                  d   j                  |i}| j                  r | j                  di |S 	 | j                  | j                  |      }|S c c}w # t        $ r  | j                  di |cY S w xY w)a  Deserialize a serialized value to a model instance.

        If the parent schema is transient, create a new (transient) instance.
        Otherwise, attempt to find an existing instance in the database.
        :param value: The value to deserialize.
        rK   r"   rQ   keysr   r&   )

isinstancedictrM   rE   rL   
make_errorrI   r@   _get_existing_instancer
   )r   rQ   argsr.   rR   rW   results          r   _deserializezRelated._deserialize]   s     %&4$$%*-1->->?T??ooiu4oHH&&q)--u5E>>%4%%...	/001C1CUKF
  @  	/ &4%%...	/s   B8B= =CCc                l   | j                   rw | j                  j                  |      j                  di | j                  D ci c](  }|j
                  |j                  |j
                        * c}j                         }|S | j                  D cg c]  }|j                  |j
                         }}	 | j                  j                  ||      }|t        |S c c}w c c}w # t        $ rC}| j                  D cg c]  }|j
                   nc c}w }}| j                  d||      |d}~ww xY w)a  Retrieve the related object from an existing instance in the DB.

        :param related_model: The related model to query
        :param value: The serialized value to mapto an existing instance.
        :raises NoResultFound: if there is no matching record.
        r"   rV   Nr&   )r,   rG   query	filter_byrE   rL   getone	TypeErrorrZ   r
   )r   r@   rQ   rR   r]   lookup_valueserrorrW   s           r   r[   zRelated._get_existing_instances   s    <<""=1AEARARStxx488!44S  "  >B=N=NOTUYYtxx0OMOT))-G ~## T P  T-1->->?T???ooiu4oHeSTs0   -C"C"7C' '	D30D.>DD..D3)NN)r,   zlist[str] | str | Noner-   z
str | None)returnztype[DeclarativeMeta] | None)rg   type[DeclarativeMeta])r   r   r   __doc__default_error_messagesr*   r<   r3   r@   rE   rG   rI   rT   r^   r[   r   r   s   @r   r!   r!      s     	 3 +/!='= =$ $ $
 1 1 4 4 ! ! # #A,r   r!   c                  "     e Zd ZdZ fdZ xZS )Nestedz7Nested field that inherits the session from its parent.c                    t        | j                  d      rJ| j                  j                  | j                  _        | j                  j                  | j                  _        t        |   |i |S )NrG   )r;   schemar0   rG   rI   r   r^   )r   r\   r.   r   s      r   r^   zNested._deserialize   sR    4;;	*"&))"3"3DKK$(II$7$7DKK!w#T4V44r   )r   r   r   ri   r^   r   r   s   @r   rl   rl      s    A5 5r   rl   c                v    | j                   }|j                  D cg c]  }|j                  |       c}S c c}w )zaGet primary key properties for a SQLAlchemy model.

    :param model: SQLAlchemy model class
    )
__mapper__primary_keyget_property_by_column)r3   r=   r-   s      r   rC   rC      s4    
 F@F@R@RSfF))&1SSSs   6c                4    t        |       rt        |       S | gS r   )r   list)rQ   s    r   r+   r+      s    4U;4;H%Hr   )r3   rh   rg   zlist[MapperProperty])rQ   r   rg   rt   )
__future__r   r'   typingr   r   r   marshmallowr   marshmallow.utilsr   
sqlalchemyr	   sqlalchemy.orm.excr
   sqlalchemy.ext.declarativer   sqlalchemy.ormr   r   r   Fieldr!   rl   rC   r+   r&   r   r   <module>r~      sg    "  + +  8  ,:-P&++ Psfll sl5V]] 5TIr   