From fa4e9540fed7460abd4b07f430e3045a8ad28f6a Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 7 Feb 2014 14:50:44 +0530 Subject: [PATCH] BugFix: Nested Set Model - on_trash and before_rename Also, minor fix in bean --- webnotes/model/bean.py | 2 +- webnotes/utils/nestedset.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/webnotes/model/bean.py b/webnotes/model/bean.py index 9798984ec3..431e5ad8a5 100644 --- a/webnotes/model/bean.py +++ b/webnotes/model/bean.py @@ -232,7 +232,7 @@ class Bean: add_to_response(webnotes.local.response, webnotes.call(getattr(self.controller, method), *args, **kwargs)) - args = [self, method] + args + args = [self, method] + list(args) for handler in webnotes.get_hooks("bean_event:" + self.doc.doctype + ":" + method) \ + webnotes.get_hooks("bean_event:*:" + method): add_to_response(webnotes.local.response, webnotes.call(webnotes.get_attr(handler), *args, **kwargs)) diff --git a/webnotes/utils/nestedset.py b/webnotes/utils/nestedset.py index ab654ba97f..fa55611732 100644 --- a/webnotes/utils/nestedset.py +++ b/webnotes/utils/nestedset.py @@ -17,6 +17,8 @@ from webnotes import msgprint, _ class NestedSetRecursionError(webnotes.ValidationError): pass class NestedSetMultipleRootsError(webnotes.ValidationError): pass +class NestedSetChildExistsError(webnotes.ValidationError): pass +class NestedSetInvalidMergeError(webnotes.ValidationError): pass # called in the on_update method def update_nsm(doc_obj): @@ -188,19 +190,29 @@ class DocTypeNestedSet(object): def on_trash(self): if not self.nsm_parent_field: self.nsm_parent_field = webnotes.scrub(self.doc.doctype) + "_parent" + parent = self.doc.fields[self.nsm_parent_field] if not parent: msgprint(_("Root ") + self.doc.doctype + _(" cannot be deleted."), raise_exception=1) + + # cannot delete non-empty group + has_children = webnotes.conn.sql("""select count(name) from `tab{doctype}` + where `{nsm_parent_field}`=%s""".format(doctype=self.doc.doctype, nsm_parent_field=self.nsm_parent_field), + (self.doc.name,))[0][0] + if has_children: + webnotes.throw("{cannot_delete}. {children_exist}: {name}.".format( + children_exist=_("Children exist for"), name=self.doc.name, + cannot_delete=_("Cannot delete")), NestedSetChildExistsError) self.doc.fields[self.nsm_parent_field] = "" update_nsm(self) - def before_rename(self, newdn, olddn, merge=False, group_fname="is_group"): + def before_rename(self, olddn, newdn, merge=False, group_fname="is_group"): if merge: is_group = webnotes.conn.get_value(self.doc.doctype, newdn, group_fname) if self.doc.fields[group_fname] != is_group: webnotes.throw(_("""Merging is only possible between Group-to-Group or - Ledger-to-Ledger""")) + Ledger-to-Ledger"""), NestedSetInvalidMergeError) def after_rename(self, olddn, newdn, merge=False): if merge: