
    h=                         d dl Z d dlmZ d dlmZ d dlmZmZ dZd Z	d Z
 G d d	e      Z G d
 de      Z G d de      Z G d d      Z G d de      Zy)    N)DBRef)_import_class)DoesNotExistMultipleObjectsReturned)BaseDict
StrictDictBaseListEmbeddedDocumentListLazyReferencec                       fd}|S )z;Decorator that ensures _mark_as_changed method gets called.c                 @     | g|i |}| j                          |S N_mark_as_changed)selfargskwargsresultparent_methods       ]/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/mongoengine/base/datastructures.pywrapperz(mark_as_changed_wrapper.<locals>.wrapper   s(    t5d5f5     r   r   s   ` r   mark_as_changed_wrapperr      s     Nr   c                       fd}|S )zPDecorator that ensures _mark_as_changed method gets called with the key argumentc                 b    |r|| vs| |   |d   k7  r| j                  |        | |g|i |S )Nr   r   )r   keyr   r   r   s       r   r   z,mark_key_as_changed_wrapper.<locals>.wrapper    s@    s$$s)tAw*>!!#&T38888r   r   r   s   ` r   mark_key_as_changed_wrapperr      s    9 Nr   c                   t    e Zd ZdZdZdZdZ fdZd
dZ fdZ	d Z
d Z eej                        Z eej                        Z eej                         Z eej$                        Z eej&                        Z eej(                        Z eej*                        Z eej,                        Zd
d	Z xZS )r   z+A special dict so we can watch any changes.FNc                     t        d      }t        ||      rt        j                  |      | _        || _        t        |   |       y NBaseDocumentr   
isinstanceweakrefproxy	_instance_namesuper__init__)r   
dict_itemsinstancenamer#   	__class__s        r   r+   zBaseDict.__init__0   ;    $^4h-$]]84DN
$r   c                 H    	 | j                  |      S # t        $ r |cY S w xY wr   )__getitem__KeyErrorr   r   defaults      r   getzBaseDict.get8   s,    	##C(( 	N	s    !!c                 
   t         |   |      }t        d      }t        ||      r|j                  | j                  |_        |S t        |t
              rOt        |t              s?t        |d | j                   d|       }t         | !  ||       | j                  |_        |S t        |t              rMt        |t              s=t        |d | j                   d|       }t         | !  ||       | j                  |_        |S )NEmbeddedDocument.)r*   r2   r   r%   r(   dictr   r)   __setitem__listr	   r   r   valuer8   r/   s       r   r2   zBaseDict.__getitem__?   s    #C(();<e-.5??3J"nnEO  t$Zx-HUDTZZL#*?@EGU+"nnEO
 	 t$Zx-HUDTZZL#*?@EGU+"nnEOr   c                 "    d | _         d| _        | S NFr-   _dereferencedr   s    r   __getstate__zBaseDict.__getstate__O       "r   c                 
    |} | S r   r   r   states     r   __setstate__zBaseDict.__setstate__T       r   c                     t        | j                  d      rS|r+| j                  j                  | j                   d|        y | j                  j                  | j                         y y Nr   r9   )hasattrr(   r   r)   r   r   s     r   r   zBaseDict._mark_as_changeda   sP    4>>#56//4::,au0EF//

;	 7r   r   )__name__
__module____qualname____doc__rB   r(   r)   r+   r6   r2   rD   rI   r   r:   r;   __delattr____delitem__r   popclearupdatepopitem
setdefaultr   __classcell__r/   s   @r   r   r   )   s    5MIE% 
 .d.>.>?K-d.>.>?K-d.>.>?K
