[fix] dynamic link checking during cancel

This commit is contained in:
Anand Doshi 2015-08-14 12:48:55 +05:30
parent a56b892539
commit ac2312e9ab
2 changed files with 22 additions and 8 deletions

View file

@ -150,27 +150,41 @@ def check_if_doc_is_linked(doc, method="Delete"):
if item and item.parent != doc.name and ((method=="Delete" and item.docstatus<2) or
(method=="Cancel" and item.docstatus==1)):
# raise exception only if
# linked to an non-cancelled doc when deleting
# or linked to a submitted doc when cancelling
frappe.throw(_("Cannot delete or cancel because {0} {1} is linked with {2} {3}").format(doc.doctype,
doc.name, item.parenttype if item.parent else link_dt, item.parent or item.name),
frappe.LinkExistsError)
def check_if_doc_is_dynamically_linked(doc):
def check_if_doc_is_dynamically_linked(doc, method="Delete"):
for query in dynamic_link_queries:
for df in frappe.db.sql(query, as_dict=True):
if frappe.get_meta(df.parent).issingle:
# dynamic link in single doc
refdoc = frappe.db.get_singles_dict(df.parent)
if refdoc.get(df.options)==doc.doctype and refdoc.get(df.fieldname)==doc.name:
if (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))
):
# raise exception only if
# linked to an non-cancelled doc when deleting
# or linked to a submitted doc when cancelling
frappe.throw(_("Cannot delete or cancel because {0} {1} is linked with {2} {3}").format(doc.doctype,
doc.name, df.parent, ""), frappe.LinkExistsError)
else:
# dynamic link in table
for name in frappe.db.sql_list("""select name from `tab{parent}` where
{options}=%s and {fieldname}=%s""".format(**df), (doc.doctype, doc.name)):
frappe.throw(_("Cannot delete or cancel because {0} {1} is linked with {2} {3}").format(doc.doctype,
doc.name, df.parent, name), frappe.LinkExistsError)
for refdoc in frappe.db.sql("""select name, docstatus from `tab{parent}` where
{options}=%s and {fieldname}=%s""".format(**df), (doc.doctype, doc.name), as_dict=True):
if ((method=="Delete" and refdoc.docstatus < 2) or (method=="Cancel" and refdoc.docstatus==1)):
# raise exception only if
# linked to an non-cancelled doc when deleting
# or linked to a submitted doc when cancelling
frappe.throw(_("Cannot delete or cancel because {0} {1} is linked with {2} {3}")\
.format(doc.doctype, doc.name, df.parent, refdoc.name), frappe.LinkExistsError)
def delete_linked_todos(doc):
delete_doc("ToDo", frappe.db.sql_list("""select name from `tabToDo`

View file

@ -581,7 +581,7 @@ class Document(BaseDocument):
from frappe.model.delete_doc import check_if_doc_is_linked, check_if_doc_is_dynamically_linked
if not self.flags.ignore_links:
check_if_doc_is_linked(self, method="Cancel")
check_if_doc_is_dynamically_linked(self)
check_if_doc_is_dynamically_linked(self, method="Cancel")
@staticmethod
def whitelist(f):