Browse Source

refactor: dynamically load web form module (#18486) (#18495)

(cherry picked from commit 3491d0dcb0)

Co-authored-by: Ankush Menat <ankush@frappe.io>
version-14
mergify[bot] 2 years ago
committed by GitHub
parent
commit
f8307cda0d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 16 deletions
  1. +10
    -15
      frappe/website/doctype/web_form/web_form.py
  2. +2
    -1
      frappe/www/list.py

+ 10
- 15
frappe/website/doctype/web_form/web_form.py View File

@@ -98,7 +98,6 @@ def get_context(context):
"""Build context to render the `web_form.html` template"""
context.in_edit_mode = False
context.in_view_mode = False
self.set_web_form_module()

if frappe.form_dict.is_list:
context.template = "website/doctype/web_form/templates/web_list.html"
@@ -284,13 +283,14 @@ def get_context(context):

def add_custom_context_and_script(self, context):
"""Update context from module if standard and append script"""
if self.web_form_module:
new_context = self.web_form_module.get_context(context)
if self.is_standard:
web_form_module = get_web_form_module(self)
new_context = web_form_module.get_context(context)

if new_context:
context.update(new_context)

js_path = os.path.join(os.path.dirname(self.web_form_module.__file__), scrub(self.name) + ".js")
js_path = os.path.join(os.path.dirname(web_form_module.__file__), scrub(self.name) + ".js")
if os.path.exists(js_path):
script = frappe.render_template(open(js_path).read(), context)

@@ -300,9 +300,7 @@ def get_context(context):

context.script = script

css_path = os.path.join(
os.path.dirname(self.web_form_module.__file__), scrub(self.name) + ".css"
)
css_path = os.path.join(os.path.dirname(web_form_module.__file__), scrub(self.name) + ".css")
if os.path.exists(css_path):
style = open(css_path).read()

@@ -322,14 +320,6 @@ def get_context(context):

return parents

def set_web_form_module(self):
"""Get custom web form module if exists"""
self.web_form_module = self.get_web_form_module()

def get_web_form_module(self):
if self.is_standard:
return get_doc_module(self.module, self.doctype, self.name)

def validate_mandatory(self, doc):
"""Validate mandatory web form fields"""
missing = []
@@ -368,6 +358,11 @@ def get_context(context):
return False


def get_web_form_module(doc):
if doc.is_standard:
return get_doc_module(doc.module, doc.doctype, doc.name)


@frappe.whitelist(allow_guest=True)
@rate_limit(key="web_form", limit=5, seconds=60, methods=["POST"])
def accept(web_form, data, docname=None):


+ 2
- 1
frappe/www/list.py View File

@@ -163,6 +163,7 @@ def prepare_filters(doctype, controller, kwargs):

def get_list_context(context, doctype, web_form_name=None):
from frappe.modules import load_doctype_module
from frappe.website.doctype.web_form.web_form import get_web_form_module

list_context = context or frappe._dict()
meta = frappe.get_meta(doctype)
@@ -193,7 +194,7 @@ def get_list_context(context, doctype, web_form_name=None):
# get context from web form module
if web_form_name:
web_form = frappe.get_doc("Web Form", web_form_name)
list_context = update_context_from_module(web_form.get_web_form_module(), list_context)
list_context = update_context_from_module(get_web_form_module(web_form), list_context)

# get path from '/templates/' folder of the doctype
if not meta.custom and not list_context.row_template:


Loading…
Cancel
Save