Skip to content

Commit 2d605f3

Browse files
committed
- Drop Queryset .modify full_response parameter (deprecated a while
ago and not working since pymongo3) - Fix Cursor 'snapshot' feature (now using cursor modifiers as expected by pymongo3+)
1 parent f49baf5 commit 2d605f3

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

docs/changelog.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ Development
1010
- Fixed a bug causing inaccurate query results, while combining ``__raw__`` and regular filters for the same field #2264
1111
- Add support for the `elemMatch` projection operator in .fields() (e.g BlogPost.objects.fields(elemMatch__comments="test")) #2267
1212
- DictField validate failed without default connection (bug introduced in 0.19.0) #2239
13+
- Fix cursor snapshot feature (`Doc.objects().snapshot(True)`), which was deprecated but is now working
1314
- Remove methods deprecated years ago:
14-
- name parameter in Field constructor e.g `StringField(name="...")`, was replaced by db_field
15+
- `name` parameter in Field constructor e.g `StringField(name="...")`, was replaced by db_field
1516
- Queryset.slave_okay() was deprecated since pymongo3
1617
- dropDups was dropped with MongoDB3
1718
- ``Queryset._ensure_indexes`` and ``Queryset.ensure_indexes``, the right method to use is ``Document.ensure_indexes``
19+
- Remove `full_response` from ``Queryset.modify``, as it is not working with Pymongo 3+
1820

1921
Changes in 0.19.1
2022
=================

mongoengine/queryset/base.py

+11-26
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,7 @@ def update_one(self, upsert=False, write_concern=None, full_result=False, **upda
600600
**update
601601
)
602602

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

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

667-
if full_response:
668-
if result["value"] is not None:
669-
result["value"] = self._document._from_son(
670-
result["value"], only_fields=self.only_fields
671-
)
672-
else:
673-
if result is not None:
674-
result = self._document._from_son(result, only_fields=self.only_fields)
660+
if result is not None:
661+
result = self._document._from_son(result, only_fields=self.only_fields)
675662

676663
return result
677664

@@ -1594,25 +1581,23 @@ def _collection(self):
15941581

15951582
@property
15961583
def _cursor_args(self):
1597-
fields_name = "projection"
1598-
# snapshot is not handled at all by PyMongo 3+
1599-
# TODO: evaluate similar possibilities using modifiers
1600-
if self._snapshot:
1601-
msg = "The snapshot option is not anymore available with PyMongo 3+"
1602-
warnings.warn(msg, DeprecationWarning)
1584+
projection_field_name = "projection"
16031585

16041586
cursor_args = {}
16051587
if not self._timeout:
16061588
cursor_args["no_cursor_timeout"] = True
16071589

1590+
if self._snapshot:
1591+
cursor_args["modifiers"] = {"$snapshot": True}
1592+
16081593
if self._loaded_fields:
1609-
cursor_args[fields_name] = self._loaded_fields.as_dict()
1594+
cursor_args[projection_field_name] = self._loaded_fields.as_dict()
16101595

16111596
if self._search_text:
1612-
if fields_name not in cursor_args:
1613-
cursor_args[fields_name] = {}
1597+
if projection_field_name not in cursor_args:
1598+
cursor_args[projection_field_name] = {}
16141599

1615-
cursor_args[fields_name]["_text_score"] = {"$meta": "textScore"}
1600+
cursor_args[projection_field_name]["_text_score"] = {"$meta": "textScore"}
16161601

16171602
return cursor_args
16181603

tests/queryset/test_queryset.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -5595,10 +5595,10 @@ def test_create_count(self):
55955595
self.Person.objects.create(name="Baz")
55965596
assert self.Person.objects.count(with_limit_and_skip=True) == 3
55975597

5598-
newPerson = self.Person.objects.create(name="Foo_1")
5598+
self.Person.objects.create(name="Foo_1")
55995599
assert self.Person.objects.count(with_limit_and_skip=True) == 4
56005600

5601-
def test_no_cursor_timeout(self):
5601+
def test_cursor_args_no_cursor_timeout(self):
56025602
qs = self.Person.objects()
56035603
assert qs._cursor_args == {} # ensure no regression of #2148
56045604

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

5611+
def test_cursor_args_snapshot(self):
5612+
self.Person.drop_collection()
5613+
self.Person.objects.create(name="Foo")
5614+
self.Person.objects.create(name="Bar")
5615+
self.Person.objects.create(name="Baz")
5616+
5617+
qs = self.Person.objects()
5618+
assert qs._cursor_args == {}
5619+
5620+
qs = self.Person.objects().snapshot(False)
5621+
assert qs._cursor_args == {}
5622+
5623+
qs = self.Person.objects().snapshot(True)
5624+
assert qs._cursor_args == {"modifiers": {"$snapshot": True}}
5625+
assert len(list(qs)) == 3
5626+
56115627

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

0 commit comments

Comments
 (0)