-
Notifications
You must be signed in to change notification settings - Fork 3
/
lti.py
316 lines (263 loc) · 9.85 KB
/
lti.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from collections import defaultdict
from datetime import datetime
import json
import logging
from logging.handlers import RotatingFileHandler
import re
from flask import Flask, redirect, render_template, request, url_for, Response
from canvasapi import Canvas
from canvasapi.user import User
from canvasapi.exceptions import CanvasException
from pylti.flask import lti
from pytz import utc, timezone
import requests
import six
from config import (
ALLOWED_CANVAS_DOMAINS,
API_KEY,
CANVAS_URL,
GOOGLE_ANALYTICS,
LOCAL_TIME_FORMAT,
LOG_BACKUP_COUNT,
LOG_FORMAT,
LOG_FILE,
LOG_LEVEL,
LOG_MAX_BYTES,
TIME_ZONE,
)
app = Flask(__name__)
app.config.from_object("config")
formatter = logging.Formatter(LOG_FORMAT)
handler = RotatingFileHandler(
LOG_FILE, maxBytes=LOG_MAX_BYTES, backupCount=LOG_BACKUP_COUNT
)
handler.setLevel(logging.getLevelName(LOG_LEVEL))
handler.setFormatter(formatter)
app.logger.addHandler(handler)
canvas = Canvas(CANVAS_URL, API_KEY)
@app.context_processor
def add_google_analytics_id():
return dict(GOOGLE_ANALYTICS=GOOGLE_ANALYTICS)
def error(exception=None):
return Response(
render_template(
"error.htm.j2",
message=exception.get(
"exception", "Please contact your System Administrator."
),
)
)
@app.route("/launch", methods=["GET", "POST"])
@lti(error=error, request="initial", role="staff", app=app)
def launch(lti=lti):
canvas_domain = request.values.get("custom_canvas_api_domain")
if canvas_domain not in ALLOWED_CANVAS_DOMAINS:
msg = (
"<p>This tool is only available from the following domain(s):<br/>{}</p>"
"<p>You attempted to access from this domain:<br/>{}</p>"
)
return render_template(
"error.htm.j2",
message=msg.format(", ".join(ALLOWED_CANVAS_DOMAINS), canvas_domain),
)
course_id = request.form.get("custom_canvas_course_id")
return redirect(url_for("show_assignments", course_id=course_id))
@app.route("/", methods=["GET"])
def index(lti=lti):
return "Please contact your System Administrator."
@app.route("/status", methods=["GET"])
def status(): # pragma: no cover
"""
Runs smoke tests and reports status
"""
status = {
"tool": "Due Date Changer",
"checks": {"index": False, "xml": False, "api_key": False},
"url": url_for("index", _external=True),
"xml_url": url_for("xml", _external=True),
"canvas_url": CANVAS_URL,
"debug": app.debug,
}
# Check index
try:
response = requests.get(url_for("index", _external=True), verify=False)
status["checks"]["index"] = (
response.text == "Please contact your System Administrator."
)
except Exception:
app.logger.exception("Index check failed.")
# Check xml
try:
response = requests.get(url_for("xml", _external=True), verify=False)
status["checks"]["xml"] = "application/xml" in response.headers.get(
"Content-Type"
)
except Exception:
app.logger.exception("XML check failed.")
# Check API Key
try:
self_user = canvas.get_user("self")
status["checks"]["api_key"] = isinstance(self_user, User)
except Exception:
app.logger.exception("API check failed.")
# Overall health check - if all checks are True
status["healthy"] = all(v is True for k, v in status["checks"].items())
return Response(json.dumps(status), mimetype="application/json")
@app.route("/course/<course_id>/assignments", methods=["GET"])
@lti(error=error, request="session", role="staff", app=app)
def show_assignments(course_id, lti=lti):
try:
course = canvas.get_course(course_id)
assignments = course.get_assignments()
quiz_dict = {quiz.id: quiz for quiz in course.get_quizzes()}
except CanvasException as err:
app.logger.exception(
"Error getting course, assignments or quizzes from Canvas."
)
return error({"exception": err})
assignment_quiz_list = []
try:
for assignment in assignments:
if hasattr(assignment, "quiz_id"):
quiz = quiz_dict.get(assignment.quiz_id)
if hasattr(quiz, "show_correct_answers_at_date"):
assignment.show_correct_answers_at_date = datetime_localize(
quiz.show_correct_answers_at_date
)
if hasattr(quiz, "hide_correct_answers_at_date"):
assignment.hide_correct_answers_at_date = datetime_localize(
quiz.hide_correct_answers_at_date
)
assignment_quiz_list.append(assignment)
except CanvasException as err:
app.logger.exception("Error getting assignments from Canvas.")
return error({"exception": err})
return render_template(
"assignments.htm.j2", assignments=assignment_quiz_list, course=course
)
@app.route("/course/<course_id>/update", methods=["POST"])
@lti(error=error, request="session", role="staff", app=app)
def update_assignments(course_id, lti=lti):
def fix_date(value):
try:
value = datetime.strptime(value, LOCAL_TIME_FORMAT)
value = local_tz.localize(value)
return value.isoformat()
except (ValueError, TypeError):
# Not a valid time. Just ignore.
return ""
def error_json(assignment_id, updated_list):
msg = "There was an error editing one of the assignments. (ID: {})"
msg = msg.format(assignment_id)
if len(updated_list) > 0:
"{} {} assignments have been updated successfully.".format(
msg, len(updated_list)
)
return Response(
json.dumps({"error": True, "message": msg, "updated": updated_list}),
mimetype="application/json",
)
def is_ajax_request(request):
"""
Determine whether or not a request was made via AJAX.
"""
return request.headers.get("X-Ddc-Ajax", "").lower() == "true"
if not is_ajax_request(request):
return render_template("error.htm.j2", message="Non-AJAX requests not allowed.")
try:
course = canvas.get_course(course_id)
except CanvasException:
msg = "Error getting course #{}.".format(course_id)
app.logger.exception(msg)
return Response(
json.dumps({"error": True, "message": msg, "updated": []}),
mimetype="application/json",
)
post_data = request.form
local_tz = timezone(TIME_ZONE)
assignment_field_map = defaultdict(dict)
for key, value in six.iteritems(post_data):
if not re.match(r"\d+-[a-z_]+", key):
continue
assignment_id, field_name = key.split("-")
assignment_field_map[assignment_id].update({field_name: value})
if len(assignment_field_map) < 1:
return Response(
json.dumps(
{
"error": True,
"message": "There were no assignments to update.",
"updated": [],
}
),
mimetype="application/json",
)
updated_list = []
for assignment_id, field in six.iteritems(assignment_field_map):
assignment_type = field.get("assignment_type", "assignment")
quiz_id = field.get("quiz_id")
payload = {
"published": field.get("published") == "on",
"due_at": fix_date(field.get("due_at")),
"lock_at": fix_date(field.get("lock_at")),
"unlock_at": fix_date(field.get("unlock_at")),
}
if assignment_type == "quiz" and quiz_id:
payload.update(
{
"show_correct_answers_at": fix_date(
field.get("show_correct_answers_at")
),
"hide_correct_answers_at": fix_date(
field.get("hide_correct_answers_at")
),
}
)
try:
quiz = course.get_quiz(quiz_id)
quiz.edit(quiz=payload)
updated_list.append(
{"id": assignment_id, "title": quiz.title, "type": "Quiz"}
)
except CanvasException:
app.logger.exception("Error getting/editing quiz #{}.".format(quiz_id))
return error_json(assignment_id, updated_list)
else:
try:
assignment = course.get_assignment(assignment_id)
assignment.edit(assignment=payload)
updated_list.append(
{
"id": assignment_id,
"title": assignment.name,
"type": "Assignment",
}
)
except CanvasException:
app.logger.exception(
"Error getting/editing assignment #{}.".format(assignment_id)
)
return error_json(assignment_id, updated_list)
return Response(
json.dumps(
{
"error": False,
"message": "Successfully updated {} assignments.".format(
len(updated_list)
),
"updated": updated_list,
}
),
mimetype="application/json",
)
@app.route("/lti.xml", methods=["GET"])
def xml():
return Response(render_template("lti.xml.j2"), mimetype="application/xml")
@app.template_filter()
def datetime_localize(utc_datetime, format=LOCAL_TIME_FORMAT):
if not utc_datetime.tzinfo:
# Localize to UTC if there is no timezone information.
utc_datetime = utc.localize(utc_datetime)
new_tz = timezone(TIME_ZONE)
local_datetime = utc_datetime.astimezone(new_tz)
return local_datetime.strftime(format)