Refactor HTML formatting to use mark_safe for safe rendering in various tables and models

This commit is contained in:
Ross
2026-01-28 14:28:36 +00:00
parent a61c8529ae
commit a512c2ba09
9 changed files with 54 additions and 45 deletions
+20 -16
View File
@@ -19,6 +19,8 @@ from .models import (
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from easy_thumbnails.files import get_thumbnailer
from easy_thumbnails.exceptions import InvalidImageFormatError
@@ -83,12 +85,14 @@ class CaseTable(SelectionTable):
def get_view_cell(record):
if record.open_access:
return format_html(f"""<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-opencollective" viewBox="0 0 16 16">
<path fill-opacity=".4" d="M12.995 8.195c0 .937-.312 1.912-.78 2.693l1.99 1.99c.976-1.327 1.6-2.966 1.6-4.683 0-1.795-.624-3.434-1.561-4.76l-2.068 2.028c.468.781.78 1.679.78 2.732h.04Z"/>
<path d="M8 13.151a4.995 4.995 0 1 1 0-9.99c1.015 0 1.951.273 2.732.82l1.95-2.03a7.805 7.805 0 1 0 .04 12.449l-1.951-2.03a5.072 5.072 0 0 1-2.732.781z"/>
</svg> View""")
return mark_safe(
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-opencollective" viewBox="0 0 16 16">'
'<path fill-opacity=".4" d="M12.995 8.195c0 .937-.312 1.912-.78 2.693l1.99 1.99c.976-1.327 1.6-2.966 1.6-4.683 0-1.795-.624-3.434-1.561-4.76l-2.068 2.028c.468.781.78 1.679.78 2.732h.04Z"/>'
'<path d="M8 13.151a4.995 4.995 0 1 1 0-9.99c1.015 0 1.951.273 2.732.82l1.95-2.03a7.805 7.805 0 1 0 .04 12.449l-1.951-2.03a5.072 5.072 0 0 1-2.732.781z"/>'
'</svg> View'
)
else:
return f'View'
return 'View'
def render_author(self, value):
max_length = 15 # Set the maximum length for truncating individual author names
@@ -108,7 +112,7 @@ class CaseTable(SelectionTable):
)
)
# Join all authors with commas
return format_html(", ".join(authors))
return mark_safe(", ".join(authors))
def render_associated_cases(self, record):
links = []
@@ -130,7 +134,7 @@ class CaseTable(SelectionTable):
except Case.DoesNotExist:
pass
if links:
return format_html(", ".join(links))
return mark_safe(", ".join(links))
return "No associated cases"
def render_collections(self, record):
@@ -151,7 +155,7 @@ class CaseTable(SelectionTable):
)
for collection in collections
]
return format_html(", ".join(links))
return mark_safe(", ".join(links))
return "No collections"
associated_cases = tables.Column(
@@ -166,7 +170,7 @@ class CaseTable(SelectionTable):
"atlas:case_update", text="Edit", args=[A("pk")], orderable=False
)
view = tables.LinkColumn(
"atlas:case_detail", text=get_view_cell, args=[A("pk")], orderable=False
"atlas:case_detail", text=get_view_cell, args=[A("pk")], accessor="pk", orderable=False
)
clone = tables.LinkColumn(
"atlas:case_clone", text="Clone", args=[A("pk")], orderable=False
@@ -199,7 +203,7 @@ class PopupLinkColumn(tables.Column):
obj = value.first()
if obj is None:
return format_html("<span>No image<span>")
return mark_safe("<span>No image</span>")
return obj.get_thumbnail()[0]
@@ -209,7 +213,7 @@ class SeriesTable(SelectionTable):
"atlas:series_update", text="Edit", args=[A("pk")], orderable=False
)
view = tables.LinkColumn(
"atlas:series_detail", text="View", args=[A("pk")], orderable=False
"atlas:series_detail", text="View", args=[A("pk")], accessor="pk", orderable=False
)
popup = tables.Column(empty_values=(), orderable=False)
@@ -290,7 +294,7 @@ class ConditionTable(SelectionTable):
)
def render_synonym(self, value, record):
return format_html(record.get_synonym_link())
return mark_safe(record.get_synonym_link())
return f"{record.get_synonym_link()}"
@@ -317,7 +321,7 @@ class FindingTable(SelectionTable):
def render_synonym(self, value, record):
return format_html(record.get_synonym_link())
return mark_safe(record.get_synonym_link())
class StructureTable(SelectionTable):
@@ -344,7 +348,7 @@ class StructureTable(SelectionTable):
def render_synonym(self, value, record):
return format_html(record.get_synonym_link())
return mark_safe(record.get_synonym_link())
class PresentationTable(SelectionTable):
@@ -412,7 +416,7 @@ class SubspecialtyTable(SelectionTable):
def render_synonym(self, value, record):
return format_html(record.get_synonym_link())
return mark_safe(record.get_synonym_link())
return f"{record.get_synonym_link()}"
@@ -432,7 +436,7 @@ class CaseCollectionTable(SelectionTable):
"atlas:exam_update", text="Edit", args=[A("pk")], orderable=False
)
view = tables.LinkColumn(
"atlas:collection_detail", text="View", args=[A("pk")], orderable=False
"atlas:collection_detail", text="View", args=[A("pk")], accessor="pk", orderable=False
)
created = tables.DateTimeColumn(verbose_name="Created", accessor="created", orderable=True)