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

This commit is contained in:
Jeremy Stretch 2020-03-26 21:25:10 -04:00
parent f03cc96050
commit af302d8368

View File

@ -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)