
    K:gT-              	       j   d dl m Z  d dlmZ d dlmZmZmZmZmZm	Z	m
Z
mZmZmZ d dlZd dlZd dlmZ d dlmZmZmZ erd dlmZ  G d d	ej2                        Zd
edefdZddd
eddde
e   ddfdZ G d de      Z G d de      Z G d de      Z 	 dd
eee   e	eef   f   de
ee!      ddfdZ"y)    )datetime)partial)
TYPE_CHECKINGAnyClassVarDictIterableMappingOptionalSetTypeUnionN)Self)_append_docstring_textdatetime_from_iso_strdatetime_to_iso_str)Apic            
            e Zd ZU dZ ej
                  d eej                  d      d      Z	 ej                         Zeeef   ed<   ded	d
f fdZed
ddeeef   dddee   d	efd       Z xZS )AirtableModelzW
    Base model for any data structures that will be loaded from the Airtable API.
    ignoreF)uppercase_first_letterT)extraalias_generatorpopulate_by_name_rawdatareturnNc                 L   |j                         }| j                  j                         D ]`  \  }}||j                  hD ]J  }|r|j	                  |      x}st        |t              s*|j                  t        u s=t        |      ||<   L b t        | ,  di | || _        y )N )copymodel_fieldsitemsaliasget
isinstancestr
annotationr   r   super__init__r   )selfr   raw
field_namefield_modelnamevalue	__class__s          T/var/www/html/lionshead/venv/lib/python3.12/site-packages/pyairtable/models/_base.pyr)   zAirtableModel.__init__+   s    iik (,'8'8'>'>'@#J#[%6%67TXXd^$;E$;eS)k.D.D.P!6u!=DJ	 8 (A 	 4 	    contextobjapir   r4   c                2     | di |}t        |||       |S )ao  
        Construct an instance which is able to update itself using an
        :class:`~pyairtable.Api`.

        Args:
            obj: The JSON data structure used to construct the instance.
                 Will be passed to `parse_obj <https://docs.pydantic.dev/latest/usage/models/#helper-functions>`_.
            api: The connection to use for saving updates.
            context: An object, sequence of objects, or mapping of names to objects
                which will be used as arguments to ``str.format()`` when constructing
                the URL for a :class:`~pyairtable.models._base.RestfulModel`.
        r3   r   )cascade_api)clsr5   r6   r4   instances        r1   from_apizAirtableModel.from_api;   s     ( ::Hc73r2   )__name__
__module____qualname____doc__pydantic
ConfigDictr   
inflectioncamelizemodel_configPrivateAttrr   r   r&   r   __annotations__r)   classmethodr   SelfTyper;   __classcell__r0   s   @r1   r   r      s     '8&&
 3 3ERL 08//1D$sCx.1s t    "&#s(^ 
 # 
 r2   r   r5   r   c                 R    t        j                  t        |       j                        S N)rB   
underscoretyper<   )r5   s    r1   _context_namerO   T   s      c!3!344r2   r3   r6   r   r4   c                   |i }|rt        |t              st        |      |i}|j                  dt	                     }t        |       |v ry|j                  t        |              t        | t        t        t        f      r| D ]  }t        |||        t        | t              r,| j                         D ]  \  }}t        ||i |d|i        t        | t              syi |t        |       | i}t        | t              r| j                  ||       t        |       j                  D ]   }t!        | |d      x}st        |||       " y)a  
    Ensure all nested objects have access to the given Api instance,
    and trigger them to configure their URLs accordingly.

    Args:
        api: The instance of the API to set.
        context: A mapping of class names to instances of that class.
    N__visited__r3   key)r%   dictrO   
