11import copy
2- import operator
32import numbers
3+ import operator
44from collections import Hashable
55from functools import partial
66
7- import pymongo
8- from bson import json_util , ObjectId
7+ from bson import ObjectId , json_util
98from bson .dbref import DBRef
109from bson .son import SON
10+ import pymongo
1111
1212from mongoengine import signals
13- from mongoengine .common import _import_class
14- from mongoengine .errors import (ValidationError , InvalidDocumentError ,
15- LookUpError , FieldDoesNotExist )
16- from mongoengine .python_support import PY3 , txt_type
17- from mongoengine .base .common import get_document , ALLOW_INHERITANCE
13+ from mongoengine .base .common import ALLOW_INHERITANCE , get_document
1814from mongoengine .base .datastructures import (
1915 BaseDict ,
2016 BaseList ,
2117 EmbeddedDocumentList ,
22- StrictDict ,
23- SemiStrictDict
18+ SemiStrictDict ,
19+ StrictDict
2420)
2521from mongoengine .base .fields import ComplexBaseField
22+ from mongoengine .common import _import_class
23+ from mongoengine .errors import (FieldDoesNotExist , InvalidDocumentError ,
24+ LookUpError , ValidationError )
25+ from mongoengine .python_support import PY3 , txt_type
2626
2727__all__ = ('BaseDocument' , 'NON_FIELD_ERRORS' )
2828
@@ -73,7 +73,7 @@ def __init__(self, *args, **values):
7373 # if so raise an Exception.
7474 if not self ._dynamic and (self ._meta .get ('strict' , True ) or _created ):
7575 _undefined_fields = set (values .keys ()) - set (
76- self ._fields .keys () + ['id' , 'pk' , '_cls' , '_text_score' ])
76+ self ._fields .keys () + ['id' , 'pk' , '_cls' , '_text_score' ])
7777 if _undefined_fields :
7878 msg = (
7979 "The fields '{0}' do not exist on the document '{1}'"
@@ -310,7 +310,7 @@ def to_mongo(self, use_db_field=True, fields=None):
310310 data = SON ()
311311 data ["_id" ] = None
312312 data ['_cls' ] = self ._class_name
313-
313+
314314 # only root fields ['test1.a', 'test2'] => ['test1', 'test2']
315315 root_fields = set ([f .split ('.' )[0 ] for f in fields ])
316316
@@ -333,11 +333,11 @@ def to_mongo(self, use_db_field=True, fields=None):
333333 i .replace (key , '' ) for i in fields
334334 if i .startswith (key )]
335335
336- ex_vars ['fields' ] = embedded_fields
336+ ex_vars ['fields' ] = embedded_fields
337337
338338 if 'use_db_field' in f_inputs :
339339 ex_vars ['use_db_field' ] = use_db_field
340-
340+
341341 value = field .to_mongo (value , ** ex_vars )
342342
343343 # Handle self generating fields
@@ -566,8 +566,10 @@ def _get_changed_fields(self, inspected=None):
566566 continue
567567 if isinstance (field , ReferenceField ):
568568 continue
569- elif (isinstance (data , (EmbeddedDocument , DynamicEmbeddedDocument ))
570- and db_field_name not in changed_fields ):
569+ elif (
570+ isinstance (data , (EmbeddedDocument , DynamicEmbeddedDocument )) and
571+ db_field_name not in changed_fields
572+ ):
571573 # Find all embedded fields that have been changed
572574 changed = data ._get_changed_fields (inspected )
573575 changed_fields += ["%s%s" % (key , k ) for k in changed if k ]
@@ -608,7 +610,7 @@ def _delta(self):
608610 break
609611 elif isinstance (d , list ) and p .lstrip ('-' ).isdigit ():
610612 if p [0 ] == '-' :
611- p = str (len (d )+ int (p ))
613+ p = str (len (d ) + int (p ))
612614 try :
613615 d = d [int (p )]
614616 except IndexError :
@@ -644,7 +646,7 @@ def _delta(self):
644646 for p in parts :
645647 if isinstance (d , list ) and p .lstrip ('-' ).isdigit ():
646648 if p [0 ] == '-' :
647- p = str (len (d )+ int (p ))
649+ p = str (len (d ) + int (p ))
648650 d = d [int (p )]
649651 elif (hasattr (d , '__getattribute__' ) and
650652 not isinstance (d , dict )):
@@ -775,8 +777,12 @@ def _build_index_spec(cls, spec):
775777 # Check to see if we need to include _cls
776778 allow_inheritance = cls ._meta .get ('allow_inheritance' ,
777779 ALLOW_INHERITANCE )
778- include_cls = (allow_inheritance and not spec .get ('sparse' , False ) and
779- spec .get ('cls' , True ) and '_cls' not in spec ['fields' ])
780+ include_cls = (
781+ allow_inheritance and
782+ not spec .get ('sparse' , False ) and
783+ spec .get ('cls' , True ) and
784+ '_cls' not in spec ['fields' ]
785+ )
780786
781787 # 733: don't include cls if index_cls is False unless there is an explicit cls with the index
782788 include_cls = include_cls and (spec .get ('cls' , False ) or cls ._meta .get ('index_cls' , True ))
0 commit comments