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

Fixes #8764: Correct view name resolution for dynamic form fields

This commit is contained in:
jeremystretch
2022-02-28 15:17:49 -05:00
parent da6ed8ea11
commit d6c272cfd6
3 changed files with 25 additions and 12 deletions

View File

@@ -17,22 +17,35 @@ from extras.utils import is_taggable
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
def get_viewname(model, action=None):
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
"""
viewname = f'{model._meta.app_label}:{model._meta.model_name}'
is_plugin = isinstance(model._meta.app_config, PluginConfig)
app_label = model._meta.app_label
model_name = model._meta.model_name
# Determine whether this is a plugin view and adjust the namespace appropriately
if isinstance(model._meta.app_config, PluginConfig):
viewname = f'plugins:{viewname}'
if rest_api:
if is_plugin:
viewname = f'plugins-api:{app_label}:{model_name}'
else:
viewname = f'{app_label}-api:{model_name}'
# Append the action, if any
if action:
viewname = f'{viewname}-{action}'
# Append the action, if any
if action:
viewname = f'{viewname}_{action}'
else:
viewname = f'{app_label}:{model_name}'
# Prepend the plugins namespace if this is a plugin model
if is_plugin:
viewname = f'plugins:{viewname}'
# Append the action, if any
if action:
viewname = f'{viewname}_{action}'
return viewname