From 292f14c1614cfa46af99c2c183b7259fd3bf00a1 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Wed, 28 Oct 2020 11:44:18 +0100 Subject: [PATCH] fix: don't save template twice --- .../doctype/web_template/web_template.py | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/frappe/website/doctype/web_template/web_template.py b/frappe/website/doctype/web_template/web_template.py index 8831bfbf58..0a8980a4e8 100644 --- a/frappe/website/doctype/web_template/web_template.py +++ b/frappe/website/doctype/web_template/web_template.py @@ -11,7 +11,7 @@ import frappe from frappe.model.document import Document from frappe import _ from frappe.modules.export_file import ( - export_to_files, + write_document_file, get_module_path, scrub_dt_dn, ) @@ -30,24 +30,36 @@ class WebTemplate(Document): if frappe.conf.developer_mode: # custom to standard if self.standard: - export_to_files(record_list=[["Web Template", self.name]], create_init=True) - self.create_template_file() - self.template = "" + self.export_to_files() # standard to custom was_standard = (self.get_doc_before_save() or {}).get("standard") if was_standard and not self.standard: - self.template = self.get_template(standard=True) - rmtree(self.get_template_folder()) + self.import_from_files() - def create_template_file(self): + def export_to_files(self): + """Export We Template to a new folder. + + Doc is exported as JSON. The content of the `template` field gets + written into a separate HTML file. The template should not be contained + in the JSON. + """ + html, self.template = self.template, "" + write_document_file(self, create_init=True) + self.create_template_file(html) + + def import_from_files(self): + self.template = self.get_template(standard=True) + rmtree(self.get_template_folder()) + + def create_template_file(self, html=None): """Touch a HTML file for the Web Template and add existing content, if any.""" if self.standard: path = self.get_template_path() if not os.path.exists(path): with open(path, "w") as template_file: - if self.template: - template_file.write(self.template) + if html: + template_file.write(html) def get_template_folder(self): """Return the absolute path to the template's folder."""