!$((
+C#DJJ/E$T[[1F%dll3G(9J<r   r   c                       e Zd ZdZdZdZdZ fdZ fdZ fdZ	d Z
d Z fd	Z eej                        Z eej                         Z eej"                        Z eej$                        Z eej&                        Z eej(                        Z eej*                        Z eej,                        Z eej.                        Z eej0                        Zdd
Z xZS )r	   z+A special list so we can watch any changes.FNc                     t        d      }t        ||      rt        j                  |      | _        || _        t        |   |       y r"   r$   )r   
list_itemsr-   r.   r#   r/   s        r   r+   zBaseList.__init__p   r0   r   c                 t   t        |t              r|dk  rt        |       |z   }t        |   |      }t        |t
              r|S t        d      }t        ||      r|j                  | j                  |_        |S t        |t              rOt        |t              s?t        |d | j                   d|       }t        | -  ||       | j                  |_        |S t        |t              rMt        |t              s=t        |d | j                   d|       }t        | -  ||       | j                  |_        |S )Nr   r8   r9   )r%   intlenr*   r2   slicer   r(   r:   r   r)   r;   r<   r	   r=   s       r   r2   zBaseList.__getitem__x   s   c3C!Gd)c/C#C(c5! L();<e-.5??3J"nnEO  t$Zx-HUDTZZL#*?@EGU+"nnEO  t$Zx-HUDTZZL#*?@EGU+"nnEOr   c              #   >   K   t         |          E d {    y 7 wr   )r*   __iter__)r   r/   s    r   rd   zBaseList.__iter__   s     7#%%%s   c                 "    d | _         d| _        | S r@   rA   rC   s    r   rD   zBaseList.__getstate__   rE   r   c                 
    |} | S r   r   rG   s     r   rI   zBaseList.__setstate__   rJ   r   c                 r    |}t        |t              rd }t        |   ||      }| j	                  |       |S r   )r%   rb   r*   r;   r   )r   r   r>   changed_keyr   r/   s        r   r;   zBaseList.__setitem__   s=    c5! K$S%0k*r   c                     t        | j                  d      r_|7| j                  j                  | j                   d|t	        |       z          y | j                  j                  | j                         y y rL   )rM   r(   r   r)   ra   rN   s     r   r   zBaseList._mark_as_changed   s[    4>>#56//4::,ac$i?P0QR//

;	 7r   r   )rO   rP   rQ   rR   rB   r(   r)   r+   r2   rd   rD   rI   r;   r   r<   appendextendinsertrU   removereversesortrT   __iadd____imul__r   rZ   r[   s   @r   r	   r	   i   s    5MIE%4&
	 %T[[1F$T[[1F$T[[1F
!$((
+C$T[[1F%dll3G"499-D)$*:*:;K&t}}5H&t}}5H<r   r	   c                   t     e Zd Z fdZed        Zed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Z xZS )r
   c                 6    t         |   |||       || _        y r   )r*   r+   r(   )r   r^   r-   r.   r/   s       r   r+   zEmbeddedDocumentList.__init__   s    Xt4!r   c                 x    |j                         D ]'  \  }}t        ||      }||k7  st        |      |k7  s' y y)zoReturn True if a given embedded doc matches all the filter
        kwargs. If it doesn't return False.
        FT)itemsgetattrstr)clsembedded_docr   r   expected_valuedoc_vals         r   __match_allz EmbeddedDocumentList.__match_all   sE    
 $*<<> 	ClC0G.(S\^-K	 r   c                 Z    |s|S |D cg c]  }| j                  ||      s| c}S c c}w )z2Return embedded docs that match the filter kwargs.) _EmbeddedDocumentList__match_all)rx   embedded_docsr   docs       r   __only_matchesz#EmbeddedDocumentList.__only_matches   s.       ,MV0LMMMs   ((c                 h    | j                  | |      }t        || j                  | j                        S )a  
        Filters the list by only including embedded documents with the
        given keyword arguments.

        This method only supports simple comparison (e.g. .filter(name='John Doe'))
        and does not support operators like __gte, __lte, __icontains like queryset.filter does

        :param kwargs: The keyword arguments corresponding to the fields to
         filter on. *Multiple arguments are treated as if they are ANDed
         together.*
        :return: A new ``EmbeddedDocumentList`` containing the matching
         embedded documents.

        Raises ``AttributeError`` if a given keyword is not a valid field for
        the embedded document class.
        #_EmbeddedDocumentList__only_matchesr
   r(   r)   r   r   valuess      r   filterzEmbeddedDocumentList.filter   s-    " $$T62#FDNNDJJGGr   c                     | j                  | |      }| D cg c]	  }||vs| }}t        || j                  | j                        S c c}w )a  
        Filters the list by excluding embedded documents with the given
        keyword arguments.

        :param kwargs: The keyword arguments corresponding to the fields to
         exclude on. *Multiple arguments are treated as if they are ANDed
         together.*
        :return: A new ``EmbeddedDocumentList`` containing the non-matching
         embedded documents.

        Raises ``AttributeError`` if a given keyword is not a valid field for
        the embedded document class.
        r   )r   r   excludeitemr   s        r   r   zEmbeddedDocumentList.exclude   sM     %%dF3#'?44w+>$??#FDNNDJJGG @s
   	AAc                     t        |       S )z
        The number of embedded documents in the list.

        :return: The length of the list, equivalent to the result of ``len()``.
        ra   rC   s    r   countzEmbeddedDocumentList.count   s     4yr   c                     | j                  | |      }t        |      dk(  rt        d| j                  z        t        |      dkD  rt	        dt        |      z        |d   S )a  
        Retrieves an embedded document determined by the given keyword
        arguments.

        :param kwargs: The keyword arguments corresponding to the fields to
         search on. *Multiple arguments are treated as if they are ANDed
         together.*
        :return: The embedded document matched by the given keyword arguments.

        Raises ``DoesNotExist`` if the arguments used to query an embedded
        document returns no results. ``MultipleObjectsReturned`` if more
        than one result is returned.
        r   z!%s matching query does not exist.   z%d items returned, instead of 1)r   ra   r   r)   r   r   s      r   r6   zEmbeddedDocumentList.get  sg     $$T62v;!BTZZOPP[1_)1CK?  ayr   c                 *    t        |       dkD  r| d   S y)zVReturn the first embedded document in the list, or ``None``
        if empty.
        r   Nr   rC   s    r   firstzEmbeddedDocumentList.first  s     t9q=7N r   c                    | j                   }| j                  j                  |   j                  j                  }| j                  | j                      j                   |di |       | j                  | j                      d   S )a  
        Creates a new instance of the EmbeddedDocument and appends it to this EmbeddedDocumentList.

        .. note::
            the instance of the EmbeddedDocument is not automatically saved to the database.
            You still need to call .save() on the parent Document.

        :param values: A dictionary of values for the embedded document.
        :return: The new embedded document instance.
        r   )r)   r(   _fieldsfielddocument_type_objrj   )r   r   r.   EmbeddedClasss       r   createzEmbeddedDocumentList.create   sh     zz..t4::LLtzz"))-*A&*AB~~djj)"--r   c                 <     | j                   j                  |i | y)z
        Saves the ancestor document.

        :param args: Arguments passed up to the ancestor Document's save
         method.
        :param kwargs: Keyword arguments passed up to the ancestor Document's
         save method.
        N)r(   save)r   r   r   s      r   r   zEmbeddedDocumentList.save1  s     	T,V,r   c                     t        |       }|D ]*  }| j                  | j                     j                  |       , t	        |      S )a  
        Deletes the embedded documents from the database.

        .. note::
            The embedded document changes are not automatically saved
            to the database after calling this method.

        :return: The number of entries deleted.
        )r<   r(   r)   rm   ra   )r   r   r   s      r   deletezEmbeddedDocumentList.delete<  sC     d 	4DNN4::&--d3	4 6{r   c                     t        |      dk(  ryt        |       }|D ]'  }|j                         D ]  \  }}t        |||        ) t        |      S )a  
        Updates the embedded documents with the given replacement values. This
        function does not support mongoDB update operators such as ``inc__``.

        .. note::
            The embedded document changes are not automatically saved
            to the database after calling this method.

        :param update: A dictionary of update values to apply to each
         embedded document.
        :return: The number of entries updated.
        r   )ra   r<   ru   setattr)r   rW   r   r   kvs         r   rW   zEmbeddedDocumentList.updateL  s\     v;!d 	$D $1a#$	$ 6{r   )rO   rP   rQ   r+   classmethodr~   r   r   r   r   r6   r   r   r   r   rW   rZ   r[   s   @r   r
   r
      sa    "   N NH(H$0."	- r   r
   c                       e Zd ZdZh dZi Zd Zd Zd Zd Z	ddZ
