
    hF                         d dl Z d dlZd dlm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mZmZmZ dZ G d d	e      Z G d
 de      Z G d de      Z G d de      Zy)    N)_document_registry)	BaseFieldComplexBaseFieldObjectIdField_import_class)InvalidDocumentError)
DO_NOTHINGDoesNotExistMultipleObjectsReturnedQuerySetManager)DocumentMetaclassTopLevelDocumentMetaclassc                   R     e Zd ZdZ fdZed        Zed        Zed        Z xZ	S )r   zMetaclass for all documents.c           
      d   | j                  |      }t        !|   }|j                  d      }|rt	        |t
              r || |||      S |j                  dd      |d<   g |d<   d|v r|j                  d      |d<   d|vrtt               }|d d d   D ]R  }t        |d      r|j                  |j                         +t        |d      s8|j                  |j                         T ||d<   d|d   d<   |d   j                  d	      rt        d
      }	 |	       |d<   i }
|d d d   D ]  }t        |d      r|
j                  |j                         t        |d      r7i }|j                  j!                         D ]5  \  }}t#        |t$              s||_        |j(                  s||_        |||<   7 |
j                  |        i }|j!                         D ]a  \  }}t#        |t$              s||_        |j(                  s||_        ||
|<   |j                  |j(                  d      dz   ||j(                  <   c |j!                         D cg c]  \  }}|dkD  s| }}}|rddj+                  |      z  }t-        |      |
|d<   |
j!                         D ci c]  \  }}|t/        |d|       c}}|d<   |d   j!                         D ci c]  \  }}||
 c}}|d<   t1        d t3        d |
j5                         D              D              |d<   d}|g}|D ]  }t/        |dd      s8t/        |di       j                  dd      s|j7                  |j8                         t        |d      sU|j                  j                  d	      }|rs|j                  j                  d      rt;        d|j8                  z         |D cg c]  }t        |d      s| }}|r"|d   j<                  }||d   j>                  fz  }dj+                  tA        |            }||d<   ||d<   |f|d<   |d   |d<    || |||      }|D ]7  }||jB                  vr|xjB                  |fz  c_!        |jB                  |_"        9 | jG                         \  }}}}t	        ||      rd |_$        |tJ        |j>                  <   |j                  j5                         D ]  }|}|jL                  ||_&        t/        |d tN              } t#        ||      rXt	        ||      rt-        d!      |jP                  r|jS                          |jT                  jV                  j7                  |       t#        |tX              rzt        |d"      rnt/        |jZ                  d tN              } t#        ||      r;| tN        k7  r2d#|j\                  j8                  d$|j&                  d%}t-        |      |jZ                  }| tN        k7  rMt	        ||      rd&|j&                  z  }t-        |      |jT                  j_                  ||j&                  |        |j&                  s|t        ||j&                        s||ja                         vsd'|j&                  z  }t-        |       |S c c}}w c c}}w c c}}w c c}w )(Nmy_metaclass_is_documentF_cached_reference_fieldsmeta_metaabstractallow_inheritanceStringField_cls_fieldsr      z#Multiple db_fields defined for: %s z, db_field_db_field_map_reverse_db_field_mapc              3   &   K   | ]	  }|d      yw)r   N ).0is     Z/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/mongoengine/base/metaclasses.py	<genexpr>z,DocumentMetaclass.__new__.<locals>.<genexpr>p   s      )
 aD)
s   c              3   L   K   | ]  }|j                   |j                  f  y wN)creation_countername)r#   vs     r%   r&   z,DocumentMetaclass.__new__.<locals>.<genexpr>r   s     VQQ//8Vs   "$_fields_orderedr"   _is_base_clsTzeDocument %s may not be subclassed. To enable inheritance, use the "allow_inheritance" meta attribute._class_name._superclasses_subclasses_typesreverse_delete_rulez9CachedReferenceFields is not allowed in EmbeddedDocumentsfieldz+Reverse delete rules are not supported for z	 (field: )zHReverse delete rules are not supported for EmbeddedDocuments (field: %s)z2%s is a document method and not a valid field name)1
_get_basessuper__new__get
issubclassr   popMetaDicthasattrmerger   r   r   updater   __dict__items
isinstancer   r*   r   joinr	   getattrtuplesortedvaluesappend__name__
ValueErrorr0   r.   reversedr1   r2   _import_classes_collectionr   owner_documentr
   	auto_syncstart_listenerdocument_typer   r   r4   	__class__register_delete_rulemro)"mcsr*   basesattrsflattened_bases	super_new	metaclassr   baser   
doc_fieldsbase_fields	attr_name
attr_valuefield_nameskr+   duplicate_db_fieldsmsgsuperclasses
class_namer   bdocument_basesr   	new_classDocumentEmbeddedDocument	DictFieldCachedReferenceFieldr4   fdelete_rulerR   s"                                    r%   r8   zDocumentMetaclass.__new__   s   ../GO	 IIn-	I/@AS$u55 %		.% @n,.() U?"YYv.E'N %:D'"- +4(JJtyy)T7+JJtzz*+ "E'N  'N
 >12'6K'ME&M
 
