From 1544823d73205ee7d4d276f3316ee7612a84deb4 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 26 Mar 2021 16:20:01 -0400 Subject: [PATCH] Closes #5926: Strip leading/trailing whitespace from TemplateColumns rendered for export --- netbox/utilities/tables.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/netbox/utilities/tables.py b/netbox/utilities/tables.py index 4d5066de8..3d48d9875 100644 --- a/netbox/utilities/tables.py +++ b/netbox/utilities/tables.py @@ -4,6 +4,7 @@ from django.contrib.contenttypes.fields import GenericForeignKey from django.core.exceptions import FieldDoesNotExist from django.db.models.fields.related import RelatedField from django.urls import reverse +from django.utils.html import strip_tags from django.utils.safestring import mark_safe from django_tables2 import RequestConfig from django_tables2.data import TableQuerysetData @@ -11,6 +12,18 @@ from django_tables2.data import TableQuerysetData from .paginator import EnhancedPaginator, get_paginate_count +def stripped_value(self, value): + """ + Replaces TemplateColumn's value() method to both strip HTML tags and remove any leading/trailing whitespace. + """ + return strip_tags(value).strip() + + +# TODO: We're monkey-patching TemplateColumn here to strip leading/trailing whitespace. This will no longer +# be necessary if django-tables2 PR #794 is accepted. (See #5926) +tables.TemplateColumn.value = stripped_value + + class BaseTable(tables.Table): """ Default table for object lists