mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-18 19:32:24 -06:00
Move utilities.utils.get_viewname() to utilities.views
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user