-
Notifications
You must be signed in to change notification settings - Fork 4
/
qexport.py
executable file
·102 lines (79 loc) · 2.42 KB
/
qexport.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
#!/usr/bin/python3
"""
This file contains code to extract quiz question from Django controlled db.
Functions:
read_questions()
write_questions()
main()
"""
import os
import sys
import django
from django.apps import AppConfig
class QexportConfig(AppConfig):
name = 'qexport'
PLATFORM = "brightspace"
ANSWER_COL_NAMES = {
'a': 'answerA',
'b': 'answerB',
'c': 'answerC',
'd': 'answerD',
'e': 'answerE'
}
OPT_PUNC = ". "
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
SECRET_KEY = os.getenv('SECRET_KEY')
# setting DB variables from mysite/settings.py:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
from devops.models import Question # noqa E402
def read_questions(mod_nm):
"""
reads questions for module 'mod_nm'
"""
recs = None
if mod_nm is None:
recs = Question.objects.values()
else:
recs = Question.objects.filter(module=mod_nm).values()
return recs
def write_questions(recs, format):
"""
Args:
recs: the data to output
Returns:
None (for now: we probably want success or error codes)
"""
for q_no, qst in enumerate(recs, start=1):
if format == "gradescope":
print(qst["text"])
print()
# marking the correct answer by '*'
for ch, col_name in ANSWER_COL_NAMES.items():
check_area = '(X)' if ch == qst["correct"].lower() else '( )'
# matching the index for 'options' &
# 'ans_options' to get correct alphabet
print(f"{check_area} {qst[col_name]}")
print()
elif format == "brightspace":
print("NewQuestion,MC")
print("QuestionText," + '"' + qst["text"] + '"')
print("Points,1")
for check, col_name in ANSWER_COL_NAMES.items():
if check == qst["correct"].lower():
check_area = 'Option,100'
else:
check_area = 'Option,0'
print(check_area, ',', '"', qst[col_name], '"')
def main():
mod_nm = None
format = PLATFORM
if len(sys.argv) > 1:
mod_nm = sys.argv[1]
if len(sys.argv) > 2:
format = sys.argv[2]
recs = read_questions(mod_nm)
write_questions(recs, format)
if __name__ == '__main__':
main()