U
    Lk7g/                     @   s>  d dl Z d dlZd dlmZ 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 d dlmZ d dlmZmZ d dlmZmZmZ d d	lmZmZmZ d d
lmZ d dlm Z m!Z!m"Z"m#Z# dZ$dd Z%G dd de&Z'G dd de
edZ(G dd de
edZ)G dd de)edZ*G dd de(edZ+G dd dZ,dS )    N)DBRef)ReadPreference)signals)BaseDictBaseDocumentBaseListDocumentMetaclassEmbeddedDocumentListTopLevelDocumentMetaclassget_document_import_class)DEFAULT_CONNECTION_NAMEget_db)set_write_concernswitch_collection	switch_db)InvalidDocumentErrorInvalidQueryErrorSaveConditionError)list_collection_names)NotUniqueErrorOperationErrorQuerySet	transform)DocumentEmbeddedDocumentDynamicDocumentDynamicEmbeddedDocumentr   InvalidCollectionErrorr   MapReduceDocumentc                 C   sV   d}t | rNt| d tr$| d }n*t| d ttfrNt | d rN| d d }|dkS )z8Helper function used for ensuring and comparing indexes.Nr   _cls)len
isinstancestrlisttuple)fieldsZfirst_field r(   8/tmp/pip-unpacked-wheel-n1etwkgt/mongoengine/document.pyincludes_cls1   s    
r*   c                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__r(   r(   r(   r)   r   <   s   r   c                       s`   e Zd ZdZdZeZdZ fddZdd Z	dd	 Z
 fd
dZ fddZ fddZ  ZS )r   a  A :class:`~mongoengine.Document` that isn't stored in its own
    collection.  :class:`~mongoengine.EmbeddedDocument`\ s should be used as
    fields on :class:`~mongoengine.Document`\ s through the
    :class:`~mongoengine.EmbeddedDocumentField` field type.

    A :class:`~mongoengine.EmbeddedDocument` subclass may be itself subclassed,
    to create a specialised version of the embedded document that will be
    stored in the same collection. To facilitate this behaviour a `_cls`
    field is added to documents (hidden though the MongoEngine interface).
    To enable this behaviour set :attr:`allow_inheritance` to ``True`` in the
    :attr:`meta` dictionary.
    )	_instanceNc                    s   t  j|| d | _g | _d S N)super__init__r.   _changed_fields)selfargskwargs	__class__r(   r)   r1   Y   s    zEmbeddedDocument.__init__c                 C   s   t || jr| j|jkS dS )NF)r#   r7   _datar3   otherr(   r(   r)   __eq__^   s    zEmbeddedDocument.__eq__c                 C   s   |  | S r/   )r;   r9   r(   r(   r)   __ne__c   s    zEmbeddedDocument.__ne__c                    s   t   }d |d< |S Nr.   )r0   __getstate__)r3   datar6   r(   r)   r>   f   s    
zEmbeddedDocument.__getstate__c                    s   t  | |d | _d S r=   )r0   __setstate__r.   )r3   stater6   r(   r)   r@   k   s    zEmbeddedDocument.__setstate__c                    s,   t  j||}d|kr(|d d kr(|d= |S )N_id)r0   to_mongor3   r4   r5   r?   r6   r(   r)   rC   o   s    zEmbeddedDocument.to_mongo)r+   r,   r-   __doc__	__slots__r   my_metaclass__hash__r1   r;   r<   r>   r@   rC   __classcell__r(   r(   r6   r)   r   @   s   r   )	metaclassc                	       sp  e Zd ZdZeZdZedd Zej	dd Z fddZ
edd	 Zed
d Zedd Zedd Zedd Z fddZdFddZdGddZdd Zdd Zdd  Zd!d" Zd#d$ Zed%d& Zed'd( Zd)d* ZdHd+d,ZdId-d.ZdJd/d0ZdKd2d3Zd4d5 Z d6d7 Z!d8d9 Z"ed:d; Z#ed<d= Z$edLd>d?Z%ed@dA Z&edBdC Z'edDdE Z(  Z)S )Mr   a
  The base class used for defining the structure and properties of
    collections of documents stored in MongoDB. Inherit from this class, and
    add fields as class attributes to define a document's structure.
    Individual documents may then be created by making instances of the
    :class:`~mongoengine.Document` subclass.

    By default, the MongoDB collection used to store documents created using a
    :class:`~mongoengine.Document` subclass will be the name of the subclass
    converted to snake_case. A different collection may be specified by
    providing :attr:`collection` to the :attr:`meta` dictionary in the class
    definition.

    A :class:`~mongoengine.Document` subclass may be itself subclassed, to
    create a specialised version of the document that will be stored in the
    same collection. To facilitate this behaviour a `_cls`
    field is added to documents (hidden though the MongoEngine interface).
    To enable this behaviour set :attr:`allow_inheritance` to ``True`` in the
    :attr:`meta` dictionary.

    A :class:`~mongoengine.Document` may use a **Capped Collection** by
    specifying :attr:`max_documents` and :attr:`max_size` in the :attr:`meta`
    dictionary. :attr:`max_documents` is the maximum number of documents that
    is allowed to be stored in the collection, and :attr:`max_size` is the
    maximum size of the collection in bytes. :attr:`max_size` is rounded up
    to the next multiple of 256 by MongoDB internally and mongoengine before.
    Use also a multiple of 256 to avoid confusions.  If :attr:`max_size` is not
    specified and :attr:`max_documents` is, :attr:`max_size` defaults to
    10485760 bytes (10MB).

    Indexes may be created by specifying :attr:`indexes` in the :attr:`meta`
    dictionary. The value should be a list of field names or tuples of field
    names. Index direction may be specified by prefixing the field names with
    a **+** or **-** sign.

    Automatic index creation can be disabled by specifying
    :attr:`auto_create_index` in the :attr:`meta` dictionary. If this is set to
    False then indexes will not be created by MongoEngine.  This is useful in
    production systems where index creation is performed as part of a
    deployment system.

    By default, _cls will be added to the start of every index (that
    doesn't contain a list) if allow_inheritance is True. This can be
    disabled by either setting cls to False on the specific index or
    by setting index_cls to False on the meta dictionary for the document.

    By default, any extra attribute existing in stored data but not declared
    in your model will raise a :class:`~mongoengine.FieldDoesNotExist` error.
    This can be disabled by setting :attr:`strict` to ``False``
    in the :attr:`meta` dictionary.
    )	__objectsc                 C   s   d| j krdS t| | j d S )zGet the primary key.id_fieldN)_metagetattrr3   r(   r(   r)   pk   s    
zDocument.pkc                 C   s   t | | jd |S )zSet the primary key.rL   )setattrrM   )r3   valuer(   r(   r)   rP      s    c                    s"   | j dkrtt|  S t| j S )zReturn the hash based on the PK of this document. If it's new
        and doesn't have a PK yet, return the default object hash instead.
        N)rP   r0   r   rH   hashrO   r6   r(   r)   rH      s    
zDocument.__hash__c                 C   s   t | jdtS )zSome Model using other db_aliasdb_alias)r   rM   getr   clsr(   r(   r)   _get_db   s    zDocument._get_dbc                 C   s
   d| _ dS )z?Detach the Document class from the (cached) database collectionN)_collectionrV   r(   r(   r)   _disconnect   s    zDocument._disconnectc                 C   s   t | dr| jdkr| jds,| jdr8|  | _n2| jdrP|  | _n|  }|  }|| | _|  }| jddr|jj	r| 
  | jS )ae  Return the PyMongo collection corresponding to this document.

        Upon first call, this method:
        1. Initializes a :class:`~pymongo.collection.Collection` corresponding
           to this document.
        2. Creates indexes defined in this document's :attr:`meta` dictionary.
           This happens only if `auto_create_index` is True.
        rY   Nmax_sizemax_documents
timeseriesZauto_create_indexT)hasattrrY   rM   rU   _get_capped_collection_get_timeseries_collectionrX   _get_collection_nameclientZ
is_primaryensure_indexes)rW   dbcollection_namer(   r(   r)   _get_collection   s    

zDocument._get_collectionc                 C   s   |   }|  }| jdpd}| jd}|d rD|d d d }|t|ddkr|| }| }|d|ks|d	|krtd
| j|S d|d}|r||d< |j	|f|S )z:Create a new or get an existing capped PyMongo collection.r[   i   r\         TZinclude_system_collectionsmaxsizezICannot create collection "{}" as a capped collection as it already exists)Zcappedrk   )
rX   ra   rM   rU   r   optionsr   formatrY   create_collection)rW   rd   re   r[   r\   
collectionrl   optsr(   r(   r)   r_      s.     
zDocument._get_capped_collectionc                 C   sf   |   }|  }| jd}|t|ddkr@|| }|  |S d|ddi}|jf ||d|S )z>Create a new or get an existing timeseries PyMongo collection.r]   Tri   ZexpireAfterSecondsN)namer]   )rX   ra   rM   rU   r   rl   poprn   )rW   rd   re   Ztimeseries_optsro   rp   r(   r(   r)   r`     s"     z#Document._get_timeseries_collectionc                    sD   t  j||}|d d kr@| jdd kr2|d= n| jd |d< |S )NrB   id)r0   rC   r8   rU   rD   r6   r(   r)   rC   )  s    zDocument.to_mongoNc              	   K   s   |dkri }| j dkrtd| jd }t|tr:| n|| }||krX| j ||< n|| | j krntd|| j	 | j
f |jf ddi|}|dkrdS | jD ]}t| || |||  q|j| _d| _dS )a4  Perform an atomic update of the document in the database and reload
        the document object using updated version.

        Returns True if the document has been updated or False if the document
        in the database doesn't match the query.

        .. note:: All unsaved changes that have been made to the document are
            rejected if the method returns True.

        :param query: the update will be performed only if the document in the
            database matches the query
        :param update: Django-style update keyword arguments
        Nz)The document does not have a primary key.rL   zAInvalid document modify query: it must modify only this document.newTF)rP   r   rM   r#   dictcopyZto_queryr   update_object_key_qsmodifyZ_fields_orderedrQ   _reloadr2   _created)r3   queryrw   rL   updatedfieldr(   r(   r)   rz   6  s*    


