Skip to content

Commit

Permalink
- Drop Queryset .modify full_response parameter (deprecated a while…
Browse files Browse the repository at this point in the history
… ago and not working since pymongo3) - Fix Cursor 'snapshot' feature (now using cursor modifiers as expected by pymongo3+)
  • Loading branch information
cp-bagerard committed Mar 22, 2020
1 parent f49baf5 commit e1960f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
4 changes: 3 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ Development
- Fixed a bug causing inaccurate query results, while combining ``__raw__`` and regular filters for the same field #2264
- Add support for the `elemMatch` projection operator in .fields() (e.g BlogPost.objects.fields(elemMatch__comments="test")) #2267
- DictField validate failed without default connection (bug introduced in 0.19.0) #2239
- Fix cursor snapshot feature (`Doc.objects().snapshot(True)`), which was deprecated but is now working
- Remove methods deprecated years ago:
- name parameter in Field constructor e.g `StringField(name="...")`, was replaced by db_field
- `name` parameter in Field constructor e.g `StringField(name="...")`, was replaced by db_field
- Queryset.slave_okay() was deprecated since pymongo3
- dropDups was dropped with MongoDB3
- ``Queryset._ensure_indexes`` and ``Queryset.ensure_indexes``, the right method to use is ``Document.ensure_indexes``
- Remove `full_response` from ``Queryset.modify``, as it is not working with Pymongo 3+

Changes in 0.19.1
=================
Expand Down
40 changes: 11 additions & 29 deletions mongoengine/queryset/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,7 @@ def update_one(self, upsert=False, write_concern=None, full_result=False, **upda
**update
)

def modify(
self, upsert=False, full_response=False, remove=False, new=False, **update
):
def modify(self, upsert=False, remove=False, new=False, **update):
"""Update and return the updated document.
Returns either the document before or after modification based on `new`
Expand All @@ -616,8 +614,6 @@ def modify(
information about the command's execution.
:param upsert: insert if document doesn't exist (default ``False``)
:param full_response: return the entire response object from the
server (default ``False``, not available for PyMongo 3+)
:param remove: remove rather than updating (default ``False``)
:param new: return updated rather than original document
(default ``False``)
Expand All @@ -639,9 +635,6 @@ def modify(
sort = queryset._ordering

try:
if full_response:
msg = "With PyMongo 3+, it is not possible anymore to get the full response."
warnings.warn(msg, DeprecationWarning)
if remove:
result = queryset._collection.find_one_and_delete(
query, sort=sort, **self._cursor_args
Expand All @@ -664,14 +657,8 @@ def modify(
except pymongo.errors.OperationFailure as err:
raise OperationError(u"Update failed (%s)" % err)

if full_response:
if result["value"] is not None:
result["value"] = self._document._from_son(
result["value"], only_fields=self.only_fields
)
else:
if result is not None:
result = self._document._from_son(result, only_fields=self.only_fields)
if result is not None:
result = self._document._from_son(result, only_fields=self.only_fields)

return result

Expand Down Expand Up @@ -1151,10 +1138,7 @@ def snapshot(self, enabled):
:param enabled: whether or not snapshot mode is enabled
..versionchanged:: 0.5 - made chainable
.. deprecated:: Ignored with PyMongo 3+
"""
msg = "snapshot is deprecated as it has no impact when using PyMongo 3+."
warnings.warn(msg, DeprecationWarning)
queryset = self.clone()
queryset._snapshot = enabled
return queryset
Expand Down Expand Up @@ -1594,25 +1578,23 @@ def _collection(self):

@property
def _cursor_args(self):
fields_name = "projection"
# snapshot is not handled at all by PyMongo 3+
# TODO: evaluate similar possibilities using modifiers
if self._snapshot:
msg = "The snapshot option is not anymore available with PyMongo 3+"
warnings.warn(msg, DeprecationWarning)
projection_field_name = "projection"

cursor_args = {}
if not self._timeout:
cursor_args["no_cursor_timeout"] = True

if self._snapshot:
cursor_args["modifiers"] = {"$snapshot": True}

if self._loaded_fields:
cursor_args[fields_name] = self._loaded_fields.as_dict()
cursor_args[projection_field_name] = self._loaded_fields.as_dict()

if self._search_text:
if fields_name not in cursor_args:
cursor_args[fields_name] = {}
if projection_field_name not in cursor_args:
cursor_args[projection_field_name] = {}

cursor_args[fields_name]["_text_score"] = {"$meta": "textScore"}
cursor_args[projection_field_name]["_text_score"] = {"$meta": "textScore"}

return cursor_args

Expand Down
20 changes: 18 additions & 2 deletions tests/queryset/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5595,10 +5595,10 @@ def test_create_count(self):
self.Person.objects.create(name="Baz")
assert self.Person.objects.count(with_limit_and_skip=True) == 3

newPerson = self.Person.objects.create(name="Foo_1")
self.Person.objects.create(name="Foo_1")
assert self.Person.objects.count(with_limit_and_skip=True) == 4

def test_no_cursor_timeout(self):
def test_cursor_args_no_cursor_timeout(self):
qs = self.Person.objects()
assert qs._cursor_args == {} # ensure no regression of #2148

Expand All @@ -5608,6 +5608,22 @@ def test_no_cursor_timeout(self):
qs = self.Person.objects().timeout(False)
assert qs._cursor_args == {"no_cursor_timeout": True}

def test_cursor_args_snapshot(self):
self.Person.drop_collection()
self.Person.objects.create(name="Foo")
self.Person.objects.create(name="Bar")
self.Person.objects.create(name="Baz")

qs = self.Person.objects()
assert qs._cursor_args == {}

qs = self.Person.objects().snapshot(False)
assert qs._cursor_args == {}

qs = self.Person.objects().snapshot(True)
assert qs._cursor_args == {"modifiers": {"$snapshot": True}}
assert len(list(qs)) == 3


if __name__ == "__main__":
unittest.main()

0 comments on commit e1960f2

Please sign in to comment.