dd	Zd
 Zd Zd Zd Zd Zd Zd Zd Zed        Zy)r   r   >   r6   rU   keysru   r   	iteritemsc                 N    |j                         D ]  \  }}t        | ||        y r   )ru   r   )r   r   r   r   s       r   r+   zStrictDict.__init__h  s'    LLN 	 DAqD!Q	 r   c                 v    || j                   v rd|z   n|}	 t        | |      S # t        $ r t        |      w xY wN
_reserved_)_special_fieldsrv   AttributeErrorr3   rN   s     r   r2   zStrictDict.__getitem__l  sF    $'4+?+?$?lS S	 4%% 	 3-	 s   # 8c                 F    || j                   v rd|z   n|}t        | ||      S r   )r   r   )r   r   r>   s      r   r;   zStrictDict.__setitem__s  s*    $'4+?+?$?lS StS%((r   c                     t        | |      S r   rM   rN   s     r   __contains__zStrictDict.__contains__w  s    tS!!r   Nc                 0    	 | |   S # t         $ r |cY S w xY wr   )r3   r4   s      r   r6   zStrictDict.getz  s%    	9 	N	s    c                 d    | j                  ||      }	 t        | |       |S # t        $ r Y |S w xY wr   )r6   delattrr   )r   r   r5   r   s       r   rU   zStrictDict.pop  sA    HHS'"	D#   		s   " 	//c              #   ,   K   | D ]  }|| |   f  y wr   r   rN   s     r   r   zStrictDict.iteritems  s#      	!CtCy. 	!s   c                 H    t        |       D cg c]	  }|| |   f c}S c c}w r   iter)r   r   s     r   ru   zStrictDict.items  s"    &*4j1DG111s   c                     t        |       S r   r   rC   s    r   iterkeyszStrictDict.iterkeys  s    Dzr   c                 *    t        t        |             S r   )r<   r   rC   s    r   r   zStrictDict.keys  s    DJr   c                 .      fd j                   D        S )Nc              3   <   K   | ]  }t        |      s|  y wr   r   ).0r   r   s     r   	<genexpr>z&StrictDict.__iter__.<locals>.<genexpr>  s     Ds1CDs   )	__slots__rC   s   `r   rd   zStrictDict.__iter__  s    Dt~~DDr   c                 F    t        t        | j                                     S r   )ra   r<   ru   rC   s    r   __len__zStrictDict.__len__  s    4

%&&r   c                 h    t        | j                               t        |j                               k(  S r   )r<   ru   r   others     r   __eq__zStrictDict.__eq__  s#    DJJL!T%++-%888r   c                     | |k(   S r   r   r   s     r   __ne__zStrictDict.__ne__  s    EM""r   c                      t         fd|D              t              }| j                  vr G fdd       }| j                  |<    j                  |   S )Nc              3   H   K   | ]  }|j                   v rd |z   n|  yw)r   N)r   )r   r   rx   s     r   r   z$StrictDict.create.<locals>.<genexpr>  s-      #
FGc&9&9!9\Aq@#
s   "c                       e Zd ZW  Zd Zy)-StrictDict.create.<locals>.SpecificStrictDictc                 T    ddj                  d | j                         D              z  S )Nz{%s}, c              3   2   K   | ]  \  }}d |d|  yw)"z": Nr   )r   r   r   s      r   r   zIStrictDict.create.<locals>.SpecificStrictDict.__repr__.<locals>.<genexpr>  s&      ..2a!A5A5).s   )joinru   rC   s    r   __repr__z6StrictDict.create.<locals>.SpecificStrictDict.__repr__  s-    !DII .6:jjl. %  r   N)rO   rP   rQ   r   r   )allowed_keys_tuples   r   SpecificStrictDictr     s    .	r   r   )tuple	frozenset_classes)rx   allowed_keysr   r   s   `  @r   r   zStrictDict.create  s_    " #
KW#
 
 !!34s||+S  *<CLL&||L))r   r   )rO   rP   rQ   r   r   r   r+   r2   r;   r   r6   rU   r   ru   r   r   rd   r   r   r   r   r   r   r   r   r   r   c  sl    ILOH  )"!2 E'9# * *r   r   c                   N     e Zd ZdZddZed        Zd	 fd	Zd Zd Z	d Z
 xZS )
