From 128327b8a3e4953234832856a8280f854e2b2f8b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 25 Jun 2020 16:50:35 -0400 Subject: [PATCH] Split url_name template filter into viewname() and validated_viewname() --- netbox/templates/dcim/device_component.html | 8 ++++---- netbox/templates/utilities/obj_list.html | 6 +++--- netbox/utilities/templatetags/helpers.py | 22 ++++++++++++++++++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/netbox/templates/dcim/device_component.html b/netbox/templates/dcim/device_component.html index 616655066..9fa66502e 100644 --- a/netbox/templates/dcim/device_component.html +++ b/netbox/templates/dcim/device_component.html @@ -9,7 +9,7 @@ @@ -17,12 +17,12 @@
{% plugin_buttons instance %} {% if request.user|can_change:instance %} - + Edit {% endif %} {% if request.user|can_delete:instance %} - + Delete {% endif %} @@ -34,7 +34,7 @@ {% if perms.extras.view_objectchange %} {% endif %} diff --git a/netbox/templates/utilities/obj_list.html b/netbox/templates/utilities/obj_list.html index 85ff050ed..47f11e1c1 100644 --- a/netbox/templates/utilities/obj_list.html +++ b/netbox/templates/utilities/obj_list.html @@ -9,10 +9,10 @@ {% endif %} {% if permissions.add and 'add' in action_buttons %} - {% add_button content_type.model_class|url_name:"add" %} + {% add_button content_type.model_class|validated_viewname:"add" %} {% endif %} {% if permissions.add and 'import' in action_buttons %} - {% import_button content_type.model_class|url_name:"import" %} + {% import_button content_type.model_class|validated_viewname:"import" %} {% endif %} {% if 'export' in action_buttons %} {% export_button content_type %} @@ -21,7 +21,7 @@

{% block title %}{{ content_type.model_class|meta:"verbose_name_plural"|bettertitle }}{% endblock %}

- {% with bulk_edit_url=content_type.model_class|url_name:"bulk_edit" bulk_delete_url=content_type.model_class|url_name:"bulk_delete" %} + {% with bulk_edit_url=content_type.model_class|validated_viewname:"bulk_edit" bulk_delete_url=content_type.model_class|validated_viewname:"bulk_delete" %} {% if permissions.change or permissions.delete %}
{% csrf_token %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 425a2fca2..e6a245a04 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -5,6 +5,7 @@ import re import yaml from django import template from django.conf import settings +from django.urls import NoReverseMatch, reverse from django.utils.html import strip_tags from django.utils.safestring import mark_safe from markdown import markdown @@ -74,11 +75,26 @@ def meta(obj, attr): @register.filter() -def url_name(model, action): +def viewname(model, action): """ - Return the URL name for the given model and action, or None if invalid. + Return the view name for the given model and action. Does not perform any validation. """ - return '{}:{}_{}'.format(model._meta.app_label, model._meta.model_name, action) + return f'{model._meta.app_label}:{model._meta.model_name}_{action}' + + +@register.filter() +def validated_viewname(model, action): + """ + Return the view name for the given model and action if valid, or None if invalid. + """ + viewname = f'{model._meta.app_label}:{model._meta.model_name}_{action}' + try: + # Validate and return the view name. We don't return the actual URL yet because many of the templates + # are written to pass a name to {% url %}. + reverse(viewname) + return viewname + except NoReverseMatch: + return None @register.filter()