From 1d4b1870cfb50e3b91b0fe187874c31d5373ea65 Mon Sep 17 00:00:00 2001 From: Danil Date: Mon, 19 Jun 2017 05:04:46 +0500 Subject: [PATCH 1/3] to_db_fields fix (#1553) --- mongoengine/queryset/base.py | 4 ++-- tests/queryset/field_list.py | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mongoengine/queryset/base.py b/mongoengine/queryset/base.py index f7c32d20..41a10ace 100644 --- a/mongoengine/queryset/base.py +++ b/mongoengine/queryset/base.py @@ -1731,14 +1731,14 @@ class BaseQuerySet(object): for x in document._subclasses][1:] for field in fields: try: - field = '.'.join(f.db_field for f in + field = '.'.join(f if isinstance(f, six.string_types) else f.db_field for f in document._lookup_field(field.split('.'))) ret.append(field) except LookUpError as err: found = False for subdoc in subclasses: try: - subfield = '.'.join(f.db_field for f in + subfield = '.'.join(f if isinstance(f, six.string_types) else f.db_field for f in subdoc._lookup_field(field.split('.'))) ret.append(subfield) found = True diff --git a/tests/queryset/field_list.py b/tests/queryset/field_list.py index d1277e06..c07cec3e 100644 --- a/tests/queryset/field_list.py +++ b/tests/queryset/field_list.py @@ -197,14 +197,18 @@ class OnlyExcludeAllTest(unittest.TestCase): title = StringField() text = StringField() + class VariousData(EmbeddedDocument): + some = BooleanField() + class BlogPost(Document): content = StringField() author = EmbeddedDocumentField(User) comments = ListField(EmbeddedDocumentField(Comment)) + various = MapField(field=EmbeddedDocumentField(VariousData)) BlogPost.drop_collection() - post = BlogPost(content='Had a good coffee today...') + post = BlogPost(content='Had a good coffee today...', various={'test_dynamic':{'some': True}}) post.author = User(name='Test User') post.comments = [Comment(title='I aggree', text='Great post!'), Comment(title='Coffee', text='I hate coffee')] post.save() @@ -215,6 +219,9 @@ class OnlyExcludeAllTest(unittest.TestCase): self.assertEqual(obj.author.name, 'Test User') self.assertEqual(obj.comments, []) + obj = BlogPost.objects.only('various.test_dynamic.some').get() + self.assertEqual(obj.various["test_dynamic"].some, True) + obj = BlogPost.objects.only('content', 'comments.title',).get() self.assertEqual(obj.content, 'Had a good coffee today...') self.assertEqual(obj.author, None) From a8d6e59a7a0434edebcb52ca63e40be5466548fa Mon Sep 17 00:00:00 2001 From: Stefan Wojcik Date: Sun, 18 Jun 2017 17:25:39 -0700 Subject: [PATCH 2/3] minor tweaks to code quality in _fields_to_dbfields --- mongoengine/queryset/base.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/mongoengine/queryset/base.py b/mongoengine/queryset/base.py index 41a10ace..6f9c372c 100644 --- a/mongoengine/queryset/base.py +++ b/mongoengine/queryset/base.py @@ -1722,25 +1722,33 @@ class BaseQuerySet(object): return frequencies def _fields_to_dbfields(self, fields): - """Translate fields paths to its db equivalents""" - ret = [] + """Translate fields' paths to their db equivalents.""" subclasses = [] - document = self._document - if document._meta['allow_inheritance']: + if self._document._meta['allow_inheritance']: subclasses = [get_document(x) - for x in document._subclasses][1:] + for x in self._document._subclasses][1:] + + db_field_paths = [] for field in fields: + field_parts = field.split('.') try: - field = '.'.join(f if isinstance(f, six.string_types) else f.db_field for f in - document._lookup_field(field.split('.'))) - ret.append(field) + field = '.'.join( + f if isinstance(f, six.string_types) else f.db_field + for f in self._document._lookup_field(field_parts) + ) + db_field_paths.append(field) except LookUpError as err: found = False + + # If a field path wasn't found on the main document, go + # through its subclasses and see if it exists on any of them. for subdoc in subclasses: try: - subfield = '.'.join(f if isinstance(f, six.string_types) else f.db_field for f in - subdoc._lookup_field(field.split('.'))) - ret.append(subfield) + subfield = '.'.join( + f if isinstance(f, six.string_types) else f.db_field + for f in subdoc._lookup_field(field_parts) + ) + db_field_paths.append(subfield) found = True break except LookUpError: @@ -1748,7 +1756,8 @@ class BaseQuerySet(object): if not found: raise err - return ret + + return db_field_paths def _get_order_by(self, keys): """Given a list of MongoEngine-style sort keys, return a list From a7cab513695ba1814d9316719429d63249fb97dc Mon Sep 17 00:00:00 2001 From: Davidrjx <1058960881@qq.com> Date: Thu, 13 Jul 2017 18:07:36 +0800 Subject: [PATCH 3/3] Use a set literal in _clean_settings (#1585) --- AUTHORS | 1 + mongoengine/connection.py | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/AUTHORS b/AUTHORS index 1d724718..96a7850e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -243,3 +243,4 @@ that much better: * Victor Varvaryuk * Stanislav Kaledin (https://github.com/sallyruthstruik) * Dmitry Yantsen (https://github.com/mrTable) + * Renjianxin (https://github.com/Davidrjx) diff --git a/mongoengine/connection.py b/mongoengine/connection.py index 7eae810f..34ff4dc3 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -146,13 +146,14 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False): raise MongoEngineConnectionError(msg) def _clean_settings(settings_dict): - irrelevant_fields = set([ - 'name', 'username', 'password', 'authentication_source', - 'authentication_mechanism' - ]) + # set literal more efficient than calling set function + irrelevant_fields_set = { + 'name', 'username', 'password', + 'authentication_source', 'authentication_mechanism' + } return { k: v for k, v in settings_dict.items() - if k not in irrelevant_fields + if k not in irrelevant_fields_set } # Retrieve a copy of the connection settings associated with the requested