zDocument.modifyFTc
              
   K   sD  |	pi }	| j drtdtjj| jfd| i|	 |rF| j|d |dkrRi }| j| j d gd}d|kpv| j	pv|}tj
j| jf| |d	|	 |  }| jdkr|  }n| j d
dr|   z|r| j|||d}n| |||\}}|dkr| j ddp|dk	}|rJ||||d}
|r6|
| ||
d< | jf |
 W n tjjk
r } zd}t|| W 5 d}~X Y nZ tjjk
r } z6d}tdt|rd}t|| t|| W 5 d}~X Y nX | j d }|s|| j dg kr| j| || |< tjj| jf| |d	|	 |   d| _	| S )a	  Save the :class:`~mongoengine.Document` to the database. If the
        document already exists, it will be updated, otherwise it will be
        created. Returns the saved object instance.

        :param force_insert: only try to create a new document, don't allow
            updates of existing documents.
        :param validate: validates the document; set to ``False`` to skip.
        :param clean: call the document clean method, requires `validate` to be
            True.
        :param write_concern: Extra keyword arguments are passed down to
            :meth:`~pymongo.collection.Collection.save` OR
            :meth:`~pymongo.collection.Collection.insert`
            which will be used as options for the resultant
            ``getLastError`` command.  For example,
            ``save(..., write_concern={w: 2, fsync: True}, ...)`` will
            wait until at least two servers have recorded the write and
            will force an fsync on the primary server.
        :param cascade: Sets the flag for cascading saves.  You can set a
            default by setting "cascade" in the document __meta__
        :param cascade_kwargs: (optional) kwargs dictionary to be passed throw
            to cascading saves.  Implies ``cascade=True``.
        :param _refs: A list of processed references used in cascading saves
        :param save_condition: only perform save if matching record in db
            satisfies condition(s) (e.g. version number).
            Raises :class:`OperationError` if the conditions are not satisfied
        :param signal_kwargs: (optional) kwargs dictionary to be passed to
            the signal calls.

        .. versionchanged:: 0.5
            In existing documents it only saves changed fields using
            set / unset.  Saves are cascaded and any
            :class:`~bson.dbref.DBRef` objects that have changes are
            saved as well.
        .. versionchanged:: 0.6
            Added cascading saves
        .. versionchanged:: 0.8
            Cascade saves are optional and default to False.  If you want
            fine grain control then you can turn off using document
            meta['cascade'] = True.  Also you can pass different kwargs to
            the cascade save using cascade_kwargs which overwrites the
            existing kwargs with custom values.
        .. versionchanged:: 0.26
           save() no longer calls :meth:`~mongoengine.Document.ensure_indexes`
           unless ``meta['auto_create_index_on_save']`` is set to True.

        abstractz!Cannot save an abstract document.document)cleanNrL   )r'   rB   )r   createdZauto_create_index_on_saveF)docforce_insertwrite_concerncascade)r   validater   r   _refsz(Tried to save duplicate unique keys (%s)zCould not save document (%s)z^E1100[01] duplicate key	shard_key)rM   rU   r   r   Zpre_savesendr7   r   rC   r|   Zpre_save_post_validationrY   rf   rc   _save_create_save_updaterw   cascade_savepymongoerrorsZDuplicateKeyErrorr   OperationFailurerematchr$   r   _fieldsZ	to_pythonZ	post_saveZ_clear_changed_fields)r3   r   r   r   r   r   Zcascade_kwargsr   save_conditionsignal_kwargsr5   Zdoc_idr   r   _	object_iderrmessagerL   r(   r(   r)   savec  s    ; 

    


 zDocument.savec           	   
   C   s   |   }t||r}|r0||jW  5 Q R  S d|krtd|d i}| ||}|||}|rt|d W  5 Q R  S ||j}W 5 Q R X |S )zXSave a new document.

        Helper method, should only be used inside save().
        rB   )rf   r   Z
