From bc2b41d2f5dc01d8d47b3efdf122bbd367a94e25 Mon Sep 17 00:00:00 2001 From: Suraj Shetty Date: Fri, 8 Dec 2023 21:15:34 +0530 Subject: [PATCH] perf: Simplify dynamic route evaluation to speed up route resolution validate complete route only if starting of the path matches with the webform route This avoids the expensive matching from huge route_map (in sites with lots of web forms) --- frappe/website/doctype/web_form/web_form.py | 2 +- frappe/website/router.py | 28 +++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/frappe/website/doctype/web_form/web_form.py b/frappe/website/doctype/web_form/web_form.py index 4e7eac423d..2b2f3743f5 100644 --- a/frappe/website/doctype/web_form/web_form.py +++ b/frappe/website/doctype/web_form/web_form.py @@ -279,7 +279,7 @@ def get_context(context): if field.fieldtype == "Select" and field.options: messages.extend(field.options.split("\n")) - messages.extend(col.label for col in self.list_columns) + messages.extend(col.get("label") if col else "" for col in self.list_columns) context.translated_messages = frappe.as_json( {message: _(message) for message in messages if message} diff --git a/frappe/website/router.py b/frappe/website/router.py index 3085212aa1..2947b89f91 100644 --- a/frappe/website/router.py +++ b/frappe/website/router.py @@ -34,27 +34,29 @@ def get_page_info_from_web_form(path): """Query published web forms and evaluate if the route matches""" from frappe.website.doctype.web_form.web_form import get_published_web_forms - rules, page_info = [], {} for d in get_published_web_forms(): + if not (path.startswith(f"{d.route}") or path.startswith(f"/{d.route}")): + continue + + rules = [] rules.append(Rule(f"/{d.route}", endpoint=d.name)) rules.append(Rule(f"/{d.route}/list", endpoint=d.name)) rules.append(Rule(f"/{d.route}/new", endpoint=d.name)) rules.append(Rule(f"/{d.route}/", endpoint=d.name)) rules.append(Rule(f"/{d.route}//edit", endpoint=d.name)) d.doctype = "Web Form" - page_info[d.name] = d + end_point = evaluate_dynamic_routes(rules, path) - end_point = evaluate_dynamic_routes(rules, path) - if end_point: - if path.endswith("/list"): - frappe.form_dict.is_list = True - elif path.endswith("/new"): - frappe.form_dict.is_new = True - elif path.endswith("/edit"): - frappe.form_dict.is_edit = True - else: - frappe.form_dict.is_read = True - return page_info[end_point] + if end_point: + if path.endswith("/list"): + frappe.form_dict.is_list = True + elif path.endswith("/new"): + frappe.form_dict.is_new = True + elif path.endswith("/edit"): + frappe.form_dict.is_edit = True + else: + frappe.form_dict.is_read = True + return d def evaluate_dynamic_routes(rules, path):