refactor(minor): use DocStatus (#19545)

This commit is contained in:
Raffael Meyer 2023-01-10 13:08:11 +01:00 committed by GitHub
parent 894f0affcc
commit ed30a6d59f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 17 deletions

View file

@ -6,6 +6,7 @@ import json
import frappe
from frappe.core.doctype.submission_queue.submission_queue import queue_submission
from frappe.desk.form.load import run_onload
from frappe.model.docstatus import DocStatus
from frappe.monitor import add_data_to_monitor
from frappe.utils.scheduler import is_scheduler_inactive
@ -17,8 +18,14 @@ def savedocs(doc, action):
set_local_name(doc)
# action
doc.docstatus = {"Save": 0, "Submit": 1, "Update": 1, "Cancel": 2}[action]
if doc.docstatus == 1:
doc.docstatus = {
"Save": DocStatus.draft(),
"Submit": DocStatus.submitted(),
"Update": DocStatus.submitted(),
"Cancel": DocStatus.cancelled(),
}[action]
if doc.docstatus.is_submitted():
if action == "Submit" and doc.meta.queue_in_background and not is_scheduler_inactive():
queue_submission(doc, action)
return

View file

@ -9,6 +9,7 @@ import frappe.defaults
import frappe.model.meta
from frappe import _, get_module_path
from frappe.desk.doctype.tag.tag import delete_tags_for_document
from frappe.model.docstatus import DocStatus
from frappe.model.dynamic_links import get_dynamic_link_map
from frappe.model.naming import revert_series_if_last
from frappe.model.utils import is_virtual_doctype
@ -265,7 +266,7 @@ def check_if_doc_is_linked(doc, method="Delete"):
# don't check for communication and todo!
continue
if method != "Delete" and (method != "Cancel" or item.docstatus != 1):
if method != "Delete" and (method != "Cancel" or not DocStatus(item.docstatus).is_submitted()):
# don't raise exception if not
# linked to a non-cancelled doc when deleting or to a submitted doc when cancelling
continue
@ -302,13 +303,12 @@ def check_if_doc_is_dynamically_linked(doc, method="Delete"):
refdoc.get(df.options) == doc.doctype
and refdoc.get(df.fieldname) == doc.name
and (
(method == "Delete" and refdoc.docstatus < 2)
or (method == "Cancel" and refdoc.docstatus == 1)
# linked to an non-cancelled doc when deleting
(method == "Delete" and not DocStatus(refdoc.docstatus).is_cancelled())
# linked to a submitted doc when cancelling
or (method == "Cancel" and DocStatus(refdoc.docstatus).is_submitted())
)
):
# raise exception only if
# linked to an non-cancelled doc when deleting
# or linked to a submitted doc when cancelling
raise_link_exists_exception(doc, df.parent, df.parent)
else:
# dynamic link in table
@ -321,14 +321,11 @@ def check_if_doc_is_dynamically_linked(doc, method="Delete"):
(doc.doctype, doc.name),
as_dict=True,
):
if (method == "Delete" and refdoc.docstatus < 2) or (
method == "Cancel" and refdoc.docstatus == 1
# linked to an non-cancelled doc when deleting
# or linked to a submitted doc when cancelling
if (method == "Delete" and not DocStatus(refdoc.docstatus).is_cancelled()) or (
method == "Cancel" and DocStatus(refdoc.docstatus).is_submitted()
):
# raise exception only if
# linked to an non-cancelled doc when deleting
# or linked to a submitted doc when cancelling
reference_doctype = refdoc.parenttype if meta.istable else df.parent
reference_docname = refdoc.parent if meta.istable else refdoc.name
at_position = f"at Row: {refdoc.idx}" if meta.istable else ""

View file

@ -118,10 +118,10 @@ def get_rendered_template(
validate_print_permission(doc)
if doc.meta.is_submittable:
if doc.docstatus == 0 and not cint(print_settings.allow_print_for_draft):
if doc.docstatus.is_draft() and not cint(print_settings.allow_print_for_draft):
frappe.throw(_("Not allowed to print draft documents"), frappe.PermissionError)
if doc.docstatus == 2 and not cint(print_settings.allow_print_for_cancelled):
if doc.docstatus.is_cancelled() and not cint(print_settings.allow_print_for_cancelled):
frappe.throw(_("Not allowed to print cancelled documents"), frappe.PermissionError)
doc.run_method("before_print", print_settings)