8000 fix: raise exception if doc before save is not found by sagarvora · Pull Request #18796 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: raise exception if doc before save is not found #18796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 9, 2022
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
12 changes: 8 additions & 4 deletions frappe/model/document.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,13 @@ def check_if_latest(self):
Will also validate document transitions (Save > Submit > Cancel) calling
`self.check_docstatus_transition`."""

self.load_doc_before_save()
self.load_doc_before_save(raise_exception=True)

self._action = "save"
previous = self.get_doc_before_save()
previous = self._doc_before_save

if not previous or self.meta.get("is_virtual"):
# previous is None for new document insert
if not previous:
self.check_docstatus_transition(0)
return

Expand Down Expand Up @@ -1048,7 +1049,7 @@ def run_before_save_methods(self):

self.set_title_field()

def load_doc_before_save(self):
def load_doc_before_save(self, *, raise_exception: bool = False):
"""load existing document from db before saving"""

self._doc_before_save = None
Expand All @@ -1059,6 +1060,9 @@ def load_doc_before_save(self):
try:
self._doc_before_save = frappe.get_doc(self.doctype, self.name, for_update=True)
except frappe.DoesNotExistError:
if raise_exception:
raise

frappe.clear_last_message()

def run_post_save_methods(self):
Expand Down
13 changes: 13 additions & 0 deletions frappe/tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,19 @@ def test_realtime_notify(self):
todo.save()
self.assertEqual(todo.notify_update.call_count, 1)

def test_error_on_saving_new_doc_with_name(self):
"""Trying to save a new doc with name should raise DoesNotExistError"""

doc = frappe.get_doc(
{
"doctype": "ToDo",
"description": "this should raise frappe.DoesNotExistError",
"name": "lets-trick-doc-save",
}
)

self.assertRaises(frappe.DoesNotExistError, doc.save)


class TestDocumentWebView(FrappeTestCase):
def get(self, path, user="Guest"):
Expand Down
0