insert_oneZinserted_id_integrate_shard_keyZfind_one_and_replace)	r3   r   r   r   ro   wc_collectionselect_dictZ
raw_objectr   r(   r(   r)   r     s    zDocument._save_createc                 C   s,   |   \}}i }|r||d< |r(||d< |S )zReturn a dict containing all the $set and $unset operations
        that should be sent to MongoDB based on the changes made to this
        Document.
        z$setz$unset)Z_delta)r3   ZupdatesZremovals
update_docr(   r(   r)   _get_update_doc  s    zDocument._get_update_docc           	      C   s`   | j dt }|D ]F}| |d}dd |D }|}|D ]}|| }q>||d|< q|S )zIntegrates the collection's shard key to the `select_dict`, which will be used for the query.
        The value from the shard key is taken from the `doc` and finally the select_dict is returned.
        r   .c                 S   s   g | ]
}|j qS r(   )Zdb_field).0pr(   r(   r)   
<listcomp>  s     z1Document._integrate_shard_key.<locals>.<listcomp>)rM   rU   r&   Z_lookup_fieldsplitjoin)	r3   r   r   r   kpathZ
actual_keyvalZakr(   r(   r)   r     s    
zDocument._integrate_shard_keyc              	   C   s   |   }|d }d}i }|dk	r0tj| jf|}||d< | ||}|  }|r|dk}	t||}
|
j|||	dj}W 5 Q R X |	s|d dkrt	d|dk	r|
d}|dkrd	}||fS )
z`Update an existing document.

        Helper method, should only be used inside save().
        rB   FN)upsertnr   z2Race condition preventing document update detectedZupdatedExistingT)rf   r   r}   r7   r   r   r   
