Skip to content

Commit 0972ec9

Browse files
committedDec 11, 2019
Order of tools and categories is now directly tied to the order in the whitelist.
1 parent 1cf52e6 commit 0972ec9

File tree

5 files changed

+83
-58
lines changed

5 files changed

+83
-58
lines changed
 

‎lti.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ def index(lti=lti):
226226
)
227227

228228
try:
229-
tools_by_category = filter_tool_list(session["course_id"], session["api_key"])
229+
tools_by_category, cagetory_order = filter_tool_list(
230+
session["course_id"], session["api_key"]
231+
)
230232
except CanvasException:
231233
app.logger.exception("Couldn't connect to Canvas")
232234
return return_error(
@@ -243,6 +245,7 @@ def index(lti=lti):
243245
return render_template(
244246
"main_template.html",
245247
tools_by_category=tools_by_category,
248+
category_order=cagetory_order,
246249
course=session["course_id"],
247250
)
248251

‎templates/lti_list.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ <h3 class="card-title text-center">{{lti.display_name}}</h3>
4444
{% endfor %}
4545
{% else %}
4646
<div class="row">
47-
<div class="col-md-8 ml-auto">
47+
<div class="col-md-8 ml-auto mr-auto">
4848
<p class="text-center" id="no_ltis">No LTIs available in this category.</p>
49-
<hr />
5049
</div>
5150
</div>
5251
{% endif %}

‎templates/main_template.html

+10-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="container-fluid jump-to-section">
55
<h2>Jump to section:</h2>
66
<ul>
7-
{% for category in tools_by_category %}
7+
{% for category in category_order %}
88
<li><a class="btn btn-primary" href="#{{ category|slugify}}">{{ category }}</a></li>
99
{% endfor %}
1010
{% include "extra_links.html" ignore missing %}
@@ -14,18 +14,16 @@ <h2>Jump to section:</h2>
1414

1515
{% block content %}
1616

17-
{% for category, tools in tools_by_category.items() %}
18-
<div class="row">
19-
<div class="col-md-8 ml-auto mr-auto">
20-
<h2 class="text-center section_headline" id="{{ category|slugify}}">{{ category }}</h2>
21-
<hr />
17+
{% for category in category_order %}
18+
<div class="row">
19+
<div class="col-md-8 ml-auto mr-auto">
20+
<h2 class="text-center section_headline" id="{{ category|slugify}}">{{ category }}</h2>
21+
<hr />
22+
</div>
2223
</div>
23-
</div>
24-
25-
{% with ltis=tools %}
26-
{% include "lti_list.html" %}
27-
{% endwith %}
28-
24+
{% with ltis=tools_by_category[category] %}
25+
{% include "lti_list.html" %}
26+
{% endwith %}
2927
{% endfor %}
3028

3129
{% include "custom_tools.html" ignore missing %}

‎utils.py

+52-28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
import settings
88

99

10+
def get_tool_info(whitelist, tool_name):
11+
"""
12+
Search the whitelist by tool name.
13+
14+
:returns: A dictionary , or none if no tool matching that name was found.
15+
:rtype: dict or None
16+
"""
17+
for category, category_tools in whitelist.items():
18+
for tool_info in category_tools:
19+
print(tool_info)
20+
if tool_info.get("name") == tool_name:
21+
tool_info.update({"category": category})
22+
return tool_info
23+
24+
return None
25+
26+
1027
def filter_tool_list(course_id, access_token):
1128
"""
1229
Filter tool list down to those on whitelist and sort by category.
@@ -31,38 +48,45 @@ def filter_tool_list(course_id, access_token):
3148

3249
course = canvas.get_course(course_id)
3350
installed_tools = course.get_external_tools(include_parents=True)
34-
3551
tools_by_category = defaultdict(list)
3652
for installed_tool in installed_tools:
37-
for tool in whitelist:
38-
if installed_tool.name != tool.get("name"):
39-
continue
40-
41-
is_course_navigation = hasattr(installed_tool, "course_navigation")
42-
43-
if tool.get("is_launchable", False):
44-
if is_course_navigation:
45-
sessionless_launch_url = installed_tool.get_sessionless_launch_url(
46-
launch_type="course_navigation"
47-
)
48-
else:
49-
sessionless_launch_url = installed_tool.get_sessionless_launch_url()
50-
else:
51-
sessionless_launch_url = None
53+
tool_info = get_tool_info(whitelist, installed_tool.name)
54+
if not tool_info:
55+
continue
5256

53-
tool_info = tool
54-
tool_info.update(
55-
{
56-
"id": installed_tool.id,
57-
"lti_course_navigation": is_course_navigation,
58-
"sessionless_launch_url": sessionless_launch_url,
59-
"screenshot": "screenshots/" + tool["screenshot"],
60-
}
61-
)
57+
is_course_navigation = hasattr(installed_tool, "course_navigation")
6258

63-
tools_by_category[tool.get("category", "Uncategorized")].append(tool_info)
64-
65-
return tools_by_category
59+
if tool_info.get("is_launchable", False):
60+
if is_course_navigation:
61+
sessionless_launch_url = installed_tool.get_sessionless_launch_url(
62+
launch_type="course_navigation"
63+
)
64+
else:
65+
sessionless_launch_url = installed_tool.get_sessionless_launch_url()
66+
else:
67+
sessionless_launch_url = None
68+
69+
tool_info.update(
70+
{
71+
"id": installed_tool.id,
72+
"lti_course_navigation": is_course_navigation,
73+
"sessionless_launch_url": sessionless_launch_url,
74+
"screenshot": "screenshots/" + tool_info["screenshot"],
75+
}
76+
)
77+
78+
tools_by_category[tool_info.get("category", "Uncategorized")].append(tool_info)
79+
80+
for category, tools in tools_by_category.items():
81+
# Determine tool order based on order in whitelist
82+
order = {
83+
tool.get("display_name"): i for i, tool in enumerate(whitelist[category])
84+
}
85+
tools_by_category[category] = sorted(
86+
tools, key=lambda k: order[k["display_name"]]
87+
)
88+
89+
return tools_by_category, list(whitelist.keys())
6690

6791

6892
def slugify(value):

‎whitelist.json.template

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
[
2-
{
3-
"display_name": "Name to Display",
4-
"name": "Installed Tool Name",
5-
"tool_id": "tool_id",
6-
"allowed_roles": [""],
7-
"desc": "Tool Description",
8-
"screenshot": "screenshot.png",
9-
"logo": "logo.svg",
10-
"filter_by": ["all"],
11-
"docs_url": "https://example.com/tool/docs/",
12-
"is_launchable": true,
13-
"category": "Course Tool"
14-
}
15-
]
1+
{
2+
"Course Tool": [
3+
{
4+
"display_name": "Name to Display",
5+
"name": "Installed Tool Name",
6+
"tool_id": "tool_id",
7+
"allowed_roles": [""],
8+
"desc": "Tool Description",
9+
"screenshot": "screenshot.png",
10+
"logo": "logo.svg",
11+
"filter_by": ["all"],
12+
"docs_url": "https://example.com/tool/docs/",
13+
"is_launchable": true
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.