From 6c2922ddf0f22806da1bc8f2264b91c1ebd85a9c Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 27 Nov 2013 14:34:02 +0530 Subject: [PATCH] [cleanup] [minor] added webnotes.model.delete_doc (module) --- core/doctype/defaultvalue/defaultvalue.py | 2 +- core/doctype/profile/test_profile.py | 2 +- webnotes/__init__.py | 11 ++- webnotes/model/__init__.py | 5 -- webnotes/model/bean.py | 2 +- webnotes/model/delete_doc.py | 91 ++++++++++++++++++++++ webnotes/model/utils.py | 94 ----------------------- webnotes/widgets/reportview.py | 3 +- 8 files changed, 102 insertions(+), 108 deletions(-) create mode 100644 webnotes/model/delete_doc.py diff --git a/core/doctype/defaultvalue/defaultvalue.py b/core/doctype/defaultvalue/defaultvalue.py index 3701a2ecb9..0b042d71b7 100644 --- a/core/doctype/defaultvalue/defaultvalue.py +++ b/core/doctype/defaultvalue/defaultvalue.py @@ -19,4 +19,4 @@ def on_doctype_update(): where Key_name="defaultvalue_parent_parenttype_index" """): webnotes.conn.commit() webnotes.conn.sql("""alter table `tabDefaultValue` - add index defaultvalue_parent_parenttype_index(parent, parentttype)""") \ No newline at end of file + add index defaultvalue_parent_parenttype_index(parent, parenttype)""") \ No newline at end of file diff --git a/core/doctype/profile/test_profile.py b/core/doctype/profile/test_profile.py index e09fbf5183..3576bc15e9 100644 --- a/core/doctype/profile/test_profile.py +++ b/core/doctype/profile/test_profile.py @@ -3,7 +3,7 @@ import webnotes, unittest -from webnotes.model.utils import delete_doc, LinkExistsError +from webnotes.model.delete_doc import delete_doc, LinkExistsError class TestProfile(unittest.TestCase): def test_delete(self): diff --git a/webnotes/__init__.py b/webnotes/__init__.py index 7e7956af1b..435376af98 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -431,17 +431,20 @@ def get_doctype(doctype, processed=False): import webnotes.model.doctype return webnotes.model.doctype.get(doctype, processed) -def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=None, for_reload=False, ignore_permissions=False): - import webnotes.model.utils +def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=None, + for_reload=False, ignore_permissions=False): + import webnotes.model.delete_doc if not ignore_doctypes: ignore_doctypes = [] if isinstance(name, list): for n in name: - webnotes.model.utils.delete_doc(doctype, n, doclist, force, ignore_doctypes, for_reload, ignore_permissions) + webnotes.model.delete_doc.delete_doc(doctype, n, doclist, force, ignore_doctypes, + for_reload, ignore_permissions) else: - webnotes.model.utils.delete_doc(doctype, name, doclist, force, ignore_doctypes, for_reload, ignore_permissions) + webnotes.model.delete_doc.delete_doc(doctype, name, doclist, force, ignore_doctypes, + for_reload, ignore_permissions) def clear_perms(doctype): conn.sql("""delete from tabDocPerm where parent=%s""", doctype) diff --git a/webnotes/model/__init__.py b/webnotes/model/__init__.py index 3cad98d219..f54d413a78 100644 --- a/webnotes/model/__init__.py +++ b/webnotes/model/__init__.py @@ -23,11 +23,6 @@ def insert(doclist): return wrapper -@webnotes.whitelist() -def delete_doc(doctype=None, name=None, doclist = None, force=0): - import webnotes.model.utils - return webnotes.model.utils.delete_doc(doctype, name, doclist, force) - def rename(doctype, old, new, debug=False): import webnotes.model.rename_doc webnotes.model.rename_doc.rename_doc(doctype, old, new, debug) diff --git a/webnotes/model/bean.py b/webnotes/model/bean.py index dde95edc40..ab886d0414 100644 --- a/webnotes/model/bean.py +++ b/webnotes/model/bean.py @@ -400,7 +400,7 @@ class Bean: _("No Permission to ") + ptype, raise_exception=BeanPermissionError) def check_no_back_links_exist(self): - from webnotes.model.utils import check_if_doc_is_linked + from webnotes.model.delete_doc import check_if_doc_is_linked check_if_doc_is_linked(self.doc.doctype, self.doc.name, method="Cancel") def check_mandatory(self): diff --git a/webnotes/model/delete_doc.py b/webnotes/model/delete_doc.py new file mode 100644 index 0000000000..85f7294af5 --- /dev/null +++ b/webnotes/model/delete_doc.py @@ -0,0 +1,91 @@ +# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors +# MIT License. See license.txt + +from __future__ import unicode_literals + +import webnotes +import webnotes.model.meta + +def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=[], for_reload=False, ignore_permissions=False): + """ + Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record + """ + + # get from form + if not doctype: + doctype = webnotes.form_dict.get('dt') + name = webnotes.form_dict.get('dn') + + if not doctype: + webnotes.msgprint('Nothing to delete!', raise_exception =1) + + # already deleted..? + if not webnotes.conn.exists(doctype, name): + return + + if not for_reload: + check_permission_and_not_submitted(doctype, name, ignore_permissions) + + run_on_trash(doctype, name, doclist) + # check if links exist + if not force: + check_if_doc_is_linked(doctype, name) + + try: + tablefields = webnotes.model.meta.get_table_fields(doctype) + webnotes.conn.sql("delete from `tab%s` where name=%s" % (doctype, "%s"), name) + for t in tablefields: + if t[0] not in ignore_doctypes: + webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), name) + except Exception, e: + if e.args[0]==1451: + webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name)) + + raise + + # delete attachments + from webnotes.utils.file_manager import remove_all + remove_all(doctype, name) + + return 'okay' + +def check_permission_and_not_submitted(doctype, name, ignore_permissions=False): + # permission + if not ignore_permissions and webnotes.session.user!="Administrator" and not webnotes.has_permission(doctype, "cancel"): + webnotes.msgprint(_("User not allowed to delete."), raise_exception=True) + + # check if submitted + if webnotes.conn.get_value(doctype, name, "docstatus") == 1: + webnotes.msgprint(_("Submitted Record cannot be deleted")+": "+name+"("+doctype+")", + raise_exception=True) + +def run_on_trash(doctype, name, doclist): + # call on_trash if required + if doclist: + bean = webnotes.bean(doclist) + else: + bean = webnotes.bean(doctype, name) + + bean.run_method("on_trash") + +class LinkExistsError(webnotes.ValidationError): pass + +def check_if_doc_is_linked(dt, dn, method="Delete"): + """ + Raises excption if the given doc(dt, dn) is linked in another record. + """ + from webnotes.model.rename_doc import get_link_fields + link_fields = get_link_fields(dt) + link_fields = [[lf['parent'], lf['fieldname'], lf['issingle']] for lf in link_fields] + + for link_dt, link_field, issingle in link_fields: + if not issingle: + item = webnotes.conn.get_value(link_dt, {link_field:dn}, + ["name", "parent", "parenttype", "docstatus"], as_dict=True) + + if item and item.parent != dn and (method=="Delete" or + (method=="Cancel" and item.docstatus==1)): + webnotes.msgprint(method + " " + _("Error") + ":"+\ + ("%s (%s) " % (dn, dt)) + _("is linked in") + (" %s (%s)") % + (item.parent or item.name, item.parent and item.parenttype or link_dt), + raise_exception=LinkExistsError) diff --git a/webnotes/model/utils.py b/webnotes/model/utils.py index 15b584dd9c..66bca7d3da 100644 --- a/webnotes/model/utils.py +++ b/webnotes/model/utils.py @@ -113,100 +113,6 @@ def copy_doclist(doclist, no_copy = []): return cl -def getvaluelist(doclist, fieldname): - """ - Returns a list of values of a particualr fieldname from all Document object in a doclist - """ - l = [] - for d in doclist: - l.append(d.fields[fieldname]) - return l - -def delete_doc(doctype=None, name=None, doclist = None, force=0, ignore_doctypes=[], for_reload=False, ignore_permissions=False): - """ - Deletes a doc(dt, dn) and validates if it is not submitted and not linked in a live record - """ - import webnotes.model.meta - - # get from form - if not doctype: - doctype = webnotes.form_dict.get('dt') - name = webnotes.form_dict.get('dn') - - if not doctype: - webnotes.msgprint('Nothing to delete!', raise_exception =1) - - # already deleted..? - if not webnotes.conn.exists(doctype, name): - return - - if not for_reload: - check_permission_and_not_submitted(doctype, name, ignore_permissions) - - run_on_trash(doctype, name, doclist) - # check if links exist - if not force: - check_if_doc_is_linked(doctype, name) - - try: - tablefields = webnotes.model.meta.get_table_fields(doctype) - webnotes.conn.sql("delete from `tab%s` where name=%s" % (doctype, "%s"), name) - for t in tablefields: - if t[0] not in ignore_doctypes: - webnotes.conn.sql("delete from `tab%s` where parent = %s" % (t[0], '%s'), name) - except Exception, e: - if e.args[0]==1451: - webnotes.msgprint("Cannot delete %s '%s' as it is referenced in another record. You must delete the referred record first" % (doctype, name)) - - raise - - # delete attachments - from webnotes.utils.file_manager import remove_all - remove_all(doctype, name) - - return 'okay' - -def check_permission_and_not_submitted(doctype, name, ignore_permissions=False): - # permission - if not ignore_permissions and webnotes.session.user!="Administrator" and not webnotes.has_permission(doctype, "cancel"): - webnotes.msgprint(_("User not allowed to delete."), raise_exception=True) - - # check if submitted - if webnotes.conn.get_value(doctype, name, "docstatus") == 1: - webnotes.msgprint(_("Submitted Record cannot be deleted")+": "+name+"("+doctype+")", - raise_exception=True) - -def run_on_trash(doctype, name, doclist): - # call on_trash if required - if doclist: - bean = webnotes.bean(doclist) - else: - bean = webnotes.bean(doctype, name) - - bean.run_method("on_trash") - -class LinkExistsError(webnotes.ValidationError): pass - -def check_if_doc_is_linked(dt, dn, method="Delete"): - """ - Raises excption if the given doc(dt, dn) is linked in another record. - """ - from webnotes.model.rename_doc import get_link_fields - link_fields = get_link_fields(dt) - link_fields = [[lf['parent'], lf['fieldname'], lf['issingle']] for lf in link_fields] - - for link_dt, link_field, issingle in link_fields: - if not issingle: - item = webnotes.conn.get_value(link_dt, {link_field:dn}, - ["name", "parent", "parenttype", "docstatus"], as_dict=True) - - if item and item.parent != dn and (method=="Delete" or - (method=="Cancel" and item.docstatus==1)): - webnotes.msgprint(method + " " + _("Error") + ":"+\ - ("%s (%s) " % (dn, dt)) + _("is linked in") + (" %s (%s)") % - (item.parent or item.name, item.parent and item.parenttype or link_dt), - raise_exception=LinkExistsError) - def set_default(doc, key): if not doc.is_default: webnotes.conn.set(doc, "is_default", 1) diff --git a/webnotes/widgets/reportview.py b/webnotes/widgets/reportview.py index be5ead1225..db0cf40ab2 100644 --- a/webnotes/widgets/reportview.py +++ b/webnotes/widgets/reportview.py @@ -333,7 +333,6 @@ def get_labels(columns): def delete_items(): """delete selected items""" import json - from webnotes.model import delete_doc from webnotes.model.code import get_obj il = json.loads(webnotes.form_dict.get('items')) @@ -344,7 +343,7 @@ def delete_items(): dt_obj = get_obj(doctype, d) if hasattr(dt_obj, 'on_trash'): dt_obj.on_trash() - delete_doc(doctype, d) + webnotes.delete_doc(doctype, d) except Exception, e: webnotes.errprint(webnotes.getTraceback()) pass