update_oneZ
raw_resultr   rU   )r3   r   r   r   ro   r   r   r   r   r   r   Z
last_errorZupdated_existingr(   r(   r)   r   '  s4      
zDocument._save_updatec           	      K   s   | dpg }td}td}| j D ]\}}t|||fs@q(| j |}|r(t|tr\q(t|ddsjq(|jj	 dt
|j }|r(||kr(|| ||d< |jf | g |_q(dS )zXRecursively save any references and generic references on the
        document.
        r   ReferenceFieldGenericReferenceFieldr2   T,N)rU   r   r   itemsr#   r8   r   rN   r7   r+   r$   appendr   r2   )	r3   r5   r   r   r   rq   rW   refZref_idr(   r(   r)   r   M  s"    
zDocument.cascade_savec                 C   s0   t | ds*| jdt}|| j|  | _| jS )z;Return the default queryset corresponding to this document.rK   queryset_class)r^   rM   rU   r   r7   rf   _Document__objects)r3   r   r(   r(   r)   ry   h  s    
zDocument._qsc                 C   sZ   d| j i}| jjdt }|D ]4}| }|d}|D ]}t||}q6||d|< q |S )a  Return a query dict that can be used to fetch this document.

        Most of the time the dict is a simple PK lookup, but in case of
        a sharded collection with a compound shard key, it can contain a more
        complex query.

        Note that the dict returned by this method uses MongoEngine field
        names instead of PyMongo field names (e.g. "pk" instead of "_id",
        "some__nested__field" instead of "some.nested.field", etc.).
        rP   r   r   __)rP   r7   rM   rU   r&   r   rN   r   )r3   r   r   r   r   Zfield_partspartr(   r(   r)   rx   p  s    

zDocument._object_keyc                 K   sb   | j dkrJ|ddrB|  }d|kr,|d= | jjf |jf |S td| jjf | jjf |S )zPerforms an update on the :class:`~mongoengine.Document`
        A convenience wrapper to :meth:`~mongoengine.QuerySet.update`.

        Raises :class:`OperationError` if called on an object that has not yet
        been saved.
        Nr   Fr!   z*attempt to update a document not yet saved)rP   rU   rC   ry   filterr   r   rx   )r3   r5   r}   r(   r(   r)   rw     s    
zDocument.updatec              
   K   s   |pi }t jj| jfd| i| td}| j D ] \}}t||r4t| |	  q4z| j
