Skip to content

Commit 343ddab

Browse files
rixxHungNguyen-tma
authored andcommitted
Use new safelinks, reuse cleaner
1 parent e716d69 commit 343ddab

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

pretalx_pages/views.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from functools import partial
2+
13
import bleach
24
from csp.decorators import csp_update
35
from django import forms
@@ -17,10 +19,34 @@
1719
UpdateView,
1820
)
1921
from i18nfield.forms import I18nModelForm
22+
from pretalx.common.templatetags import rich_text
2023
from pretalx.common.views.mixins import EventPermissionRequired
2124

2225
from .models import Page
2326

27+
ALLOWED_ATTRIBUTES = dict(rich_text.ALLOWED_ATTRIBUTES)
28+
ALLOWED_ATTRIBUTES["a"] = ["href", "title", "target", "class"]
29+
ALLOWED_ATTRIBUTES["p"] = ["class"]
30+
ALLOWED_ATTRIBUTES["li"] = ["class"]
31+
ALLOWED_ATTRIBUTES["img"] = ["src", "title", "alt", "class"]
32+
CLEANER = bleach.Cleaner(
33+
tags=rich_text.ALLOWED_TAGS
34+
| {"img", "p", "br", "s", "sup", "sub", "u", "h3", "h4", "h5", "h6"},
35+
attributes=ALLOWED_ATTRIBUTES,
36+
protocols=rich_text.ALLOWED_PROTOCOLS | {"data"},
37+
filters=[
38+
partial(
39+
bleach.linkifier.LinkifyFilter,
40+
url_re=rich_text.TLD_REGEX,
41+
parse_email=True,
42+
email_re=rich_text.EMAIL_REGEX,
43+
skip_tags={"pre", "code"},
44+
callbacks=bleach.linkifier.DEFAULT_CALLBACKS
45+
+ [rich_text.safelink_callback],
46+
)
47+
],
48+
)
49+
2450

2551
class PageList(EventPermissionRequired, ListView):
2652
model = Page
@@ -232,27 +258,5 @@ def get_context_data(self, **kwargs):
232258
ctx = super().get_context_data()
233259
page = self.get_page()
234260
ctx["page_title"] = page.title
235-
from pretalx.common.templatetags.rich_text import (
236-
ALLOWED_ATTRIBUTES,
237-
ALLOWED_PROTOCOLS,
238-
ALLOWED_TAGS,
239-
md,
240-
)
241-
242-
attributes = dict(ALLOWED_ATTRIBUTES)
243-
attributes["a"] = ["href", "title", "target", "class"]
244-
attributes["p"] = ["class"]
245-
attributes["li"] = ["class"]
246-
attributes["img"] = ["src", "title", "alt", "class"]
247-
248-
ctx["content"] = bleach.clean(
249-
md.reset().convert(str(page.text)),
250-
tags=ALLOWED_TAGS
251-
| {"img", "p", "br", "s", "sup", "sub", "u", "h3", "h4", "h5", "h6"},
252-
attributes=attributes,
253-
protocols=ALLOWED_PROTOCOLS
254-
| {
255-
"data",
256-
},
257-
)
261+
ctx["content"] = CLEANER.clean(rich_text.md.reset().convert(str(page.text)))
258262
return ctx

0 commit comments

Comments
 (0)