From cde153d75b817002b77cdc6a09ed011bdec2ff84 Mon Sep 17 00:00:00 2001 From: Felipe Orellana Date: Mon, 19 Sep 2016 10:17:22 -0700 Subject: [PATCH 1/2] 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) From 417a4fcc6681d94051cb24fb94f11e6bf3ea76b2 Mon Sep 17 00:00:00 2001 From: Felipe Orellana Date: Tue, 20 Sep 2016 10:26:25 -0400 Subject: [PATCH 2/2] Renamed hook Renamed hook to extend_website_page_controller_context to better fit the definition of a website page controller. --- frappe/website/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/website/context.py b/frappe/website/context.py index bb790b05f6..dee2934037 100644 --- a/frappe/website/context.py +++ b/frappe/website/context.py @@ -85,7 +85,7 @@ def build_context(context): update_controller_context(context, context.controller) # controller context extensions - context_controller_hooks = frappe.get_hooks("extend_controller_context") or {} + context_controller_hooks = frappe.get_hooks("extend_website_page_controller_context") or {} for controller, extension in context_controller_hooks.items(): if isinstance(extension, list): for ext in extension: