1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Move utilities.utils.get_viewname() to utilities.views

This commit is contained in:
Jeremy Stretch
2024-03-21 12:20:24 -04:00
parent 2719fa3b5a
commit 3547ea376c
12 changed files with 50 additions and 43 deletions

View File

@@ -4,6 +4,7 @@ from django.urls import reverse
from django.urls.exceptions import NoReverseMatch
from django.utils.translation import gettext_lazy as _
from netbox.plugins import PluginConfig
from netbox.registry import registry
from .permissions import resolve_permission
@@ -12,6 +13,7 @@ __all__ = (
'GetReturnURLMixin',
'ObjectPermissionRequiredMixin',
'ViewTab',
'get_viewname',
'register_model_view',
)
@@ -180,6 +182,39 @@ class ViewTab:
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):
"""
This decorator can be used to "attach" a view to any model in NetBox. This is typically used to inject