mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
#9072: Implement a mechanism for dynamically registering model detail views
This commit is contained in:
8
netbox/utilities/templates/tabs/model_view_tabs.html
Normal file
8
netbox/utilities/templates/tabs/model_view_tabs.html
Normal file
@@ -0,0 +1,8 @@
|
||||
{% for tab in tabs %}
|
||||
<li role="presentation" class="nav-item">
|
||||
<a href="{{ tab.url }}" class="nav-link{% if tab.is_active %} active{% endif %}">
|
||||
{{ tab.label }}
|
||||
{% if tab.badge_value %}{% badge tab.badge_value %}{% endif %}
|
||||
</a>
|
||||
</li>
|
||||
{% endfor %}
|
50
netbox/utilities/templatetags/tabs.py
Normal file
50
netbox/utilities/templatetags/tabs.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from django import template
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.urls import reverse
|
||||
|
||||
from extras.registry import registry
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
#
|
||||
# Object detail view tabs
|
||||
#
|
||||
|
||||
@register.inclusion_tag('tabs/model_view_tabs.html', takes_context=True)
|
||||
def model_view_tabs(context, instance):
|
||||
app_label = instance._meta.app_label
|
||||
model_name = instance._meta.model_name
|
||||
user = context['request'].user
|
||||
tabs = []
|
||||
|
||||
# Retrieve registered views for this model
|
||||
try:
|
||||
views = registry['views'][app_label][model_name]
|
||||
except KeyError:
|
||||
# No views have been registered for this model
|
||||
views = []
|
||||
|
||||
# Compile a list of tabs to be displayed in the UI
|
||||
for view in views:
|
||||
if view['tab_label'] and (not view['tab_permission'] or user.has_perm(view['tab_permission'])):
|
||||
|
||||
# Determine the value of the tab's badge (if any)
|
||||
if view['tab_badge'] and callable(view['tab_badge']):
|
||||
badge_value = view['tab_badge'](instance)
|
||||
elif view['tab_badge']:
|
||||
badge_value = view['tab_badge']
|
||||
else:
|
||||
badge_value = None
|
||||
|
||||
tabs.append({
|
||||
'name': view['name'],
|
||||
'url': reverse(f"{app_label}:{model_name}_{view['name']}", args=[instance.pk]),
|
||||
'label': view['tab_label'],
|
||||
'badge_value': badge_value,
|
||||
'is_active': context.get('active_tab') == view['name'],
|
||||
})
|
||||
|
||||
return {
|
||||
'tabs': tabs,
|
||||
}
|
35
netbox/utilities/urls.py
Normal file
35
netbox/utilities/urls.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from django.urls import path
|
||||
from django.utils.module_loading import import_string
|
||||
from django.views.generic import View
|
||||
|
||||
from extras.registry import registry
|
||||
|
||||
|
||||
def get_model_urls(app_label, model_name):
|
||||
"""
|
||||
Return a list of URL paths for detail views registered to the given model.
|
||||
|
||||
Args:
|
||||
app_label: App/plugin name
|
||||
model_name: Model name
|
||||
"""
|
||||
paths = []
|
||||
|
||||
# Retrieve registered views for this model
|
||||
try:
|
||||
views = registry['views'][app_label][model_name]
|
||||
except KeyError:
|
||||
# No views have been registered for this model
|
||||
views = []
|
||||
|
||||
for view in views:
|
||||
# Import the view class or function
|
||||
callable = import_string(view['path'])
|
||||
if issubclass(callable, View):
|
||||
callable = callable.as_view()
|
||||
# Create a path to the view
|
||||
paths.append(
|
||||
path(f"{view['name']}/", callable, name=f"{model_name}_{view['name']}", kwargs=view['kwargs'])
|
||||
)
|
||||
|
||||
return paths
|
@@ -3,8 +3,16 @@ from django.core.exceptions import ImproperlyConfigured
|
||||
from django.urls import reverse
|
||||
from django.urls.exceptions import NoReverseMatch
|
||||
|
||||
from extras.registry import registry
|
||||
from .permissions import resolve_permission
|
||||
|
||||
__all__ = (
|
||||
'ContentTypePermissionRequiredMixin',
|
||||
'GetReturnURLMixin',
|
||||
'ObjectPermissionRequiredMixin',
|
||||
'register_model_view',
|
||||
)
|
||||
|
||||
|
||||
#
|
||||
# View Mixins
|
||||
@@ -122,3 +130,33 @@ class GetReturnURLMixin:
|
||||
|
||||
# If all else fails, return home. Ideally this should never happen.
|
||||
return reverse('home')
|
||||
|
||||
|
||||
def register_model_view(model, name, view_path, tab_label=None, tab_badge=None, tab_permission=None, kwargs=None):
|
||||
"""
|
||||
Register a subview for a core model.
|
||||
|
||||
Args:
|
||||
model: The Django model class with which this view will be associated
|
||||
name: The name to register when creating a URL path
|
||||
view_path: A dotted path to the view class or function (e.g. 'myplugin.views.FooView')
|
||||
tab_label: The label to display for the view's tab under the model view (optional)
|
||||
tab_badge: A static value or callable to display a badge within the view's tab (optional). If a callable is
|
||||
specified, it must accept the current object as its single positional argument.
|
||||
tab_permission: The name of the permission required to display the tab (optional)
|
||||
kwargs: A dictionary of keyword arguments to send to the view (optional)
|
||||
"""
|
||||
app_label = model._meta.app_label
|
||||
model_name = model._meta.model_name
|
||||
|
||||
if model_name not in registry['views'][app_label]:
|
||||
registry['views'][app_label][model_name] = []
|
||||
|
||||
registry['views'][app_label][model_name].append({
|
||||
'name': name,
|
||||
'path': view_path,
|
||||
'tab_label': tab_label,
|
||||
'tab_badge': tab_badge,
|
||||
'tab_permission': tab_permission,
|
||||
'kwargs': kwargs or {},
|
||||
})
|
||||
|
Reference in New Issue
Block a user