Repository navigation
Expand file tree
/
Copy pathembedding.py
More file actions
119 lines (102 loc) · 3.96 KB
/
Copy pathembedding.py
File metadata and controls
119 lines (102 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from urllib.parse import urlsplit
from django.conf import settings
from django.template.loader import render_to_string
from django.utils.html import format_html
from django.utils.text import slugify
from django.utils.translation import pgettext_lazy
from feincms3.embedding import embed_srf, embed_vimeo, embed_youtube
__all__ = ["embed", "wrap"]
# This isn't a list of recommended third party providers.
# Additions are welcome, especially those including a handler.
_providers = {
"youtube": {
"handler": embed_youtube,
"title": "YouTube",
"privacy_policy_url": "https://policies.google.com/privacy",
},
"vimeo": {
"handler": embed_vimeo,
"title": "Vimeo",
"privacy_policy_url": "https://vimeo.com/privacy",
},
"srf": {
"handler": embed_srf,
"title": "SRF",
"privacy_policy_url": "https://www.srf.ch/rechtliches-datenschutzerklaerung",
},
"google": {
"handler": None,
"title": "Google",
"privacy_policy_url": "https://policies.google.com/privacy",
},
"mailchimp": {
"handler": None,
"title": "Mailchimp",
"privacy_policy_url": "https://mailchimp.com/legal/privacy/",
},
"cleverreach": {
"handler": None,
"title": "CleverReach",
"privacy_policy_url": "http://www.cleverreach.com/privacy-policy/",
},
"raisenow": {
"handler": None,
"title": "RaiseNow",
"privacy_policy_url": "https://www.raisenow.com/privacy-policy",
},
}
_providers.update(getattr(settings, "EMBED_PROVIDERS", {}))
_default_description = pgettext_lazy(
"f3cc",
"We would like to show you content from the provider {title}, but respect your privacy. If you agree to the {link_start}provider's privacy policy{link_end}, please click the following button to view the content.",
)
_default_button = pgettext_lazy("f3cc", "Show the content")
def _render_description(title, privacy_policy_url):
if privacy_policy_url:
return _default_description.format(
title=title,
link_start=format_html(
'<a href="{}" target="_blank" rel="noopener">', privacy_policy_url
),
link_end="</a>",
)
return _default_description.format(title=title, link_start="", link_end="")
def embed(url):
for provider, config in _providers.items():
if (handler := config.get("handler")) and (html := handler(url)) is not None:
return _render(html, provider, config)
return ""
def wrap(provider, html, **kwargs):
return _render(html, provider, _providers[provider], **kwargs)
def oembed(url, *, oembed_json=None):
if oembed_json is None:
from feincms3.plugins.external import oembed_json # noqa: PLC0415
if (data := oembed_json(url)) and (html := data.get("html")):
# provider_name is optional. The frontend ignores embeds with an empty
# provider, so the content couldn't be shown at all without a fallback.
provider_name = data.get("provider_name") or urlsplit(url).hostname or ""
provider = slugify(provider_name) or "oembed"
return render_to_string(
"feincms3_cookiecontrol/embed.html",
{
"embedded_html": f'<div class="responsive-embed widescreen {provider}">{html}</div>',
"provider": provider,
"description": _render_description(provider_name or provider, ""),
"button": _default_button,
},
)
return ""
def _render(html, provider, config, *, description=None, button=None):
return render_to_string(
"feincms3_cookiecontrol/embed.html",
{
"embedded_html": html,
"provider": provider,
"description": description
or _render_description(
config.get("title") or provider, config.get("privacy_policy_url")
),
"button": button or _default_button,
}
| config,
)