def update_nsm(doc_obj): # get fields, data from the DocType opf = 'old_parent' if str(doc_obj.__class__)=='webnotes.model.doc.Document': # passed as a Document object d = doc_obj pf = "parent_" + webnotes.scrub(d.doctype) else: # passed as a DocType object d = doc_obj.doc pf = "parent_" + webnotes.scrub(d.doctype) if hasattr(doc_obj,'nsm_parent_field'): pf = doc_obj.nsm_parent_field if hasattr(doc_obj,'nsm_oldparent_field'): opf = doc_obj.nsm_oldparent_field p, op = d.fields.get(pf) or None, d.fields.get(opf) or None # has parent changed (?) or parent is None (root) if not d.lft and not d.rgt: update_add_node(d, p or '', pf) elif op != p: update_move_node(d, pf) # set old parent d.fields[opf] = p webnotes.conn.set_value(d.doctype, d.name, opf, p or '') # reload d._loadfromdb()
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 scrub(txt): return webnotes.scrub(txt)