From cde153d75b817002b77cdc6a09ed011bdec2ff84 Mon Sep 17 00:00:00 2001 From: Felipe Orellana Date: Mon, 19 Sep 2016 10:17:22 -0700 Subject: [PATCH] Added inheritable context hook --- frappe/website/context.py | 47 ++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/frappe/website/context.py b/frappe/website/context.py index 993bf54db7..74ef53d320 100644 --- a/frappe/website/context.py +++ b/frappe/website/context.py @@ -33,6 +33,25 @@ def get_context(path, args=None): return context +def update_controller_context(context, controller): + module = frappe.get_module(controller) + + if module: + # get config fields + for prop in ("base_template_path", "template", "no_cache", "no_sitemap", + "condition_field"): + if hasattr(module, prop): + context[prop] = getattr(module, prop) + + if hasattr(module, "get_context"): + ret = module.get_context(context) + if ret: + context.update(ret) + + if hasattr(module, "get_children"): + context.children = module.get_children(context) + + def build_context(context): """get_context method of doc or module is supposed to render content templates and push it into context""" @@ -62,22 +81,18 @@ def build_context(context): context[prop] = getattr(context.doc, prop, False) elif context.controller: - module = frappe.get_module(context.controller) - - if module: - # get config fields - for prop in ("base_template_path", "template", "no_cache", "no_sitemap", - "condition_field"): - if hasattr(module, prop): - context[prop] = getattr(module, prop) - - if hasattr(module, "get_context"): - ret = module.get_context(context) - if ret: - context.update(ret) - - if hasattr(module, "get_children"): - context.children = module.get_children(context) + # controller based context + update_controller_context(context, context.controller) + + # controller context extensions + context_controller_hooks = frappe.get_hooks("extend_controller_context") or {} + for controller, extension in context_controller_hooks.items(): + if isinstance(extension, list): + for ext in extension: + if controller == context.controller: + update_controller_context(context, ext) + else: + update_controller_context(context, extension) add_metatags(context)