forked from canonical/ubuntu.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
127 lines (103 loc) · 3.38 KB
/
helpers.py
File metadata and controls
127 lines (103 loc) · 3.38 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
120
121
122
123
124
125
126
127
import html
import bleach
import markdown
from markupsafe import Markup
def get_download_url(model_details):
"""
Return the appropriate ubuntu models.com/download url for the model
:param model_details: a certifiedmodeldetails resource
:return: appropriate ubuntu.com/download url
"""
platform_category = model_details.get("category", "").lower()
architecture = model_details.get("architecture", "").lower()
make = model_details.get("make", "").lower()
configuration_name = model_details.get("model", "").lower()
if model_details.get("level") == "Enabled":
# Enabled systems use oem images without download links.
return
if platform_category in ["desktop", "laptop"]:
return "https://ubuntu.com/download/desktop"
if make == "nvidia" and "jetson" in configuration_name:
return "https://ubuntu.com/download/nvidia-jetson"
if "qualcomm" in make and "dragonwing" in configuration_name:
return "https://ubuntu.com/download/qualcomm-iot#evaluation-kit"
if "renesas" in make and "rz/g" in configuration_name:
return "https://ubuntu.com/download/renesas-iot"
if "core" in platform_category:
if make == "xilinx":
return "https://ubuntu.com/download/amd"
return "https://ubuntu.com/download/iot/"
if "server" in platform_category:
# Server platforms have special landing pages based on architecture.
arch = ""
if "arm" in architecture:
arch = "arm"
elif "ppc" in architecture:
arch = "power"
elif "s390x" in architecture:
arch = "s390x"
else:
return "https://ubuntu.com/download/server/"
return f"https://ubuntu.com/download/server/{arch}"
return "https://ubuntu.com/download"
def convert_markdown_to_html(text):
"""
Convert markdown to HTML while ensuring security by sanitizing
the output. Only pure Markdown is allowed, raw HTML is escaped
and displayed as code. Tables and images are not supported.
"""
if not text:
return ""
escaped_text = html.escape(text)
html_content = markdown.markdown(
escaped_text,
extensions=[
"markdown.extensions.fenced_code",
"markdown.extensions.nl2br",
],
)
allowed_tags = [
"p",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"hr",
"br",
"ul",
"ol",
"li",
"blockquote",
"pre",
"code",
"em",
"strong",
"a",
]
allowed_attrs = {
"a": ["href", "title"],
"code": ["class"],
"pre": ["class"],
}
clean_html = bleach.clean(
html_content, tags=allowed_tags, attributes=allowed_attrs, strip=True
)
return Markup(clean_html)
def _get_clean_in_filter(filter_in):
"""
Return a clean comma-separated values string from a list of values
This is required for the in filter query parameter in the API
:return: comma separated value of a list or the parameter itself
"""
if isinstance(filter_in, list):
return ",".join(filter_in)
return filter_in
def _get_category_pathname(form_factor):
if form_factor == "Ubuntu Core":
return "iot"
elif form_factor == "Server SoC":
return "socs"
else:
return form_factor.lower() + "s"