|
@@ -4,6 +4,7 @@ from django.urls import reverse
|
|
|
from django.urls.exceptions import NoReverseMatch
|
|
from django.urls.exceptions import NoReverseMatch
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
|
+from netbox.plugins import PluginConfig
|
|
|
from netbox.registry import registry
|
|
from netbox.registry import registry
|
|
|
from .permissions import resolve_permission
|
|
from .permissions import resolve_permission
|
|
|
|
|
|
|
@@ -12,6 +13,7 @@ __all__ = (
|
|
|
'GetReturnURLMixin',
|
|
'GetReturnURLMixin',
|
|
|
'ObjectPermissionRequiredMixin',
|
|
'ObjectPermissionRequiredMixin',
|
|
|
'ViewTab',
|
|
'ViewTab',
|
|
|
|
|
+ 'get_viewname',
|
|
|
'register_model_view',
|
|
'register_model_view',
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -180,6 +182,39 @@ class ViewTab:
|
|
|
return self.badge
|
|
return self.badge
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+#
|
|
|
|
|
+# Utility functions
|
|
|
|
|
+#
|
|
|
|
|
+
|
|
|
|
|
+def get_viewname(model, action=None, rest_api=False):
|
|
|
|
|
+ """
|
|
|
|
|
+ Return the view name for the given model and action, if valid.
|
|
|
|
|
+
|
|
|
|
|
+ :param model: The model or instance to which the view applies
|
|
|
|
|
+ :param action: A string indicating the desired action (if any); e.g. "add" or "list"
|
|
|
|
|
+ :param rest_api: A boolean indicating whether this is a REST API view
|
|
|
|
|
+ """
|
|
|
|
|
+ is_plugin = isinstance(model._meta.app_config, PluginConfig)
|
|
|
|
|
+ app_label = model._meta.app_label
|
|
|
|
|
+ model_name = model._meta.model_name
|
|
|
|
|
+
|
|
|
|
|
+ if rest_api:
|
|
|
|
|
+ viewname = f'{app_label}-api:{model_name}'
|
|
|
|
|
+ if is_plugin:
|
|
|
|
|
+ viewname = f'plugins-api:{viewname}'
|
|
|
|
|
+ if action:
|
|
|
|
|
+ viewname = f'{viewname}-{action}'
|
|
|
|
|
+
|
|
|
|
|
+ else:
|
|
|
|
|
+ viewname = f'{app_label}:{model_name}'
|
|
|
|
|
+ if is_plugin:
|
|
|
|
|
+ viewname = f'plugins:{viewname}'
|
|
|
|
|
+ if action:
|
|
|
|
|
+ viewname = f'{viewname}_{action}'
|
|
|
|
|
+
|
|
|
|
|
+ return viewname
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def register_model_view(model, name='', path=None, kwargs=None):
|
|
def register_model_view(model, name='', path=None, kwargs=None):
|
|
|
"""
|
|
"""
|
|
|
This decorator can be used to "attach" a view to any model in NetBox. This is typically used to inject
|
|
This decorator can be used to "attach" a view to any model in NetBox. This is typically used to inject
|