Просмотр исходного кода

Closes #5926: Strip leading/trailing whitespace from TemplateColumns rendered for export

Jeremy Stretch 4 лет назад
Родитель
Сommit
1544823d73
1 измененных файлов с 13 добавлено и 0 удалено
  1. 13 0
      netbox/utilities/tables.py

+ 13 - 0
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