mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-25 01:48:38 -06:00
Avoid instantiating PluginTemplateExtension subclasses when the specified method has not been defined
This commit is contained in:
parent
f03cc96050
commit
af302d8368
@ -2,6 +2,7 @@ from django import template as template_
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
|
from extras.plugins import PluginTemplateExtension
|
||||||
from extras.registry import registry
|
from extras.registry import registry
|
||||||
|
|
||||||
register = template_.Library()
|
register = template_.Library()
|
||||||
@ -17,23 +18,24 @@ def _get_registered_content(obj, method, template_context):
|
|||||||
'obj': obj,
|
'obj': obj,
|
||||||
'request': template_context['request'],
|
'request': template_context['request'],
|
||||||
'settings': template_context['settings'],
|
'settings': template_context['settings'],
|
||||||
'config': {}, # Defined per-plugin
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model_name = obj._meta.label_lower
|
model_name = obj._meta.label_lower
|
||||||
template_extensions = registry['plugin_template_extensions'].get(model_name, [])
|
template_extensions = registry['plugin_template_extensions'].get(model_name, [])
|
||||||
for template_extension in template_extensions:
|
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
|
# Update context with plugin-specific configuration parameters
|
||||||
plugin_name = template_extension.__module__.split('.')[0]
|
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)
|
instance = template_extension(context)
|
||||||
try:
|
content = getattr(instance, method)()
|
||||||
content = getattr(instance, method)()
|
|
||||||
except NotImplementedError:
|
|
||||||
# This content renderer class does not define content for this method
|
|
||||||
continue
|
|
||||||
html += content
|
html += content
|
||||||
|
|
||||||
return mark_safe(html)
|
return mark_safe(html)
|
||||||
|
Loading…
Reference in New Issue
Block a user