#DbD) 	/DtY'!!$,,/ 4) -1]]-@-@-B 8)Iz%j)< &/JO%...7
+-7K	*8 !!+.	/$ %*[[] 	!Izj)4'JO&&&/
#$.Jy! 
 3 3Q7!; 
++,	 .9->->-@JTQAEqJJ7$))DW:XXC&s++ &i5?5E5E5G"
-1QAwq*a(("
o #?399;*
QAqD*
%& $) )
V*BSBSBUVV)
 $
  V
# 	D46wgr@c*d#@$ !!$--0tW% %)JJNN3F$G!(
1K$]--( 	& &5R=8Q!RR)!,::L^A.::<<Lxx,-#m!-o $wm.h c46	 # 	+D4+++  TG+ **DK	+ !	
  i*$(I! 5>9001 &&--/ *	0EA'#, !!%:JGK!12i)9:.S  ;;$$&88??B!-.71g3F%agg/DjQa+z0I 160H0H%**V  /s33KKj(i)9:8:?**E  /s3344Y

KX 

Hejj1$IMMO;JUZZW*3//U*	0X s K"
*
B Ss$   ZZZ!Z'Z-Z-c                 z    t        |t              r|S g | j                  |      }fd|D        }t        |      S )Nc              3   P   K   | ]  }|v rj                  |      r|  y wr(   )rH   )r#   rf   seens     r%   r&   z/DocumentMetaclass._get_bases.<locals>.<genexpr>   s!     Naddkk!nNs   	&&&)rB   
BasesTuple_DocumentMetaclass__get_bases)rU   rV   unique_basesrq   s      @r%   r6   zDocumentMetaclass._get_bases   s:    eZ(L&N5N,''    c              #   ~   K   |D ]2  }|t         u r| | j                  |j                        E d {    4 y 7 wr(   )objectrs   	__bases__)rU   rV   r[   s      r%   __get_baseszDocumentMetaclass.__get_bases   s?      	7Dv~Jt~~666		7 7s   1=;=c                 f    t        d      }t        d      }t        d      }t        d      }||||fS )Nri   rj   rk   rl   r   )rU   ri   rj   rk   rl   s        r%   rL   z!DocumentMetaclass._import_classes   s?     ,();<!+.	,-CD)96JJJru   )
rI   
__module____qualname____doc__r8   classmethodr6   rs   rL   __classcell__rR   s   @r%   r   r      sK    &AF ( ( 7 7 K Kru   r   c                   2     e Zd ZdZ fdZed        Z xZS )r   zjMetaclass for top-level documents (i.e. documents that have their own
    collection in the database.
    c                 
   | j                  |      }t        |   }|j                  d      t        k(  r:dd d g g d dd d d d
|d<   d|d<   |d   j                  |j                  di              n5|j                  di       |d<   |d   j                  dd      |d   d<   d|d<   d|d	<   d
|v r#|d
   }t        |d      r|j                  |d   d<   d|v r|d= |D cg c]  }|j                  t        k(  s| }}|sd n|d   }|rUd|j                  di       v rA|j                  j                  dd      s%dz  }	t        j                  |	t               |d   d= |j                  d      s|d   j                  d      r6|r)|j                  j                  dd      sd}	t        |	       || ||      S t               }
