Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changes in 0.10.1 - DEV
- Fix infinite recursion with CASCADE delete rules under specific conditions. #1046
- Fix CachedReferenceField bug when loading cached docs as DBRef but failing to save them. #1047
- Fix ignored chained options #842
- Document save's save_condition error raises `SaveConditionError` exception #1070

Changes in 0.10.0
=================
Expand Down
9 changes: 6 additions & 3 deletions mongoengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
ALLOW_INHERITANCE,
get_document
)
from mongoengine.errors import InvalidQueryError, InvalidDocumentError
from mongoengine.errors import (InvalidQueryError, InvalidDocumentError,
SaveConditionError)
from mongoengine.python_support import IS_PYMONGO_3
from mongoengine.queryset import (OperationError, NotUniqueError,
QuerySet, transform)
Expand Down Expand Up @@ -294,6 +295,8 @@ def save(self, force_insert=False, validate=True, clean=True,
if the condition is satisfied in the current db record.
.. versionchanged:: 0.10
:class:`OperationError` exception raised if save_condition fails.
.. versionchanged:: 0.10.1
:class: save_condition failure now raises a `SaveConditionError`
"""
signals.pre_save.send(self.__class__, document=self)

Expand Down Expand Up @@ -359,8 +362,8 @@ def is_new_object(last_error):
last_error = collection.update(select_dict, update_query,
upsert=upsert, **write_concern)
if not upsert and last_error['nModified'] == 0:
raise OperationError('Race condition preventing'
' document update detected')
raise SaveConditionError('Race condition preventing'
' document update detected')
created = is_new_object(last_error)

if cascade is None:
Expand Down
4 changes: 4 additions & 0 deletions mongoengine/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class NotUniqueError(OperationError):
pass


class SaveConditionError(OperationError):
pass


class FieldDoesNotExist(Exception):
"""Raised when trying to set a field
not declared in a :class:`~mongoengine.Document`
Expand Down
8 changes: 4 additions & 4 deletions tests/document/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from mongoengine import *
from mongoengine.errors import (NotRegistered, InvalidDocumentError,
InvalidQueryError, NotUniqueError,
FieldDoesNotExist)
FieldDoesNotExist, SaveConditionError)
from mongoengine.queryset import NULLIFY, Q
from mongoengine.connection import get_db
from mongoengine.base import get_document
Expand Down Expand Up @@ -1021,7 +1021,7 @@ def UUID(i):
flip(w1)
self.assertTrue(w1.toggle)
self.assertEqual(w1.count, 1)
self.assertRaises(OperationError,
self.assertRaises(SaveConditionError,
w1.save, save_condition={'save_id': UUID(42)})
w1.reload()
self.assertFalse(w1.toggle)
Expand Down Expand Up @@ -1050,7 +1050,7 @@ def UUID(i):
self.assertEqual(w1.count, 2)
flip(w2)
flip(w2)
self.assertRaises(OperationError,
self.assertRaises(SaveConditionError,
w2.save, save_condition={'save_id': old_id})
w2.reload()
self.assertFalse(w2.toggle)
Expand All @@ -1063,7 +1063,7 @@ def UUID(i):
self.assertTrue(w1.toggle)
self.assertEqual(w1.count, 3)
flip(w1)
self.assertRaises(OperationError,
self.assertRaises(SaveConditionError,
w1.save, save_condition={'count__gte': w1.count})
w1.reload()
self.assertTrue(w1.toggle)
Expand Down