jf | jj	|dd W n8 tjjk
r } zd|j }t|W 5 d}~X Y nX t jj| jfd| i| dS )aW  Delete the :class:`~mongoengine.Document` from the database. This
        will only take effect if the document has been previously saved.

        :param signal_kwargs: (optional) kwargs dictionary to be passed to
            the signal calls.
        :param write_concern: Extra keyword arguments are passed down which
            will be used as options for the resultant ``getLastError`` command.
            For example, ``save(..., w: 2, fsync: True)`` will
            wait until at least two servers have recorded the write and
            will force an fsync on the primary server.
        r   	FileFieldT)r   Z_from_doc_deletezCould not delete document (%s)N)r   Z
pre_deleter   r7   r   r   r   r#   rN   deletery   r   rx   r   r   r   r4   r   Zpost_delete)r3   r   r   r   rq   r   r   r   r(   r(   r)   r     s    
 

zDocument.deletec              	      sn   t | j|}|  | W 5 Q R X  fdd| _fdd| _ | _|sRdn| j| _| j| _ | j_| S )aT  
        Temporarily switch the database for a document instance.

        Only really useful for archiving off data and calling `save()`::

            user = User.objects.get(id=user_id)
            user.switch_db('archive-db')
            user.save()

        :param str db_alias: The database alias to use for saving the document

        :param bool keep_created: keep self._created value after switching db, else is reset to True


        .. seealso::
            Use :class:`~mongoengine.context_managers.switch_collection`
            if you need to read from another collection
        c                      s    S r/   r(   r(   ro   r(   r)   <lambda>      z$Document.switch_db.<locals>.<lambda>c                      s    S r/   r(   r(   )rd   r(   r)   r     r   T)	r   r7   rf   rX   rY   r|   ry   r   _collection_obj)r3   rT   keep_createdrW   r(   )ro   rd   r)   r     s    zDocument.switch_dbc              	      sX   t | j|}|  W 5 Q R X  fdd| _ | _|s<dn| j| _| j| _ | j_| S )an  
        Temporarily switch the collection for a document instance.

        Only really useful for archiving off data and calling `save()`::

            user = User.objects.get(id=user_id)
            user.switch_collection('old-users')
            user.save()

        :param str collection_name: The database alias to use for saving the
            document

        :param bool keep_created: keep self._created value after switching collection, else is reset to True


        .. seealso::
            Use :class:`~mongoengine.context_managers.switch_db`
            if you need to read from another database
        c                      s    S r/   r(   r(   r   r(   r)   r     r   z,Document.switch_collection.<locals>.<lambda>T)r   r7   rf   rY   r|   ry   r   r   )r3   re   r   rW   r(   r   r)   r     s    zDocument.switch_collectionrh   c                 C   s   t d}| | g|d  | S )zHandles dereferencing of :class:`~bson.dbref.DBRef` objects to
        a maximum depth in order to cut down the number queries to mongodb.
        DeReferencerh   r   )r3   	max_depthr   r(   r(   r)   select_related  s    zDocument.select_relatedc                 O   sT  d}|r,t |d tr,|d }|dd }nd|kr<|d }| jdkrP| d| jtjjf | j	j
