-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_problem_quiz.py
executable file
·74 lines (59 loc) · 2.38 KB
/
single_problem_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
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course_id', help='ID of the course', required=True, type=int)
parser.add_argument('-a', '--assignment_id', help='ID of the assignment', required=True, type=int)
parser.add_argument('-e', '--excel_path', help='Path to Excel file', required=True, type=str)
parser.add_argument('-d', '--dir_path', help='Path to attachment directory', required=False, type=str)
args = parser.parse_args()
course_id = args.course_id
assignment_id = args.assignment_id
excel_path = args.excel_path
dir_path = args.dir_path
upload_attachment = True if dir_path else False
import os
import logging
# check if the path is valid
if not os.path.exists(excel_path):
logging.warning('Please check on the path to Excel file: {}'.format(excel_path))
exit(1)
if upload_attachment and not os.path.exists(dir_path):
logging.warning('Please check on the path to attachment directory: {}'.format(dir_path))
exit(1)
from canvasapi import Canvas
from canvasapi.exceptions import CanvasException
from dotenv import load_dotenv
from utils import *
logging.basicConfig(level=logging.WARNING)
# load environment variables from test or production environment
load_dotenv('test.env')
BASE_URL = os.getenv('BASE_URL') # Canvas API BASE URL
API_KEY = os.getenv('API_KEY') # Canvas API key
# retrieve course and assignment
client = Canvas(BASE_URL, API_KEY)
try:
course = client.get_course(course_id)
assignment = course.get_assignment(assignment_id)
except CanvasException as e:
print(e)
exit(1)
# read scores from excel
id2score=get_id2score(excel_path)
# read attachments from directory
id2path = get_id2path(dir_path)
# upload attachment to comments using assignment_id
submissions = assignment.get_submissions()
for submission in submissions:
user_id = submission.user_id
try:
submission.edit(submission={
'posted_grade': id2score[user_id]
})
print('User {} has been graded'.format(user_id))
if not upload_attachment: continue
file_path = id2path[user_id]
submission.upload_comment(file_path)
print('File {} has been uploaded for user {}'.format(file_path, user_id))
except KeyError as e:
logging.warning('Please check on user {}: failed at entering score or uploading attachment'.format(user_id))
except CanvasException as e:
logging.warning(e)