mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
* Move extras.plugins to netbox.plugins & add deprecation warnings * Move plugin template tags from extras to utilities * Move plugins tests from extras to netbox * Add TODO reminders for v4.0
This commit is contained in:
83
netbox/utilities/templatetags/plugins.py
Normal file
83
netbox/utilities/templatetags/plugins.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from django import template as template_
|
||||
from django.conf import settings
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from netbox.plugins import PluginTemplateExtension
|
||||
from netbox.registry import registry
|
||||
|
||||
register = template_.Library()
|
||||
|
||||
|
||||
def _get_registered_content(obj, method, template_context):
|
||||
"""
|
||||
Given an object and a PluginTemplateExtension method name and the template context, return all the
|
||||
registered content for the object's model.
|
||||
"""
|
||||
html = ''
|
||||
context = {
|
||||
'object': obj,
|
||||
'request': template_context['request'],
|
||||
'settings': template_context['settings'],
|
||||
'csrf_token': template_context['csrf_token'],
|
||||
'perms': template_context['perms'],
|
||||
}
|
||||
|
||||
model_name = obj._meta.label_lower
|
||||
template_extensions = registry['plugins']['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, {})
|
||||
|
||||
# Call the method to render content
|
||||
instance = template_extension(context)
|
||||
content = getattr(instance, method)()
|
||||
html += content
|
||||
|
||||
return mark_safe(html)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def plugin_buttons(context, obj):
|
||||
"""
|
||||
Render all buttons registered by plugins
|
||||
"""
|
||||
return _get_registered_content(obj, 'buttons', context)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def plugin_left_page(context, obj):
|
||||
"""
|
||||
Render all left page content registered by plugins
|
||||
"""
|
||||
return _get_registered_content(obj, 'left_page', context)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def plugin_right_page(context, obj):
|
||||
"""
|
||||
Render all right page content registered by plugins
|
||||
"""
|
||||
return _get_registered_content(obj, 'right_page', context)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def plugin_full_width_page(context, obj):
|
||||
"""
|
||||
Render all full width page content registered by plugins
|
||||
"""
|
||||
return _get_registered_content(obj, 'full_width_page', context)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def plugin_list_buttons(context, model):
|
||||
"""
|
||||
Render all list buttons registered by plugins
|
||||
"""
|
||||
return _get_registered_content(model, 'list_buttons', context)
|
@@ -19,9 +19,9 @@ from jinja2.sandbox import SandboxedEnvironment
|
||||
from mptt.models import MPTTModel
|
||||
|
||||
from dcim.choices import CableLengthUnitChoices, WeightUnitChoices
|
||||
from extras.plugins import PluginConfig
|
||||
from extras.utils import is_taggable
|
||||
from netbox.config import get_config
|
||||
from netbox.plugins import PluginConfig
|
||||
from urllib.parse import urlencode
|
||||
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
|
||||
|
||||
|
Reference in New Issue
Block a user