From fb029082192c65d68491fafac997a98049beaf5b Mon Sep 17 00:00:00 2001 From: Sagar Vora Date: Mon, 30 Dec 2019 10:52:19 +0530 Subject: [PATCH] fix: (re)allow custom filters/methods to be used with jinja --- frappe/utils/jinja.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/frappe/utils/jinja.py b/frappe/utils/jinja.py index befb9336fa..7c3b9b0482 100644 --- a/frappe/utils/jinja.py +++ b/frappe/utils/jinja.py @@ -16,6 +16,7 @@ def get_jenv(): set_filters(jenv) jenv.globals.update(get_safe_globals()) + jenv.globals.update(get_jenv_customization('methods')) frappe.local.jenv = jenv @@ -124,4 +125,27 @@ def set_filters(jenv): jenv.filters["flt"] = flt jenv.filters["abs_url"] = abs_url - if frappe.flags.in_setup_help: return + if frappe.flags.in_setup_help: + return + + jenv.filters.update(get_jenv_customization('filters')) + + +def get_jenv_customization(customization_type): + '''Returns a dict with filter/method name as key and definition as value''' + + import frappe + + out = {} + if not getattr(frappe.local, "site", None): + return out + + values = frappe.get_hooks("jenv", {}).get(customization_type) + if not values: + return out + + for value in values: + fn_name, fn_string = value.split(":") + out[fn_name] = frappe.get_attr(fn_string) + + return out