-
Notifications
You must be signed in to change notification settings - Fork 2
/
assign_quiz.py
97 lines (76 loc) · 2.79 KB
/
assign_quiz.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
import pandas as pd
import canvasapi
import sys
import settings
def assign_quiz(canvas, assignment_id, course_id, student_id):
"""
Function to assign quiz in Canvas. Takes assignment ID, course ID and students' Canvas user ID.
"""
try:
course = canvas.get_course(course_id)
assignment = course.get_assignment(assignment_id)
except canvasapi.exceptions.Unauthorized as e:
print(
f"ERROR: Course ({course_id}) does not exist OR user is not authorized to access"
)
return
except TypeError:
print(f"ERROR: Course ID must be an INTEGER. Instead given: {course_id}")
return
except Exception as e:
print(
f"ERROR: Unable to fetch assignment, please check assignment ID: {assignment_id}"
)
return
# Filter out students who could not be found
student_id = list(filter(lambda x: x != None, student_id))
if len(student_id) == 0:
print(
f"No valid student ids provided for course ({course_id}), assignment ({assignment_id})"
)
return
try:
assignment.create_override(assignment_override={"student_ids": student_id})
except canvasapi.exceptions.BadRequest:
print(
f"ERROR Unable to add students with ids: {student_id} to assignment. Please ensure all students don't already belong to assignment"
)
def process_input(canvas, input_csv):
"""
Creates an dictionary of all assignment IDs and assigns a list containing student IDs for each assignment ID
"""
df = pd.read_csv(input_csv)
assignment_dict = {}
for _, row in df.iterrows():
assignment_id = row["assignment_id"]
try:
user_id = row["id"]
except canvasapi.exceptions.ResourceDoesNotExist as e:
print(e)
print(f"unable to find student: {sis_id}")
if assignment_id in assignment_dict:
assignment_dict[assignment_id].append(user_id)
else:
assignment_dict[assignment_id] = [user_id]
return assignment_dict
def main(input_csv):
"""
Main entry point for assign_quiz.py script
"""
# Prompt the user for a course ID
course_id = input("Enter course ID: ")
canvas = canvasapi.Canvas(settings.INSTANCE, settings.TOKEN)
# add_canvas_ids(input_csv, course_id)
assignment_dict = process_input(canvas, input_csv)
# Calls assign_quiz function once for every assignment ID
for key in assignment_dict:
assign_quiz(canvas, key, course_id, assignment_dict.get(key))
if __name__ == "__main__":
if len(sys.argv) > 1:
input_csv = sys.argv[1]
else:
print(
"Please run the program again with the csv file passed as command-line arguments"
)
sys.exit()
main(input_csv)