diff --git a/docs/plugins/development/views.md b/docs/plugins/development/views.md index 45d7064cf..6d1329a4a 100644 --- a/docs/plugins/development/views.md +++ b/docs/plugins/development/views.md @@ -187,11 +187,16 @@ Plugins can inject custom content into certain areas of the detail views of appl * `full_width_page()` - Inject content across the entire bottom of the page * `buttons()` - Add buttons to the top of the page +Plugins can also inject custom content into certain areas of the list views of applicable models using the same subclass of `PluginTemplateExtension`. One method is available: + +* `list_buttons()` - Add buttons to the top of the list view page + Additionally, a `render()` method is available for convenience. This method accepts the name of a template to render, and any additional context data you want to pass. Its use is optional, however. When a PluginTemplateExtension is instantiated, context data is assigned to `self.context`. Available data include: -* `object` - The object being viewed +* `object` - The object being viewed (for detail views only) +* `model` - The model of the list view (for list views only) * `request` - The current request * `settings` - Global NetBox settings * `config` - Plugin-specific configuration parameters diff --git a/netbox/extras/plugins/templates.py b/netbox/extras/plugins/templates.py index 5f3d038c6..e9b9a9dca 100644 --- a/netbox/extras/plugins/templates.py +++ b/netbox/extras/plugins/templates.py @@ -63,3 +63,11 @@ class PluginTemplateExtension: automatically handled. """ raise NotImplementedError + + def list_buttons(self): + """ + Buttons that will be rendered and added to the existing list of buttons on the list view. Content + should be returned as an HTML string. Note that content does not need to be marked as safe because this is + automatically handled. + """ + raise NotImplementedError diff --git a/netbox/extras/templatetags/plugins.py b/netbox/extras/templatetags/plugins.py index b2f4ec0a7..560d15e01 100644 --- a/netbox/extras/templatetags/plugins.py +++ b/netbox/extras/templatetags/plugins.py @@ -73,3 +73,11 @@ 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) diff --git a/netbox/extras/tests/dummy_plugin/template_content.py b/netbox/extras/tests/dummy_plugin/template_content.py index 6151454ea..364768a22 100644 --- a/netbox/extras/tests/dummy_plugin/template_content.py +++ b/netbox/extras/tests/dummy_plugin/template_content.py @@ -16,5 +16,8 @@ class SiteContent(PluginTemplateExtension): def buttons(self): return "SITE CONTENT - BUTTONS" + def list_buttons(self): + return "SITE CONTENT - LIST BUTTONS" + template_extensions = [SiteContent] diff --git a/netbox/templates/generic/object_list.html b/netbox/templates/generic/object_list.html index c58565c31..98a09b0b0 100644 --- a/netbox/templates/generic/object_list.html +++ b/netbox/templates/generic/object_list.html @@ -1,6 +1,7 @@ {% extends 'base/layout.html' %} {% load buttons %} {% load helpers %} +{% load plugins %} {% load render_table from django_tables2 %} {% load static %} @@ -24,6 +25,8 @@ Context: {% block controls %}
+ {% plugin_list_buttons model %} + {% block extra_controls %}{% endblock %} {% if 'add' in actions %} {% add_button model %}