setdefaultsetidaddlisttupler8   r"   r   RestfulModel_set_apirN   r!   getattr)r5   r6   r4   visitedr/   rR   r,   field_values           r1   r8   r8   X   s3    z'40 )73  **=#%@G	#w'KK3 #eS)*EsG4 #t))+JCs,Cw,Cs,CD &c=) 32-,c2G#|$S'* 3i,,
!#z488;8S': -r2   c                       e Zd ZU dZdZee   ed<    ej                         Z
ded<    ej                  d      Zeed<    ej                  d      Zeed	<   d
eddf fdZdddeeef   ddfdZddeeeef      ddfdZ xZS )rZ   a  
    Base model for any data structures that wrap around a REST API endpoint.

    Subclasses can pass a number of keyword arguments to control serialization behavior:

        * ``url=``: format string for building the URL to be used when saving changes to this model.
     _RestfulModel__url_patternr   _apidefault_urlN_url_contextkwargsr   c                 d    |j                  d| j                        | _        t        |           y )Nurl)popra   r(   __init_subclass__r9   rg   r0   s     r1   rk   zRestfulModel.__init_subclass__   s'    "JJuc.?.?@!#r2   r6   r4   c                    || _         || _        	  | j                  j                  di |d| i| _        | j                  r=| j                  j                  d      s!|j                  | j                        | _        yyy# t
        t        f$ rK}g |j                  |j                         D ci c]  \  }}|dk7  s|| nc c}}w c}}|_         d}~ww xY w)zV
        Set a link to the API and build the REST URL used for this resource.
        r*   rQ   Nhttpr   )rb   rf   ra   formatre   KeyErrorAttributeErrorargsr"   
startswith	build_url)r*   r6   r4   exckvs         r1   r[   zRestfulModel._set_api   s     	#	1**11GGG$GDI 99TYY11&9dii0DI :9 .) 	$+MMOJO&1aqM7IAOJCH 	s)   %A? ?C C.C
<C
CCr5   c           	      j   |%| j                   j                  | j                        }t        |       j	                  || j                   | j
                        }| j                  j                  t        |       j                  D ci c]  }||j                  j                  |        c}       yc c}w )zk
        Reload the model's contents from the given object, or by making a GET request to the API.
        Nr3   )	rb   r$   re   rN   r;   rf   __dict__updater!   )r*   r5   copyablerR   s       r1   _reloadzRestfulModel._reload   s     ;))--		*C:&&sDIIt?P?P&Q8<T
