From af302d8368ceb31e246ee8a0853972426c89e0a4 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 26 Mar 2020 21:25:10 -0400 Subject: [PATCH] Avoid instantiating PluginTemplateExtension subclasses when the specified method has not been defined --- netbox/extras/templatetags/plugins.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/netbox/extras/templatetags/plugins.py b/netbox/extras/templatetags/plugins.py index 63c126fc8..e3d1d0e46 100644 --- a/netbox/extras/templatetags/plugins.py +++ b/netbox/extras/templatetags/plugins.py @@ -2,6 +2,7 @@ from django import template as template_ from django.conf import settings from django.utils.safestring import mark_safe +from extras.plugins import PluginTemplateExtension from extras.registry import registry register = template_.Library() @@ -17,23 +18,24 @@ def _get_registered_content(obj, method, template_context): 'obj': obj, 'request': template_context['request'], 'settings': template_context['settings'], - 'config': {}, # Defined per-plugin } model_name = obj._meta.label_lower template_extensions = registry['plugin_template_extensions'].get(model_name, []) for template_extension in template_extensions: + # If the class has not overridden the specified method, we can skip it (because we know it + # will raise NotImplementedError). + if getattr(template_extension, method) == getattr(PluginTemplateExtension, method): + continue + # Update context with plugin-specific configuration parameters plugin_name = template_extension.__module__.split('.')[0] - context['config'] = settings.PLUGINS_CONFIG.get(plugin_name) + context['config'] = settings.PLUGINS_CONFIG.get(plugin_name, {}) + # Call the method to render content instance = template_extension(context) - try: - content = getattr(instance, method)() - except NotImplementedError: - # This content renderer class does not define content for this method - continue + content = getattr(instance, method)() html += content return mark_safe(html)