2
0
Эх сурвалжийг харах

Avoid instantiating PluginTemplateExtension subclasses when the specified method has not been defined

Jeremy Stretch 5 жил өмнө
parent
commit
af302d8368

+ 9 - 7
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)