From 5a3d8f925ee93a393ff5143d372b4e97bdfa4bb2 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 25 Feb 2022 01:27:21 +0530 Subject: [PATCH] feat: Document.rename API Transform current document object using the rename_doc API. The design of the API should allow for easy action queueing. Defined as `rename(self, name: str, merge: bool = False, force: bool = False)` Usage: In [1]: doc = frappe.get_doc("Person", "5a188f66c1") In [2]: doc.rename("5a188f66c2") In [3]: doc.name Out[3]: '5a188f66c2' --- frappe/model/document.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/frappe/model/document.py b/frappe/model/document.py index cb36c18b47..5017d342c1 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -938,6 +938,14 @@ class Document(BaseDocument): self.docstatus = DocStatus.cancelled() return self.save() + @whitelist.__func__ + def _rename(self, name: str, merge: bool = False, force: bool = False): + """Cancel the document. Sets `docstatus` = 2, then saves. + """ + from frappe.model.rename_doc import rename_doc + self.name = rename_doc(doc=self, new=name, merge=merge, force=force) + self.reload() + @whitelist.__func__ def submit(self): """Submit the document. Sets `docstatus` = 1, then saves.""" @@ -948,6 +956,12 @@ class Document(BaseDocument): """Cancel the document. Sets `docstatus` = 2, then saves.""" return self._cancel() + @whitelist.__func__ + def rename(self, name: str, merge: bool = False, force: bool = False): + """Rename the document to `name`. This transforms the current object. + """ + return self._rename(name=name, merge=merge, force=force) + def delete(self, ignore_permissions=False): """Delete document.""" frappe.delete_doc(self.doctype, self.name, ignore_permissions = ignore_permissions, flags=self.flags)