diff --git a/core/doctype/doctype/doctype.py b/core/doctype/doctype/doctype.py index ce3f813974..8c4edf6fe2 100644 --- a/core/doctype/doctype/doctype.py +++ b/core/doctype/doctype/doctype.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import webnotes -from webnotes import msgprint +from webnotes import msgprint, _ import os from webnotes.utils import now, cint @@ -100,11 +100,13 @@ class DocType: webnotes.conn.sql("delete from `tabProperty Setter` where doc_type = %s", self.doc.name) webnotes.conn.sql("delete from `tabReport` where ref_doctype=%s", self.doc.name) - def on_rename(self, new, old, merge=False): + def before_rename(self, old, new, merge=False): + if merge: + webnotes.throw(_("DocType can not be merged")) + + def after_rename(self, old, new, merge=False): if self.doc.issingle: - webnotes.conn.sql("""\ - update tabSingles set doctype=%s - where doctype=%s""", (new, old)) + webnotes.conn.sql("""update tabSingles set doctype=%s where doctype=%s""", (new, old)) else: webnotes.conn.sql("rename table `tab%s` to `tab%s`" % (old, new)) diff --git a/core/doctype/profile/profile.py b/core/doctype/profile/profile.py index 0762041da0..d223a1e261 100644 --- a/core/doctype/profile/profile.py +++ b/core/doctype/profile/profile.py @@ -237,11 +237,27 @@ Thank you,
webnotes.conn.sql("""delete from `tabComment` where comment_doctype='Message' and (comment_docname=%s or owner=%s)""", (self.doc.name, self.doc.name)) - - def on_rename(self,newdn,olddn, merge=False): + def before_rename(self, olddn, newdn, merge=False): webnotes.clear_cache(user=olddn) - self.validate_rename(newdn, olddn) - + self.validate_rename(olddn, newdn) + + def validate_rename(self, olddn, newdn): + # do not allow renaming administrator and guest + if olddn in ["Administrator", "Guest"]: + webnotes.msgprint("""Hey! You are restricted from renaming the user: %s""" % \ + (olddn, ), raise_exception=1) + + self.validate_email_type(newdn) + + def validate_email_type(self, email): + from webnotes.utils import validate_email_add + + email = email.strip() + if not validate_email_add(email): + webnotes.msgprint("%s is not a valid email id" % email) + raise Exception + + def after_rename(self, olddn, newdn, merge=False): tables = webnotes.conn.sql("show tables") for tab in tables: desc = webnotes.conn.sql("desc `%s`" % tab[0], as_dict=1) @@ -263,21 +279,6 @@ Thank you,
# update __Auth table if not merge: webnotes.conn.sql("""update __Auth set user=%s where user=%s""", (newdn, olddn)) - - def validate_rename(self, newdn, olddn): - # do not allow renaming administrator and guest - if olddn in ["Administrator", "Guest"]: - webnotes.msgprint("""Hey! You are restricted from renaming the user: %s""" % \ - (olddn, ), raise_exception=1) - - self.validate_email_type(newdn) - - def validate_email_type(self, email): - from webnotes.utils import validate_email_add - - email = email.strip() - if not validate_email_add(email): - webnotes.throw("%s is not a valid email id" % email) def add_roles(self, *roles): for role in roles: diff --git a/webnotes/__init__.py b/webnotes/__init__.py index 0266e94ffa..01aaf63e4d 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -458,7 +458,7 @@ def reload_doc(module, dt=None, dn=None, force=False): def rename_doc(doctype, old, new, debug=0, force=False, merge=False): from webnotes.model.rename_doc import rename_doc - rename_doc(doctype, old, new, force=force, merge=merge) + return rename_doc(doctype, old, new, force=force, merge=merge) def insert(doclist): import webnotes.model diff --git a/webnotes/model/rename_doc.py b/webnotes/model/rename_doc.py index 4c5fcbcaf6..541f81b75c 100644 --- a/webnotes/model/rename_doc.py +++ b/webnotes/model/rename_doc.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import webnotes -from webnotes import _ from webnotes.utils import cint import webnotes.model.doctype from webnotes.model.doc import validate_name @@ -23,10 +22,10 @@ def rename_doc(doctype, old, new, force=False, merge=False): # get doclist of given doctype doclist = webnotes.model.doctype.get(doctype) - # call on_rename - obj = webnotes.get_obj(doctype, old) - if hasattr(obj, 'on_rename'): - new = obj.on_rename(new, old, merge) or new + # call before_rename + old_obj = webnotes.get_obj(doctype, old) + if hasattr(old_obj, 'before_rename'): + new = old_obj.before_rename(old, new, merge) or new new = validate_rename(doctype, new, doclist, merge, force) @@ -45,6 +44,11 @@ def rename_doc(doctype, old, new, force=False, merge=False): if merge: webnotes.delete_doc(doctype, old) + # call after_rename + new_obj = webnotes.get_obj(doctype, new) + if hasattr(new_obj, 'after_rename'): + new_obj.after_rename(old, new, merge) + return new def update_attachments(doctype, old, new): diff --git a/webnotes/utils/nestedset.py b/webnotes/utils/nestedset.py index 5981945636..be82fff496 100644 --- a/webnotes/utils/nestedset.py +++ b/webnotes/utils/nestedset.py @@ -14,8 +14,6 @@ from __future__ import unicode_literals import webnotes from webnotes import msgprint, _ -from webnotes.model.bean import Bean -from webnotes.model.doc import Document # called in the on_update method def update_nsm(doc_obj): @@ -191,13 +189,15 @@ class DocTypeNestedSet(object): self.doc.fields[self.nsm_parent_field] = "" update_nsm(self) - def on_rename(self, newdn, olddn, merge=False, group_fname="is_group"): + def before_rename(self, newdn, olddn, 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: - msgprint(_("""Merging is only possible between Group-to-Group or - Ledger-to-Ledger"""), raise_exception=1) - + webnotes.throw(_("""Merging is only possible between Group-to-Group or + Ledger-to-Ledger""")) + + def after_rename(self, olddn, newdn, merge=False): + if merge: parent_field = "parent_" + self.doc.doctype.replace(" ", "_").lower() rebuild_tree(self.doc.doctype, parent_field) @@ -213,4 +213,3 @@ class DocTypeNestedSet(object): (self.doc.doctype, self.nsm_parent_field, '%s'), (self.doc.name)): webnotes.throw(self.doc.doctype + ": " + self.doc.name + _(" can not be marked as a ledger as it has existing child")) - \ No newline at end of file