Skip to content

Commit 1152770

Browse files
authored
Use html_format instead of mark_safe + format (#11086)
* Use html_format instead of mark_safe + format This isn't a security issue, but it's a good practice to use html_format. * Format
1 parent 0952cf9 commit 1152770

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

readthedocs/integrations/admin.py

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

33
from django import urls
44
from django.contrib import admin
5+
from django.utils.html import format_html
56
from django.utils.safestring import mark_safe
67
from pygments.formatters import HtmlFormatter
78

@@ -110,11 +111,10 @@ def exchanges(self, obj):
110111
HttpExchange._meta.model_name,
111112
),
112113
)
113-
return mark_safe(
114-
'<a href="{}?{}={}">{} HTTP transactions</a>'.format(
115-
url,
116-
"integrations__pk",
117-
obj.pk,
118-
obj.exchanges.count(),
119-
),
114+
return format_html(
115+
'<a href="{}?{}={}">{} HTTP transactions</a>',
116+
url,
117+
"integrations__pk",
118+
obj.pk,
119+
obj.exchanges.count(),
120120
)

readthedocs/projects/validators.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.core.exceptions import ValidationError
88
from django.core.validators import RegexValidator
99
from django.utils.deconstruct import deconstructible
10-
from django.utils.safestring import mark_safe
10+
from django.utils.html import format_html
1111
from django.utils.translation import gettext_lazy as _
1212

1313
from readthedocs.projects.constants import LANGUAGES
@@ -114,11 +114,12 @@ def validate_build_config_file(path):
114114
)
115115
if any(ch in path for ch in invalid_characters):
116116
raise ValidationError(
117-
mark_safe(
117+
format_html(
118118
_(
119119
"Found invalid character. Avoid these characters: "
120120
"<code>{invalid_characters}</code>"
121-
).format(invalid_characters=invalid_characters),
121+
),
122+
invalid_characters=invalid_characters,
122123
),
123124
code="path_invalid",
124125
)
@@ -128,19 +129,17 @@ def validate_build_config_file(path):
128129
)
129130
if not is_valid and len(valid_filenames) == 1:
130131
raise ValidationError(
131-
mark_safe(
132-
_("The only allowed filename is <code>{filename}</code>.").format(
133-
filename=valid_filenames[0]
134-
),
132+
format_html(
133+
_("The only allowed filename is <code>{filename}</code>."),
134+
filename=valid_filenames[0],
135135
),
136136
code="path_invalid",
137137
)
138138
if not is_valid:
139139
raise ValidationError(
140-
mark_safe(
141-
_("The only allowed filenames are <code>{filenames}</code>.").format(
142-
filenames=", ".join(valid_filenames)
143-
),
140+
format_html(
141+
_("The only allowed filenames are <code>{filenames}</code>."),
142+
filenames=", ".join(valid_filenames),
144143
),
145144
code="path_invalid",
146145
)

readthedocs/projects/views/private.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from django.shortcuts import get_object_or_404
1616
from django.urls import reverse
1717
from django.utils import timezone
18-
from django.utils.safestring import mark_safe
18+
from django.utils.html import format_html
1919
from django.utils.translation import gettext_lazy as _
2020
from django.views.generic import ListView, TemplateView
2121
from formtools.wizard.views import SessionWizardView
@@ -371,16 +371,15 @@ def get(self, request, *args, **kwargs):
371371
provider_account = account.get_provider_account()
372372
messages.error(
373373
request,
374-
mark_safe((
374+
format_html(
375375
_(
376376
'There is a problem with your {service} account, '
377377
'try reconnecting your account on your '
378378
'<a href="{url}">connected services page</a>.',
379-
).format(
380-
service=provider_account.get_brand()['name'],
381-
url=reverse('socialaccount_connections'),
382-
)
383-
)), # yapf: disable
379+
),
380+
service=provider_account.get_brand()["name"],
381+
url=reverse("socialaccount_connections"),
382+
),
384383
)
385384
return super().get(request, *args, **kwargs)
386385

0 commit comments

Comments
 (0)