| dj|d}|r|d }n
| d|jD ]}|r||krzt| || |||  W q ttfk
r"   z t| || ||j| W n  tk
r   t| | Y nX Y qX q|rBtt| jt| n|j| _d| _| S )zReloads all attributes from the database.

        :param fields: (optional) args list of fields to reload
        :param max_depth: (optional) depth of dereferencing to follow
        rh   r   Nr   zDocument does not exist)r   F)r#   intrP   ZDoesNotExistry   Zread_preferencer   ZPRIMARYr   rx   onlylimitr   r8   rQ   r{   KeyErrorAttributeErrorrU   delattrr%   setr2   r|   )r3   r'   r5   r   objr   r(   r(   r)   reload  sH    




 zDocument.reloadc                    s   t |tr.fdd| D }t| }nrt |trZ fdd|D }t| }nFt |tr fdd|D }t| }nt |ttfrd|_g |_|S )zoUsed by :meth:`~mongoengine.Document.reload` to ensure the
        correct instance is linked to self.
        c                    s    g | ]\}}|  ||fqS r(   r{   )r   r   vrO   r(   r)   r   2  s     z$Document._reload.<locals>.<listcomp>c                    s   g | ]}  |qS r(   r   r   r   keyr3   r(   r)   r   5  s     c                    s   g | ]}  |qS r(   r   r   r   r(   r)   r   8  s     N)	r#   r   r   r	   r   r   r   r.   r2   )r3   r   rR   r(   r   r)   r{   -  s    


zDocument._reloadc                 C   s(   | j dkrd}t|t| j | j S )zVReturns an instance of :class:`~bson.dbref.DBRef` useful in
        `__raw__` queries.Nz+Only saved documents can have a valid dbref)rP   r   r   r7   ra   )r3   msgr(   r(   r)   to_dbref?  s    
zDocument.to_dbrefc                    sr    fdd j D  g }fddj D g }|D ]4}|D ]*|jdpRi }|||f< ||jd< q@q8dS )z[This method registers the delete rules to apply when removing this
        object.
        c                    s   g | ]}| j krt|qS r(   r+   r   r   
class_namerV   r(   r)   r   L  s   
z1Document.register_delete_rule.<locals>.<listcomp>c                    s   g | ]}| j krt|qS r(   r   r   )document_clsr(   r)   r   Q  s   
delete_rulesN)Z_subclassesrM   rU   )rW   r   
field_nameZruleclassesZ	documentsklassr   r(   )rW   r   r)   register_delete_ruleG  s    

