fix: don't save template twice

This commit is contained in:
barredterra 2020-10-28 11:44:18 +01:00
parent cc338238f9
commit 292f14c161

View file

@ -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."""