-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdateCanvasScheduleLock.py
More file actions
executable file
·69 lines (57 loc) · 2.55 KB
/
Copy pathupdateCanvasScheduleLock.py
File metadata and controls
executable file
·69 lines (57 loc) · 2.55 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
#!/usr/local/bin/python3
import sys
from canvasapi import Canvas
from string import Template
from CanvasSettings import *
import json # For making due dates into map
import datetime
### THIS VERSION WILL LOCK assignment (not accepted after)
#### Should be used for quizzes / surveys
if len(sys.argv)!=2:
print(f"Arguments: JSONFileName")
exit(1)
# Open the schedule
with open(sys.argv[1]) as json_file:
schedule = json.load(json_file)
# Initialize a new Canvas object
canvas = Canvas(API_URL, API_KEY)
course = canvas.get_course(COURSE_ID)
sectionNamesToIds = {i.name:i.id for i in course.get_sections()}
# Iterate through the schedule
for assignment in schedule:
# Get the assignment
match = False
for a in course.get_assignments(**{"search_term":assignment}):
if a.name == assignment:
print(f'Updating: {assignment}')
match = True
if isinstance(schedule[assignment], dict):
for sect,date in schedule[assignment].items():
try:
dueDate = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p').isoformat()
except:
print(f'Invalid due date for {assignment}: {date}')
continue
if sect in sectionNamesToIds:
section = sectionNamesToIds[sect]
# If an override already exists, delete it.
for o in a.get_overrides():
if hasattr(o, 'course_section_id') and o.course_section_id == section:
o.delete()
# No existing override (now). Add one
arg = {"assignment_override[course_section_id]":str(section),"assignment_override[due_at]":dueDate,"assignment_override[lock_at]":dueDate}
a.create_override(**arg)
else:
print(f'Invalid Section name: {section}')
continue
elif isinstance(schedule[assignment], str):
try:
dueDate = datetime.datetime.strptime(schedule[assignment], '%Y-%m-%d %I:%M%p').isoformat()
except:
print(f'Invalid due date for {assignment}')
continue
# Try to update it
a.edit(**{"assignment[due_at]":dueDate})
a.edit(**{"assignment[lock_at]":dueDate})
if match == False:
print(f'No matching assignment for {assignment}')