zDocument.register_delete_rulec                 C   s4   |   }|std|  d| _|  }|| dS )zDrops the entire collection associated with this
        :class:`~mongoengine.Document` type from the database.

        Raises :class:`OperationError` if the document has no collection set
        (i.g. if it is `abstract`)
        z8Document %s has no collection defined (is it abstract ?)N)ra   r   rY   rX   drop_collection)rW   Z	coll_namerd   r(   r(   r)   r   ]  s    zDocument.drop_collectionc                 K   s@   |  |}| }|d}||d< || |  j|f|S )a<  Creates the given indexes if required.

        :param keys: a single index key or a list of index keys (to
            construct a multi-field index); keys may be prefixed with a **+**
            or a **-** to determine the index ordering
        :param background: Allows index creation in the background
        r'   
background)Z_build_index_specrv   rr   rw   rf   create_index)rW   keysr   r5   
index_specr'   r(   r(   r)   r   n  s    	


zDocument.create_indexc           
      C   s   | j dd}| j dpi }| j dd}|  }d}| j d r| j d }|D ]X}| }|d}|ppt|}| }	|	| d|	kr|	d= |j|fd	|i|	 qP|r|s| j d
rd|kr|d= |jdd	|i| dS )a  Checks the document meta data and ensures all the indexes exist.

        Global defaults can be set in the meta - see :doc:`guide/defining-documents`

        By default, this will get called automatically upon first interaction with the
        Document collection (query, save, etc) so unless you disabled `auto_create_index`, you
        shouldn't have to call this manually.

        This also gets called upon every call to Document.save if `auto_create_index_on_save` is set to True

        If called multiple times, MongoDB will not re-recreate indexes if they exist already

        .. note:: You can disable automatic index creation by setting
                  `auto_create_index` to False in the documents meta data
        Zindex_backgroundF
index_opts	index_clsTindex_specsr'   rW   r   allow_inheritancer!   N)r!   )rM   rU   rf   rv   rr   r*   rw   r   )
rW   r   r   r   ro   Zcls_indexedr   specr'   rp   r(   r(   r)   rc     s(    



zDocument.ensure_indexesc                    s   | j drg S g   fdd|  dd }g } D ]$}||D ]}||krF|| qFq:dg|krv|dg | j ddr| j d	r|d
g |S )zLists all indexes that should be created for the Document collection.
        It includes all the indexes from super- and sub-classes.

        Note that it will only return the indexes' fields, not the indexes' options
        r   c                    s   |  krt | tr |  | jD ]P}t |tr"|tkr"|jds"| j|  jkr"| kr" | | q"| 	 D ]<}t |tr|| j|  jkr|| kr| | | q|d S )Nr   )
r#   r
   r   	__bases__r   rM   rU   rf   Z	full_name__subclasses__)rW   Zbase_clssubclassr   get_classesr(   r)   r     s8    





z*Document.list_indexes.<locals>.get_classesc                 S   sB   g }| j d r>| j d }|D ] }| }|d}|| q|S )Nr   r'   )rM   rv   rr   r   )rW   indexesr   r   r'   r(   r(   r)   get_indexes_spec  s    


z/Document.list_indexes.<locals>.get_indexes_spec)rB   rh   r   Tr   r!   rh   )rM   rU   r   )rW   r   r   r   indexr(   r   r)   list_indexes  s     
zDocument.list_indexesc                    s   |   g  |  }|  D ]Z}d|d d krl|d d d |d } fdd|D  q  |d  q  fddD }fd	d D }d
g|krd} D ]}t|r||krd} qq|r|d
g ||dS )zCompares the indexes defined in MongoEngine with the ones
        existing in the database. Returns any missing/extra indexes.
        Z_ftsr   r   rh   weightsc                    s   g | ]}| fqS r(   r(   )r   r   )
