Skip to content

Commit d362d85

Browse files
committed
Remove most instances of mark_safe
1 parent 3ffbff5 commit d362d85

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

adserver/admin.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
from django.db import models
99
from django.template.response import TemplateResponse
1010
from django.utils import timezone
11-
from django.utils.html import escape
1211
from django.utils.html import format_html
13-
from django.utils.safestring import mark_safe
1412
from django.utils.translation import gettext_lazy as _
1513
from djstripe.models import Invoice
1614
from simple_history.admin import SimpleHistoryAdmin
@@ -190,9 +188,11 @@ def report(self, instance):
190188
if not instance.pk:
191189
return "" # pragma: no cover
192190

193-
name = escape(instance.name)
194-
url = instance.get_absolute_url()
195-
return mark_safe(f'<a href="{url}">{name}</a> Report')
191+
return format_html(
192+
'<a href="{}">{}</a>',
193+
instance.get_absolute_url(),
194+
f"{instance.name} Report",
195+
)
196196

197197

198198
class CampaignInline(admin.TabularInline):
@@ -296,10 +296,10 @@ def report(self, instance):
296296
if not instance.pk:
297297
return "" # pragma: no cover
298298

299-
return mark_safe(
300-
'<a href="{url}">{name}</a>'.format(
301-
name=escape(instance.name) + " Report", url=instance.get_absolute_url()
302-
)
299+
return format_html(
300+
'<a href="{}">{}</a>',
301+
instance.get_absolute_url(),
302+
f"{instance.name} Report",
303303
)
304304

305305
def stripe_customer(self, obj):
@@ -344,8 +344,10 @@ def ad_image(self, obj):
344344
if not obj.image:
345345
return ""
346346

347-
return mark_safe(
348-
f'<img src="{obj.image.url}" style="max-width: {self.MAX_IMAGE_WIDTH}px" />'
347+
return format_html(
348+
'<img src="{}" style="max-width: {}px" />',
349+
obj.image.url,
350+
self.MAX_IMAGE_WIDTH,
349351
)
350352

351353
def ctr(self, obj):
@@ -774,11 +776,10 @@ def campaign_report(self, instance):
774776
if not instance.pk or not instance.advertiser:
775777
return "" # pragma: no cover
776778

777-
return mark_safe(
778-
'<a href="{url}">{name}</a>'.format(
779-
name=escape(instance.name) + " Report",
780-
url=instance.advertiser.get_absolute_url(),
781-
)
779+
return format_html(
780+
'<a href="{}">{}</a>',
781+
instance.advertiser.get_absolute_url(),
782+
f"{instance.name} Report",
782783
)
783784

784785
def num_ads(self, obj):
@@ -949,8 +950,10 @@ class AdBaseAdmin(RemoveDeleteMixin, admin.ModelAdmin):
949950

950951
def page_url(self, instance):
951952
if instance.url:
952-
return mark_safe(
953-
'<a href="{url}">{url}</a>'.format(url=escape(instance.url))
953+
return format_html(
954+
'<a href="{}">{}</a>',
955+
instance.url,
956+
instance.url,
954957
)
955958
return None
956959

adserver/models.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from django.urls import reverse
2626
from django.utils import timezone
2727
from django.utils.functional import cached_property
28+
from django.utils.html import format_html
2829
from django.utils.html import mark_safe
2930
from django.utils.text import slugify
3031
from django.utils.translation import gettext_lazy as _
@@ -2001,22 +2002,24 @@ def render_links(self, link=None, preview=False):
20012002
url = link or self.link
20022003
if not self.text:
20032004
template = get_template("adserver/advertisement-body.html")
2004-
ad_html = template.render(
2005-
{
2006-
"ad": self,
2007-
"preview": preview,
2008-
}
2009-
).strip()
2010-
else:
2011-
ad_html = self.text
2012-
2013-
return mark_safe(
2014-
ad_html.replace(
2015-
"<a>",
2016-
'<a href="%s" rel="nofollow noopener sponsored" target="_blank">' % url,
2005+
return mark_safe(
2006+
template.render(
2007+
{
2008+
"url": url,
2009+
"ad": self,
2010+
"preview": preview,
2011+
}
2012+
).strip()
20172013
)
2014+
2015+
# Old style ads where the text was fully customizable
2016+
ad_html = self.text
2017+
a_tag = format_html(
2018+
'<a href="{}" rel="nofollow noopener sponsored" target="_blank">', url
20182019
)
20192020

2021+
return mark_safe(ad_html.replace("<a>", a_tag))
2022+
20202023
def render_ad(
20212024
self,
20222025
ad_type,

adserver/templates/adserver/advertisement-body.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% spaceless %}
2-
<a>
2+
<a href="{{ url }}" rel="nofollow noopener sponsored" target="_blank">
33
{% if ad.headline or preview %}<strong class="ea-headline">{{ ad.headline|default:"" }} </strong>{% endif %}
44
<span class="ea-body">{{ ad.content|default:"" }}</span>
55
{% if ad.cta or preview %}<strong class="ea-cta"> {{ ad.cta|default:"" }}</strong>{% endif %}

0 commit comments

Comments
 (0)