|d d d   D ]  }t        |d      r|
j                  |j                          n't        |d      r|
j                  |j                         t#        |d	d      s`|j                  j                  d      r||
j                  dd       }t%        |      s ||      |
d<    |
j                  |j                  di              t'        d |D              }|s|
d   du r|
d   st        d      d|
vr8dj)                  d D              j+                  d      j-                         |
d<   |
|d<    || ||      }|j                  }
|j/                  |
d         |
d<   |
j                  d      }t%        |      r ||      |j                  d<   d
t1        |      vrt3               |_        |j6                  j9                         D ]X  \  }}|j:                  s|j                  j                  d      }|r||k7  rt        d      |rC||j                  d<   ||_        Z |j                  j                  d      s| j?                  |      \  }}||j                  d<   tA        |      |j6                  |<   ||j6                  |   _!        |j6                  |   |_        ||jD                  |<   ||jF                  |<   |f|jH                  z   |_$        tJ        tL        f}|j                  d      }|D ]C  }|jN                  tQ        fd|D              xs |f}tS        |d|i      }tU        ||       E |S c c}w )Nr   TF)
r   max_documentsmax_sizeorderingindexesid_fieldindex_background
index_optsdelete_rulesr   r   r-   r   r   r   objectsqueryset_classr   
collectionz-Trying to set a collection on a subclass (%s)z/Abstract document cannot have non-abstract baser   c              3   l   K   | ],  }t        |d       s|j                  j                  d       . yw)r   r   N)r=   r   r9   )r#   rf   s     r%   r&   z4TopLevelDocumentMetaclass.__new__.<locals>.<genexpr>U  s+      
()7@SAGGKK
#
s   4 4r   zGOnly direct subclasses of Document may set "allow_inheritance" to False c              3   J   K   | ]  }|j                         rd |z  n|  yw)z_%sN)isupper)r#   cs     r%   r&   z4TopLevelDocumentMetaclass.__new__.<locals>.<genexpr>e  s!     FAQYY[	a7Fs   !#_r   index_specsr   z!Cannot override primary key field)r   r{   c              3   P   K   | ]  }t        |      st        |        y wr(   )r=   rD   )r#   r[   r*   s     r%   r&   z4TopLevelDocumentMetaclass.__new__.<locals>.<genexpr>  s'      (,74QUCVd#s   &&)+r6   r7   r8   r9   r   r?   r=   r   rR   r   warningswarnSyntaxWarningrJ   r<   r>   r   rD   callableallrC   striplower_build_index_specsdirr   r   r   rA   primary_keyidget_auto_id_namesr   r*   r   r    r,   r   r   rI   rE   typesetattr)rU   r*   rV   rW   rX   rY   managerrf   parent_doc_clsrc   r   r[   r   simple_classrh   
field_namer4   
current_pkid_name
id_db_nameexceptions_to_mergemoduleexcparents	exceptionrR   s    `                       r%   r8   z!TopLevelDocumentMetaclass.__new__   sF   ../GO	 99^$(AA !!%  $)" $
 &*E'N  %)E.!'N!!%))FB"78"YYvr2E'N).w););J)NE'N:&$)E.! !%n I&Gw 013:3I3Ig/0 U?f '
!++9R*RA
 
 &49J
 		'2 66"((,,Z>ADHCMM#}-g|, 99^$g(:(::(Fn&:&:&>&>z5&QG o%S$u55 z#DbD) 	:DtV$

499%w'

4::& t^U3DJJNN:<V!XXlD9
J')3D)9D&	: 	

599Wb)*
  
-<
 
 ()U2$/  t#FFFs 
 g c46	 (::4	?K] XXl+
J,6y,AIOOL) C	N* / 1I "+!2!2!8!8!: 
	)J  &__00<
*
":$%HII "2<IOOJ/#(IL
	) "":."%"7"7	"BGZ*1IOOJ')6
)KIg&.5Ig&+$,,W5IL/9I##G,:AI++J7 *1
Y5N5N(NI%  ,-DE<(& 		0C<<D 0?    T7\6,BCIItY/		0 }
s   T5Tc                 F   d\  }}|j                   D ch c]  }| }}|j                   j                         D ch c]  }|j                   }}||vr||vr||fS d\  }}	}
t        j                         D ]  }
| d|
 }|	 d|
 }||vs||vs||fc S  yc c}w c c}w )a  Find a name for the automatic ID field for the given new class.

        Return a two-element tuple where the first item is the field name (i.e.
        the attribute name on the object) and the second element is the DB
        field name (i.e. the name of the key stored in MongoDB).

        Defaults to ('id', '_id'), or generates a non-clashing name in the form
        of ('auto_id_X', '_auto_id_X') if the default name is already taken.
        )r   _id)auto_id_auto_idr   r   N)r   rG   r   	itertoolscount)rU   rh   r   r   r   existing_fieldsr+   existing_db_fieldsid_basenameid_db_basenamer$   s              r%   r   z+TopLevelDocumentMetaclass.get_auto_id_names  s     ,8A8I8IJ*:JJ2;2C2C2J2J2LMQajjMM/)j@R.RJ&&)C&^Q" 	+A$Qqc*G*+1QC0Jo-*DV2V
**		+ KMs
   	BB)rI   r{   r|   r}   r8   r~   r   r   r   s   @r%   r   r      s#    kZ + +ru   r   c                       e Zd ZdZdZd Zy)r<   zOCustom dictionary for meta classes.
    Handles the merging of set indexes
    )r   c                     |j                         D ]1  \  }}|| j                  v r| j                  |g       |z   | |<   -|| |<   3 y r(   )rA   _merge_optionsr9   )selfnew_optionsra   r+   s       r%   r>   zMetaDict.merge  sM    %%' 	DAqD'''((1b/A-QQ		ru   N)rI   r{   r|   r}   r   r>   r"   ru   r%   r<   r<     s     "Nru   r<   c                       e Zd ZdZy)rr   z?Special class to handle introspection of bases tuple in __new__N)rI   r{   r|   r}   r"   ru   r%   rr   rr     s    Iru   rr   )r   r   mongoengine.base.commonr   mongoengine.base.fieldsr   r   r   mongoengine.commonr   mongoengine.errorsr	   mongoengine.querysetr
   r   r   r   __all__r   r   r   dictr<   rE   rr   r"   ru   r%   <module>r      sj      6 
 - 3  =^K ^KBH+ 1 H+Vt 	 	ru   