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:
@ -17,7 +17,8 @@ from extras.choices import BookmarkOrderingChoices
|
||||
from netbox.choices import ButtonColorChoices
|
||||
from utilities.permissions import get_permission_for_model
|
||||
from utilities.templatetags.builtins.filters import render_markdown
|
||||
from utilities.utils import content_type_identifier, content_type_name, dict_to_querydict, get_viewname
|
||||
from utilities.utils import content_type_identifier, content_type_name, dict_to_querydict
|
||||
from utilities.views import get_viewname
|
||||
from .utils import register_widget
|
||||
|
||||
__all__ = (
|
||||
|
@ -22,8 +22,8 @@ from utilities.forms import ConfirmationForm, get_field_value
|
||||
from utilities.paginator import EnhancedPaginator, get_paginate_count
|
||||
from utilities.rqworker import get_workers_for_queue
|
||||
from utilities.templatetags.builtins.filters import render_markdown
|
||||
from utilities.utils import copy_safe_request, count_related, get_viewname, normalize_querydict, shallow_compare_dict
|
||||
from utilities.views import ContentTypePermissionRequiredMixin, register_model_view
|
||||
from utilities.utils import copy_safe_request, count_related, normalize_querydict, shallow_compare_dict
|
||||
from utilities.views import ContentTypePermissionRequiredMixin, get_viewname, register_model_view
|
||||
from . import filtersets, forms, tables
|
||||
from .models import *
|
||||
from .scripts import run_script
|
||||
|
@ -20,7 +20,8 @@ from django_tables2.utils import Accessor
|
||||
from extras.choices import CustomFieldTypeChoices
|
||||
from utilities.permissions import get_permission_for_model
|
||||
from utilities.templatetags.builtins.filters import render_markdown
|
||||
from utilities.utils import content_type_identifier, content_type_name, get_viewname
|
||||
from utilities.utils import content_type_identifier, content_type_name
|
||||
from utilities.views import get_viewname
|
||||
|
||||
__all__ = (
|
||||
'ActionsColumn',
|
||||
|
@ -19,7 +19,7 @@ from netbox.tables import columns
|
||||
from utilities.paginator import EnhancedPaginator, get_paginate_count
|
||||
from utilities.html import highlight
|
||||
from utilities.string import title
|
||||
from utilities.utils import get_viewname
|
||||
from utilities.views import get_viewname
|
||||
from .template_code import *
|
||||
|
||||
__all__ = (
|
||||
|
@ -24,8 +24,7 @@ from utilities.exceptions import AbortRequest, AbortTransaction, PermissionsViol
|
||||
from utilities.forms import BulkRenameForm, ConfirmationForm, restrict_form_fields
|
||||
from utilities.forms.bulk_import import BulkImportForm
|
||||
from utilities.permissions import get_permission_for_model
|
||||
from utilities.utils import get_viewname
|
||||
from utilities.views import GetReturnURLMixin
|
||||
from utilities.views import GetReturnURLMixin, get_viewname
|
||||
from .base import BaseMultiObjectView
|
||||
from .mixins import ActionsMixin, TableMixin
|
||||
from .utils import get_prerequisite_model
|
||||
|
@ -18,8 +18,8 @@ from utilities.error_handlers import handle_protectederror
|
||||
from utilities.exceptions import AbortRequest, PermissionsViolation
|
||||
from utilities.forms import ConfirmationForm, restrict_form_fields
|
||||
from utilities.permissions import get_permission_for_model
|
||||
from utilities.utils import get_viewname, normalize_querydict, prepare_cloned_fields
|
||||
from utilities.views import GetReturnURLMixin
|
||||
from utilities.utils import normalize_querydict, prepare_cloned_fields
|
||||
from utilities.views import GetReturnURLMixin, get_viewname
|
||||
from .base import BaseObjectView
|
||||
from .mixins import ActionsMixin, TableMixin
|
||||
from .utils import get_prerequisite_model
|
||||
|
@ -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