feat: set module in standard web templates

This commit is contained in:
barredterra 2020-09-18 15:28:53 +02:00
parent aadb3e7150
commit 6703990cf4
4 changed files with 41 additions and 10 deletions

View file

@ -311,3 +311,4 @@ frappe.patches.v13_0.enable_custom_script
frappe.patches.v13_0.update_newsletter_content_type
execute:frappe.db.set_value('Website Settings', 'Website Settings', {'navbar_template': 'Standard Navbar', 'footer_template': 'Standard Footer'})
frappe.patches.v13_0.delete_event_producer_and_consumer_keys
frappe.patches.v13_0.web_template_set_module

View file

@ -0,0 +1,15 @@
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals
import frappe
def execute():
"""Set default module for standard Web Template, if none."""
frappe.reload_doc('website', 'doctype', 'Web Template')
standard_templates = frappe.get_list('Web Template', {'standard': 1})
for template in standard_templates:
doc = frappe.get_doc('Web Template', template.name)
if not doc.module:
doc.module = 'Website'
doc.save()

View file

@ -9,6 +9,7 @@
"field_order": [
"type",
"standard",
"module",
"template",
"fields"
],
@ -41,11 +42,18 @@
"in_standard_filter": 1,
"label": "Type",
"options": "Section\nNavbar\nFooter"
},
{
"depends_on": "standard",
"fieldname": "module",
"fieldtype": "Link",
"label": "Module",
"options": "Module Def"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2020-09-11 16:09:07.367280",
"modified": "2020-09-18 13:54:12.667877",
"modified_by": "Administrator",
"module": "Website",
"name": "Web Template",

View file

@ -17,42 +17,49 @@ from frappe.modules.export_file import (
class WebTemplate(Document):
def validate(self):
if not frappe.conf.developer_mode and self.standard:
frappe.throw(_("Cannot create standard Web Template"))
if self.standard and not (frappe.conf.developer_mode or frappe.flags.in_patch):
frappe.throw(_("Enable developer mode to create a standard Web Template"))
for field in self.fields:
if not field.fieldname:
field.fieldname = frappe.scrub(field.label)
if self.standard and not self.module:
frappe.throw(_("Please select which module this Web Template belongs to."))
def on_update(self):
if self.standard and frappe.conf.developer_mode:
export_to_files(record_list=[["Web Template", self.name]], create_init=True)
self.create_template_file()
def create_template_file(self):
"""Touch a HTML file for the Web Template and add existing content, if any."""
if self.standard:
folder = create_folder("Website", self.doctype, self.name, False)
module = self.module or "Website" # required for smooth migration
folder = create_folder(module, self.doctype, self.name, False)
path = os.path.join(folder, frappe.scrub(self.name) + ".html")
if not os.path.exists(path):
open(path, "w").close()
with open(path, "w") as template_file:
if self.template:
template_file.write(self.template)
def render(self, values):
values = values or '{}'
values = values or "{}"
values = frappe.parse_json(values)
return get_rendered_template(self.name, values)
def get_rendered_template(web_template, values):
standard = frappe.db.get_value("Web Template", web_template, "standard")
if standard:
module_path = get_module_path("Website")
wt = frappe.get_doc("Web Template", web_template)
if wt.standard:
module_path = get_module_path(wt.module)
dt, dn = scrub_dt_dn("Web Template", web_template)
scrubbed = frappe.scrub(web_template)
full_path = os.path.join("frappe", module_path, dt, dn, scrubbed + ".html")
root_app_path = os.path.abspath(os.path.join(frappe.get_app_path("frappe"), ".."))
template = os.path.relpath(full_path, root_app_path)
else:
template = frappe.db.get_value("Web Template", web_template, "template")
template = wt.template
context = values or {}
context.update({"values": values})