Skip to content

Commit 196f840

Browse files
committed
refactor: Use mark_safe for HTML content in admin views
Updated the admin views in digest_admin, feed_admin, and tag_admin to use mark_safe with format_html for rendering HTML content safely. This change enhances security by ensuring that the HTML is properly marked as safe for rendering. Additionally, modified the status_icon function in modelAdmin_utils to apply the same approach for status icons.
1 parent 81a8597 commit 196f840

4 files changed

Lines changed: 15 additions & 10 deletions

File tree

core/admin/digest_admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def show_tags(self, obj):
241241
f"<a href='{reverse('admin:core_tag_change', args=[t.id])}'>#{t.name}</a>"
242242
for t in obj.tags.all()
243243
)
244-
return format_html(tags_html)
244+
return format_html("{}", mark_safe(tags_html))
245245

246246
@admin.display(description=_("AI Agent"))
247247
def summarizer_name(self, obj):

core/admin/feed_admin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def translation_options(self, obj):
273273
html_content += f"✔️{_('Content')}<br>"
274274
if obj.summary:
275275
html_content += f"✔️{_('Summary')}<br>"
276-
return format_html(html_content)
276+
return format_html("{}", mark_safe(html_content))
277277

278278
@admin.display(description=_("Log"))
279279
def show_log(self, obj):
@@ -330,7 +330,7 @@ def show_filters(self, obj):
330330
f"<a href='{reverse('admin:core_filter_change', args=[f.id])}'>{f.name}</a>"
331331
for f in obj.filters.all()
332332
)
333-
return format_html(filters_html)
333+
return format_html("{}", mark_safe(filters_html))
334334

335335
@admin.display(description=_("tags"))
336336
def show_tags(self, obj):
@@ -340,7 +340,7 @@ def show_tags(self, obj):
340340
f"<a href='{reverse('admin:core_tag_change', args=[t.id])}'>#{t.name}</a>"
341341
for t in obj.tags.all()
342342
)
343-
return format_html(tags_html)
343+
return format_html("{}", mark_safe(tags_html))
344344

345345

346346
core_admin_site.register(Feed, FeedAdmin)

core/admin/tag_admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from django.contrib import admin
3-
from django.utils.html import format_html
3+
from django.utils.html import format_html, mark_safe
44
from django.utils.translation import gettext_lazy as _
55
from django.urls import reverse
66
from core.models import Tag
@@ -33,7 +33,7 @@ def show_filters(self, obj):
3333
f"<a href='{reverse('admin:core_filter_change', args=[f.id])}'>{f.name}</a>"
3434
for f in obj.filters.all()
3535
)
36-
return format_html(filters_html)
36+
return format_html("{}", mark_safe(filters_html))
3737

3838
@admin.display(description="URL")
3939
def show_url(self, obj):

utils/modelAdmin_utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from django.utils.html import format_html
1+
from django.utils.html import format_html, mark_safe
22
from django.contrib.contenttypes.models import ContentType
33
from core.models import OpenAIAgent, DeepLAgent, LibreTranslateAgent, TestAgent
44

@@ -50,11 +50,16 @@ def status_icon(status):
5050
match status:
5151
case None:
5252
return format_html(
53-
"<img src='/static/img/icon-loading.svg' alt='In Progress'>"
53+
"{}",
54+
mark_safe("<img src='/static/img/icon-loading.svg' alt='In Progress'>")
5455
)
5556
case True:
5657
return format_html(
57-
"<img src='/static/admin/img/icon-yes.svg' alt='Succeed'>"
58+
"{}",
59+
mark_safe("<img src='/static/admin/img/icon-yes.svg' alt='Succeed'>")
5860
)
5961
case False:
60-
return format_html("<img src='/static/admin/img/icon-no.svg' alt='Error'>")
62+
return format_html(
63+
"{}",
64+
mark_safe("<img src='/static/admin/img/icon-no.svg' alt='Error'>")
65+
)

0 commit comments

Comments
 (0)