mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-21 02:58:43 -06:00
20048 cleanup get_viewname URL resolution (#20050)
* #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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user