-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
102 lines (80 loc) · 3.2 KB
/
utils.py
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
from collections import defaultdict
import json
import re
from canvasapi import Canvas
import settings
def get_tool_info(whitelist, tool_name):
"""
Search the whitelist by tool name.
:returns: A dictionary , or none if no tool matching that name was found.
:rtype: dict or None
"""
for category, category_tools in whitelist.items():
for tool_info in category_tools:
if tool_info.get("name") == tool_name:
tool_info.update({"category": category})
return tool_info
return None
def filter_tool_list(course_id, access_token):
"""
Filter tool list down to those on whitelist and sort by category.
:param course_id: The Canvas ID of the course to get the tools for.
:type course_id: int
:param access_token: The access token (API key) to use.
:type access_token: str
:rtype: dict
:returns: A dictionary where the keys are tool categories.
The values are a list of all installed external tools that are
in that category and on the whitelist.
"""
with open(settings.whitelist, "r") as wl_file:
whitelist = json.loads(wl_file.read())
if not whitelist:
raise ValueError("whitelist.json is empty")
canvas = Canvas(settings.BASE_URL, access_token)
course = canvas.get_course(course_id)
installed_tools = course.get_external_tools(include_parents=True)
tools_by_category = defaultdict(list)
for installed_tool in installed_tools:
tool_info = get_tool_info(whitelist, installed_tool.name)
if not tool_info:
continue
is_course_navigation = hasattr(installed_tool, "course_navigation")
if tool_info.get("is_launchable", False):
if is_course_navigation:
sessionless_launch_url = installed_tool.get_sessionless_launch_url(
launch_type="course_navigation"
)
else:
sessionless_launch_url = installed_tool.get_sessionless_launch_url()
else:
sessionless_launch_url = None
tool_info.update(
{
"id": installed_tool.id,
"lti_course_navigation": is_course_navigation,
"sessionless_launch_url": sessionless_launch_url,
"screenshot": "screenshots/" + tool_info["screenshot"],
}
)
tools_by_category[tool_info.get("category", "Uncategorized")].append(tool_info)
for category, tools in tools_by_category.items():
# Determine tool order based on order in whitelist
order = {
tool.get("display_name"): i for i, tool in enumerate(whitelist[category])
}
tools_by_category[category] = sorted(
tools, key=lambda k: order[k["display_name"]]
)
return tools_by_category, list(whitelist.keys())
def slugify(value):
"""
Convert spaces to hyphens.
Remove characters that aren't alphanumerics, underscores, or hyphens.
Convert to lowercase.
Also strip leading and trailing whitespace.
From Django's "django/utils/text.py".
"""
value = str(value)
value = re.sub(r"[^\w\s-]", "", value).strip().lower()
return re.sub(r"[-\s]+", "-", value)