8O8OP8OS(##'',,8OP	
Ps   #B0rL   )r<   r=   r>   r?   ra   r   r&   rF   r@   rE   rb   re   rf   r   rk   r   r[   r   r|   rI   rJ   s   @r1   rZ   rZ      s     $&M8C=%&(&&(D%($$$R0D#0,,,T:L#:$ $ $1E 1DcN 1t 1"	
8DcN3 	
t 	
r2   rZ   c                   `    e Zd ZU dZ ej
                  d      Zeed<   e	defd       Z
d	dZy)
CanDeleteModelzD
    Mix-in for RestfulModel that allows a model to be deleted.
    Frc   _deletedr   c                     | j                   S )zb
        Indicates whether the record has been deleted since being returned from the API.
        )r   r*   s    r1   deletedzCanDeleteModel.deleted   s    
 }}r2   Nc                     | j                   st        d      | j                  j                  d| j                          d| _        y)zT
        Delete the record on the server and mark this instance as deleted.
        z%delete() called with no URL specifiedDELETETN)re   RuntimeErrorrb   requestr   r   s    r1   deletezCanDeleteModel.delete   s6     yyFGG		(DII.r2   r   N)r<   r=   r>   r?   r@   rE   r   boolrF   propertyr   r   r   r2   r1   r~   r~      s>     *X))%8Hd8  r2   r~   c                        e Zd ZU dZdZeeee         e	d<   dZ
eeee         e	d<   dZee   e	d<   dZee   e	d<   dZee   e	d	<   d
eddf fdZddZdededdf fdZ xZS )CanUpdateModela  
    Mix-in for RestfulModel that allows a model to be modified and saved.

    Subclasses can pass a number of keyword arguments to control serialization behavior:

        * ``writable=``: field names that should be written to API on ``save()``.
        * ``readonly=``: field names that should not be written to API on ``save()``.
        * ``save_null_values=``: boolean indicating whether ``save()`` should write nulls (default: true)
    N_CanUpdateModel__writable_CanUpdateModel__readonlyT_CanUpdateModel__save_nonePATCH!_CanUpdateModel__save_http_method"_CanUpdateModel__reload_after_saverg   r   c                    d|v rd|v rt        d      |j                  d| j                        | _        |j                  d| j                        | _        t	        |j                  d| j
                              | _        |j                  d| j                        | _        t	        |j                  d| j                              | _        | j                  r/t        | ddj                  d	 | j                  D              z          | j                  r/t        | d
dj                  d | j                  D              z          t        | ,  di | y )Nwritablereadonlyz-incompatible kwargs 'writable' and 'readonly'save_null_valuessave_methodreload_after_savez0The following fields can be modified and saved: z, c              3   (   K   | ]
  }d | d   ywz``Nr   .0fields     r1   	<genexpr>z3CanUpdateModel.__init_subclass__.<locals>.<genexpr>        GubrN   z;The following fields are read-only and cannot be modified:
c              3   (   K   | ]
  }d | d   ywr   r   r   s     r1   r   z3CanUpdateModel.__init_subclass__.<locals>.<genexpr>   r   r   r   )
ValueErrorrj   r   r   r   r   r   r   r   joinr(   rk   rl   s     r1   rk   z CanUpdateModel.__init_subclass__   s   J&$8LMMJ?J?vzz*<cooNO!'M3;Q;Q!R"&JJ*C,C,CD#
 >>"B))GGGH
 >>"N))GGGH
 	!+F+r2   c                 <   t        | dd      rt        d      | j                  st        d      | j                  rt	        | j                        nd}| j
                  rt	        | j
                        nd}| j                  d||| j                         }|D ]2  }t        |j                  |      x}t              s%t        |      ||<   4 | j                  j                  | j                  | j                  |      }| j                  r| j!                  |       yy)z
        Save any changes made to the instance's writable fields and update the
        instance with any refreshed values returned from the API.

        Will raise ``RuntimeError`` if the record has been deleted.
        r   Nzsave() called after delete()z#save() called with no URL specifiedT)by_aliasincludeexcludeexclude_none)json)r\   r   re   r   rU   r   
model_dumpr   r%   r$   r   r   rb   r   r   r   r|   )r*   r   r   r   rR   r/   responses          r1   savezCanUpdateModel.save   s     4T*=>>yyDEE*.//#doo&t*.//#doo&t"...	  
 C488C=0%(;/6S	  99$$T%<%<diid$S##LL" $r2   r.   r/   c                     || j                   j                  v rJ| j                  r|| j                  v rt        |      | j                  || j                  vrt        |      t
        |   ||       y rL   )r0   r!   r   rq   r   r(   __setattr__)r*   r.   r/   r0   s      r1   r   zCanUpdateModel.__setattr__  sa    4>>...44??#:$T***t4??/J$T**D%(r2   r   )r<   r=   r>   r?   r   r   r   r	   r&   rF   r   r   r   r   r   r   rk   r   r   rI   rJ   s   @r1   r   r      s     59J(3-01848J(3-018"&K$&(//*.$., , ,0#6) )C )D ) )r2   r   memoc                 d   |
t               n|}t        | t              rNt        |       |v ry|j	                  t        |              | j                          t        t        |       |      S | j                         D ]1  }t        |t              st        |t              s%t        ||       3 y)a  
    Convenience method to ensure we update forward references for all nested models.

    Any time a type annotation refers to a nested class that isn't present
    at the time the attribute is created, we need to tell pydantic to
    update forward references after all the referenced models exist.

    Only intended for use within pyAirtable, like:

        >>> from pyairtable.models._base import AirtableModel, update_forward_refs
        >>> class A(AirtableModel): ...
        >>> class B(AirtableModel): ...
        ...     class B_One(AirtableModel): ...
        ...     class B_Two(AirtableModel): ...
        >>> update_forward_refs(vars())
    N)r   )rU   r%   rN   rV   rW   model_rebuildrebuild_modelsvarsvalues
issubclassr   )r5   r   r/   s      r1   r   r   !  s    ( L35dD #tc7d?Cd3id33eT"z%'G5t, r2   rL   )#r   	functoolsr   typingr   r   r   r   r	   r
   r   r   r   r   rB   r@   typing_extensionsr   rH   pyairtable.utilsr   r   r   pyairtable.api.apir   	BaseModelr   r&   rO   r8   rZ   r~   r   intr   r   r2   r1   <module>r      s          .  &3H&& 3l5s 5s 5 "	/;	/;	/; c]	/;
 
/;d-
= -
`\ 0L)\ L)b  $ -	tM"GCH$55	6 -
3s8
 - 
 -r2   