20048 cleanup get_viewname URL resolution (#20050)
Some checks are pending
CI / build (20.x, 3.10) (push) Waiting to run
CI / build (20.x, 3.11) (push) Waiting to run
CI / build (20.x, 3.12) (push) Waiting to run

* #20048 add get_action_url utility function

* #20048 add get_action_url utility function

* #20048 add get_action_url utility function

* #20048 add get_action_url utility function

* #20048 add get_action_url utility function

* #20048 action_url template tag

* #20048 action_url template tag

* #20048 fix test

* #20048 review feedback

* #20048 fix tags
This commit is contained in:
Arthur Hanson
2025-08-11 05:38:19 -07:00
committed by GitHub
parent 1242ad68f7
commit a585bc044e
24 changed files with 220 additions and 115 deletions

View File

@@ -20,6 +20,7 @@ __all__ = (
'GetReturnURLMixin',
'ObjectPermissionRequiredMixin',
'ViewTab',
'get_action_url',
'get_viewname',
'register_model_view',
)
@@ -150,7 +151,7 @@ class GetReturnURLMixin:
# Attempt to dynamically resolve the list view for the object
if hasattr(self, 'queryset'):
try:
return reverse(get_viewname(self.queryset.model, 'list'))
return get_action_url(self.queryset.model, action='list')
except NoReverseMatch:
pass
@@ -282,6 +283,22 @@ def get_viewname(model, action=None, rest_api=False):
return viewname
def get_action_url(model, action=None, rest_api=False, kwargs=None):
"""
Return the URL for the given model and action, if valid; otherwise raise NoReverseMatch.
Will defer to _get_action_url() on the model if it exists.
:param model: The model or instance to which the URL belongs
: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 action
:param kwargs: A dictionary of keyword arguments for the view to include when resolving its URL path (optional)
"""
if hasattr(model, '_get_action_url'):
return model._get_action_url(action, rest_api, kwargs)
return reverse(get_viewname(model, action, rest_api), kwargs=kwargs)
def register_model_view(model, name='', path=None, detail=True, kwargs=None):
"""
This decorator can be used to "attach" a view to any model in NetBox. This is typically used to inject