index_typer(   r)   r     s     z,Document.compare_indexes.<locals>.<listcomp>c                    s   g | ]}| kr|qS r(   r(   r   r   )existingr(   r)   r     s      c                    s   g | ]}| kr|qS r(   r(   r   )requiredr(   r)   r     s      r   FT)missingextra)	r   rf   Zindex_informationvaluesrU   r   r   r*   remove)rW   ro   infoZtext_index_fieldsr   r   Zcls_obsoleter   r(   )r   r   r   r)   compare_indexes  s(    
zDocument.compare_indexes)N)	FTTNNNNNN)N)T)T)rh   )F)*r+   r,   r-   rE   r
   rG   rF   propertyrP   setterrH   classmethodrX   rZ   rf   r_   r`   rC   rz   r   r   r   r   r   r   ry   rx   rw   r   r   r   r   r   r{   r   r   r   r   rc   r   r   rI   r(   r(   r6   r)   r   y   sx   4

	



%

/         
 &





3


7
Ar   c                       s(   e Zd ZdZeZdZ fddZ  ZS )r   a*  A Dynamic Document class allowing flexible, expandable and uncontrolled
    schemas.  As a :class:`~mongoengine.Document` subclass, acts in the same
    way as an ordinary document but has expanded style properties.  Any data
    passed or set against the :class:`~mongoengine.DynamicDocument` that is
    not a field is automatically converted into a
    :class:`~mongoengine.fields.DynamicField` and data can be attributed to that
    field.

    .. note::

        There is one caveat on Dynamic Documents: undeclared fields cannot start with `_`
    Tc                    s>   |d }|| j kr,t| |d d| j | _nt j|| dS )YDelete the attribute by setting to None and allowing _delta
        to unset it.
        r   NF)Z_dynamic_fieldsrQ   nullr0   __delattr__)r3   r4   r5   r   r6   r(   r)   r   .  s
    
zDynamicDocument.__delattr__)	r+   r,   r-   rE   r
   rG   _dynamicr   rI   r(   r(   r6   r)   r     s   r   c                   @   s    e Zd ZdZeZdZdd ZdS )r   zA Dynamic Embedded Document class allowing flexible, expandable and
    uncontrolled schemas. See :class:`~mongoengine.DynamicDocument` for more
    information about dynamic documents.
    Tc                 O   sJ   |d }|| j kr:| j | j}t|r,| }t| || nt| |d dS )r   r   N)r   defaultcallablerQ   )r3   r4   r5   r   r  r(   r(   r)   r   E  s    
z#DynamicEmbeddedDocument.__delattr__N)r+   r,   r-   rE   r   rG   r   r   r(   r(   r(   r)   r   :  s   r   c                   @   s$   e Zd ZdZdd Zedd ZdS )r    a  A document returned from a map/reduce query.

    :param collection: An instance of :class:`~pymongo.Collection`
    :param key: Document/result key, often an instance of
                :class:`~bson.objectid.ObjectId`. If supplied as
                an ``ObjectId`` found in the given ``collection``,
                the object can be accessed via the ``object`` property.
    :param value: The result(s) for this key.
    c                 C   s   || _ || _|| _|| _d S r/   )	_documentrY   r   rR   )r3   r   ro   r   rR   r(   r(   r)   r1   ^  s    zMapReduceDocument.__init__c                 C   s~   |   jd }t|}t| j|sVz|| j| _W n" tk
rT   td|j Y nX t| dsx| j j	| j| _
| j
S | j
S )zmLazy-load the object referenced by ``self.key``. ``self.key``
        should be the ``primary_key``.
        rL   zCould not cast key as %s_key_object)r  rM   typer#   r   	Exceptionr+   r^   ZobjectsZwith_idr  )r3   rL   Zid_field_typer(   r(   r)   objectd  s    
zMapReduceDocument.objectN)r+   r,   r-   rE   r1   r   r  r(   r(   r(   r)   r    S  s   
r    )-r   r   Z
bson.dbrefr   Zpymongo.read_preferencesr   Zmongoenginer   Zmongoengine.baser   r   r   r   r	   r
   r   Zmongoengine.commonr   Zmongoengine.connectionr   r   Zmongoengine.context_managersr   r   r   Zmongoengine.errorsr   r   r   Zmongoengine.pymongo_supportr   Zmongoengine.querysetr   r   r   r   __all__r*   r  r   r   r   r   r   r    r(   r(   r(   r)   <module>   s4   $	9       )