Skip to content

Commit ba6a39e

Browse files
jxltomauvipy
authored andcommitted
Mark safe for admin form's html content (#542)
1 parent bc9dbd9 commit ba6a39e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

djcelery/admin.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.forms.widgets import Select
1111
from django.shortcuts import render_to_response
1212
from django.template import RequestContext
13-
from django.utils.html import escape
13+
from django.utils.html import escape, format_html, mark_safe
1414
from django.utils.translation import ugettext_lazy as _
1515

1616
from celery import current_app
@@ -55,37 +55,41 @@ def __init__(self, *args, **kwargs):
5555
def colored_state(task):
5656
state = escape(task.state)
5757
color = TASK_STATE_COLORS.get(task.state, 'black')
58-
return '<b><span style="color: {0};">{1}</span></b>'.format(color, state)
58+
return format_html(
59+
'<b><span style="color: {0};">{1}</span></b>', color, state
60+
)
5961

6062

6163
@display_field(_('state'), 'last_heartbeat')
6264
def node_state(node):
6365
state = node.is_alive() and 'ONLINE' or 'OFFLINE'
6466
color = NODE_STATE_COLORS[state]
65-
return '<b><span style="color: {0};">{1}</span></b>'.format(color, state)
67+
return format_html(
68+
'<b><span style="color: {0};">{1}</span></b>', color, state
69+
)
6670

6771

6872
@display_field(_('ETA'), 'eta')
6973
def eta(task):
7074
if not task.eta:
71-
return '<span style="color: gray;">none</span>'
75+
return mark_safe('<span style="color: gray;">none</span>')
7276
return escape(make_aware(task.eta))
7377

7478

7579
@display_field(_('when'), 'tstamp')
7680
def tstamp(task):
7781
# convert to local timezone
7882
value = make_aware(task.tstamp)
79-
return '<div title="{0}">{1}</div>'.format(
80-
escape(str(value)), escape(naturaldate(value)),
83+
return format_html(
84+
'<div title="{0}">{1}</div>', str(value), naturaldate(value)
8185
)
8286

8387

8488
@display_field(_('name'), 'name')
8589
def name(task):
8690
short_name = abbrtask(task.name, 16)
87-
return '<div title="{0}"><b>{1}</b></div>'.format(
88-
escape(task.name), escape(short_name),
91+
return format_html(
92+
'<div title="{0}"><b>{1}</b></div>', task.name, short_name
8993
)
9094

9195

djcelery/admin_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pprint import pformat
44

5-
from django.utils.html import escape
5+
from django.utils.html import format_html, mark_safe
66

77
FIXEDWIDTH_STYLE = '''\
88
<span title="{0}" style="font-size: {1}pt; \
@@ -43,8 +43,6 @@ def f(task):
4343

4444
if len(shortval) > maxlen:
4545
shortval = shortval[:maxlen] + '...'
46-
styled = FIXEDWIDTH_STYLE.format(
47-
escape(val[:255]), pt, escape(shortval),
48-
)
49-
return styled.replace('|br/|', '<br/>')
46+
styled = format_html(FIXEDWIDTH_STYLE, val[:255], pt, shortval)
47+
return mark_safe(styled.replace('|br/|', '<br/>'))
5048
return f

0 commit comments

Comments
 (0)