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

@@ -5,7 +5,7 @@ from django.forms import BoundField
from django.urls import reverse
from utilities.forms import widgets
from utilities.utils import get_viewname
from utilities.views import get_viewname
__all__ = (
'DynamicChoiceField',

View File

@@ -4,7 +4,8 @@ from django.urls import NoReverseMatch, reverse
from core.models import ObjectType
from extras.models import Bookmark, ExportTemplate
from utilities.utils import get_viewname, prepare_cloned_fields
from utilities.utils import prepare_cloned_fields
from utilities.views import get_viewname
__all__ = (
'add_button',

View File

@@ -12,7 +12,7 @@ from django.utils.safestring import mark_safe
from core.models import ObjectType
from utilities.forms import get_selected_values, TableConfigForm
from utilities.utils import get_viewname
from utilities.views import get_viewname
__all__ = (
'annotated_date',

View File

@@ -4,7 +4,7 @@ from django.urls.exceptions import NoReverseMatch
from django.utils.module_loading import import_string
from netbox.registry import registry
from utilities.utils import get_viewname
from utilities.views import get_viewname
__all__ = (
'model_view_tabs',

View File

@@ -21,41 +21,11 @@ from mptt.models import MPTTModel
from dcim.choices import CableLengthUnitChoices, WeightUnitChoices
from extras.utils import is_taggable
from netbox.config import get_config
from netbox.plugins import PluginConfig
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
from .constants import HTML_ALLOWED_ATTRIBUTES, HTML_ALLOWED_TAGS
from .string import title
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 csv_format(data):
"""
Encapsulate any data which contains a comma within double quotes.

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