From 457354c244676b7586e82bb8ba8cd58e98b7c2c6 Mon Sep 17 00:00:00 2001 From: John Anderson Date: Tue, 17 Mar 2020 00:03:58 -0400 Subject: [PATCH] inject origional context as obj_context --- netbox/extras/plugins/__init__.py | 11 ++++++--- netbox/extras/templatetags/plugins.py | 32 +++++++++++++-------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/netbox/extras/plugins/__init__.py b/netbox/extras/plugins/__init__.py index 46ca58336..af94b9373 100644 --- a/netbox/extras/plugins/__init__.py +++ b/netbox/extras/plugins/__init__.py @@ -18,15 +18,20 @@ class PluginTemplateContent: """ model = None - def __init__(self, obj): + def __init__(self, obj, context): self.obj = obj + self.context = context def render(self, template, extra_context=None): """ Convenience menthod for rendering the provided template name. The detail page object is automatically - passed into the template context as `obj` but an additional context dictionary may be passed as `extra_context`. + passed into the template context as `obj` and the origional detail page's context is available as + `obj_context`. An additional context dictionary may be passed as `extra_context`. """ - context = {'obj': self.obj} + context = { + 'obj': self.obj, + 'obj_context': self.context + } if isinstance(extra_context, dict): context.update(extra_context) diff --git a/netbox/extras/templatetags/plugins.py b/netbox/extras/templatetags/plugins.py index c4a3002e7..384b08b6a 100644 --- a/netbox/extras/templatetags/plugins.py +++ b/netbox/extras/templatetags/plugins.py @@ -8,16 +8,16 @@ from extras.plugins import get_content_classes register = template_.Library() -def _get_registered_content(obj, method): +def _get_registered_content(obj, method, context): """ - Given an object and a PluginTemplateContent method name, return all the registered content for the - object's model. + Given an object and a PluginTemplateContent method name and the template context, return all the + registered content for the object's model. """ html = '' plugin_template_classes = get_content_classes(obj._meta.label_lower) for plugin_template_class in plugin_template_classes: - plugin_template_renderer = plugin_template_class(obj) + plugin_template_renderer = plugin_template_class(obj, context) try: content = getattr(plugin_template_renderer, method)() except NotImplementedError: @@ -28,33 +28,33 @@ def _get_registered_content(obj, method): return mark_safe(html) -@register.simple_tag() -def plugin_buttons(obj): +@register.simple_tag(takes_context=True) +def plugin_buttons(context, obj): """ Fire signal to collect all buttons registered by plugins """ - return _get_registered_content(obj, 'buttons') + return _get_registered_content(obj, 'buttons', context) -@register.simple_tag() -def plugin_left_page(obj): +@register.simple_tag(takes_context=True) +def plugin_left_page(context, obj): """ Fire signal to collect all left page content registered by plugins """ - return _get_registered_content(obj, 'left_page') + return _get_registered_content(obj, 'left_page', context) -@register.simple_tag() -def plugin_right_page(obj): +@register.simple_tag(takes_context=True) +def plugin_right_page(context, obj): """ Fire signal to collect all right page content registered by plugins """ - return _get_registered_content(obj, 'right_page') + return _get_registered_content(obj, 'right_page', context) -@register.simple_tag() -def plugin_full_width_page(obj): +@register.simple_tag(takes_context=True) +def plugin_full_width_page(context, obj): """ Fire signal to collect all full width page content registered by plugins """ - return _get_registered_content(obj, 'full_width_page') + return _get_registered_content(obj, 'full_width_page', context)