r   )_cached_docpassthroughdocument_typec                     | j                   r|rO| j                  j                  j                  | j                        | _         | j                   st        d| z        | j                   S )N)pkz)Trying to dereference unknown document %s)r   r   objectsr6   r   r   )r   forces     r   fetchzLazyReference.fetch  sZ    5#1199===ID##"#NRV#WXXr   c                     | j                   S r   )idrC   s    r   r   zLazyReference.pk  s    wwr   c                     || _         || _        || _        t        |   | j                   j                         |       y r   )r   r   r   r*   r+   _get_collection_name)r   r   r   
cached_docr   r/   s        r   r+   zLazyReference.__init__  s9    *%&++@@BBGr   c                 X    | j                   s
t               | j                         }||   S r   )r   r3   r   r   r.   documents      r   r2   zLazyReference.__getitem__  s(    *::<~r   c                     t         j                  | d      s
t               | j                         }	 ||   S # t        $ r t               w xY w)Nr   )object__getattribute__r   r   r3   r   s      r   __getattr__zLazyReference.__getattr__  sM    &&t]; ""::<	#D>! 	# ""	#s	   7 Ac                 <    d| j                    d| j                  dS )Nz<LazyReference(r   z)>)r   r   rC   s    r   r   zLazyReference.__repr__  s"     !3!3 4BtwwkDDr   )Fr@   )rO   rP   rQ   r   r   propertyr   r+   r2   r   r   rZ   r[   s   @r   r   r     s7    ?I   H#Er   r   )r&   bsonr   mongoengine.commonr   mongoengine.errorsr   r   __all__r   r   r:   r   r<   r	   r
   r   r   r   r   r   <module>r      sh      , D		=<t =<@P<t P<fd8 dNO* O*d$EE $Er   