From c5f74cce805e7fb4a73745a41ddf1b8a5388e8f2 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 13 Feb 2020 13:13:27 -0500 Subject: [PATCH] Introduce a common template for object list views --- netbox/templates/utilities/obj_list.html | 28 ++++++++++++++++++++++++ netbox/utilities/templatetags/helpers.py | 19 ++++++++++++++-- netbox/utilities/views.py | 10 +++++---- 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 netbox/templates/utilities/obj_list.html diff --git a/netbox/templates/utilities/obj_list.html b/netbox/templates/utilities/obj_list.html new file mode 100644 index 000000000..c06afaed8 --- /dev/null +++ b/netbox/templates/utilities/obj_list.html @@ -0,0 +1,28 @@ +{% extends '_base.html' %} +{% load buttons %} +{% load helpers %} + +{% block content %} +
+ {% if permissions.add %} + {% add_button content_type.model_class|url_name:"add" %} + {% import_button content_type.model_class|url_name:"import" %} + {% endif %} + {% export_button content_type %} +
+

{% block title %}{{ content_type.model_class|model_name_plural|bettertitle }}{% endblock %}

+
+ {% if filter_form %} +
+ {% include 'utilities/obj_table.html' with bulk_edit_url=content_type.model_class|url_name:"bulk_edit" bulk_delete_url=content_type.model_class|url_name:"bulk_delete" %} +
+
+ {% include 'inc/search_panel.html' %} +
+ {% else %} +
+ {% include 'utilities/obj_table.html' with bulk_edit_url=content_type.model_class|url_name:"bulk_edit" bulk_delete_url=content_type.model_class|url_name:"bulk_delete" %} +
+ {% endif %} +
+{% endblock %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index ae679c91a..dfeddd31a 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -1,9 +1,10 @@ import datetime import json import re -import yaml +import yaml from django import template +from django.urls import NoReverseMatch, reverse from django.utils.html import strip_tags from django.utils.safestring import mark_safe from markdown import markdown @@ -11,7 +12,6 @@ from markdown import markdown from utilities.choices import unpack_grouped_choices from utilities.utils import foreground_color - register = template.Library() @@ -101,6 +101,21 @@ def model_name_plural(obj): return obj._meta.verbose_name_plural +@register.filter() +def url_name(model, action): + """ + Return the URL name for the given model and action, or None if invalid. + """ + url_name = '{}:{}_{}'.format(model._meta.app_label, model._meta.model_name, action) + try: + # Validate and return the URL name. We don't return the actual URL yet because many of the templates + # are written to pass a name to {% url %}. + reverse(url_name) + return url_name + except NoReverseMatch: + return None + + @register.filter() def contains(value, arg): """ diff --git a/netbox/utilities/views.py b/netbox/utilities/views.py index 7c38aceee..7618031ed 100644 --- a/netbox/utilities/views.py +++ b/netbox/utilities/views.py @@ -71,7 +71,7 @@ class ObjectListView(View): filterset = None filterset_form = None table = None - template_name = None + template_name = 'utilities/obj_list.html' def queryset_to_yaml(self): """ @@ -156,9 +156,11 @@ class ObjectListView(View): # Provide a hook to tweak the queryset based on the request immediately prior to rendering the object list self.queryset = self.alter_queryset(request) - # Compile user model permissions for access from within the template - perm_base_name = '{}.{{}}_{}'.format(model._meta.app_label, model._meta.model_name) - permissions = {p: request.user.has_perm(perm_base_name.format(p)) for p in ['add', 'change', 'delete']} + # Compile a dictionary indicating which permissions are available to the current user for this model + permissions = {} + for action in ('add', 'change', 'delete', 'view'): + perm_name = '{}.{}_{}'.format(model._meta.app_label, action, model._meta.model_name) + permissions[action] = request.user.has_perm(perm_name) # Construct the table based on the user's permissions table = self.table(self.queryset)