Hide exception in ObjectCountsWidget for models without a xxx_list view function (#17342)

* Hide exception in ObjectCountsWidget for models without a `xxx_list` view function

Fixes #17341

* Disable hyperlink for invalid view names

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Brian Candler 2024-09-03 14:38:06 +01:00 committed by GitHub
parent 814b699204
commit 7a2ff96abe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 3 deletions

View File

@ -183,10 +183,13 @@ class ObjectCountsWidget(DashboardWidget):
for model in get_models_from_content_types(self.config['models']):
permission = get_permission_for_model(model, 'view')
if request.user.has_perm(permission):
url = reverse(get_viewname(model, 'list'))
try:
url = reverse(get_viewname(model, 'list'))
except NoReverseMatch:
url = None
qs = model.objects.restrict(request.user, 'view')
# Apply any specified filters
if filters := self.config.get('filters'):
if url and (filters := self.config.get('filters')):
params = dict_to_querydict(filters)
filterset = getattr(resolve(url).func.view_class, 'filterset', None)
qs = filterset(params, qs).qs

View File

@ -3,7 +3,7 @@
{% if counts %}
<div class="list-group list-group-flush">
{% for model, count, url in counts %}
<a href="{{ url }}" class="list-group-item list-group-item-action px-1 py-2">
<a {% if url %}href="{{ url }}" {% endif %}class="list-group-item list-group-item-action px-1 py-2">
<div class="d-flex w-100 justify-content-between align-items-center">
{{ model|meta:"verbose_name_plural"|bettertitle }}
{% if count is None %}