-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
328 lines (238 loc) · 10.1 KB
/
main.py
File metadata and controls
328 lines (238 loc) · 10.1 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
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
317
318
319
320
321
322
323
324
325
326
327
328
import math
import random
import os
from flask import Flask, flash, request, redirect, url_for, render_template, send_file
from werkzeug.utils import secure_filename
from docx import Document
from docx.shared import Pt
from flask_pymongo import PyMongo
UPLOAD_FOLDER = 'upload/'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MONGO_URI'] = "mongodb://localhost:27017/myDatabase"
mongo = PyMongo(app)
def allowed_doc(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() == 'docx'
def allowed_picture(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() == 'jpg'
@app.route('/')
def home_page():
return render_template('index.html')
# @app.route('/testbank')
# def bank():
@app.route('/uploaddb', methods=['GET', 'POST'])
def upload_db():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return render_template('dbupload.html', msg='No file selected')
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
return render_template('dbupload.html', msg='No file selected')
# if it is a doc
numClasses= request.form["num_classes"]
nameTest = request.form["test_title"]
if file and allowed_doc(file.filename) and numClasses and nameTest:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
for x in range(int(numClasses)):
doc_to_final(file.filename, nameTest, x)
return render_template('dbupload.html',
msg='Successfully processed', doc_src='static/' + nameTest + '.docx')
return redirect(url_for('view_tests()'))
return render_template("dbupload.html")
@app.route('/testbank')
def view_tests():
directory = os.fsencode("DB")
filelist=[]
for file in os.listdir(directory):
filename = os.fsdecode(file)
newFileName="../DB/" + filename
filelist.append(newFileName)
print(newFileName)
unit = filelist[0]
return render_template("testbank.html", unit = unit[10], list = filelist)
@app.route('/DB/<address>', methods = ['GET'])
def download_file(address):
if request.method == 'GET':
fileAddress = "DB/" + address
return send_file(fileAddress)
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return render_template('upload.html', msg='No file selected')
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
return render_template('upload.html', msg='No file selected')
# if it is a doc
numTest = request.form["num_copies"]
nameTest = request.form["test_title"]
if file and allowed_doc(file.filename) and numTest and nameTest:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
doc_to_doc(filename, int(numTest), nameTest)
return render_template('upload.html',
msg='Successfully processed', doc_src='static/' + nameTest + '.docx')
# return redirect(url_for('upload_file()'))
return render_template("upload.html")
if __name__ == '__main__':
app.run(debug=True)
class MCQuestion:
def __init__(self, question, choice_list, correct_ans):
self.question = question
self.choice_list = choice_list
self.correct_ans = correct_ans
class FRQuestion:
def __init__(self, question):
self.question = question
class Test:
def __init__(self, mcList, frList):
self.mcList = mcList
self.frList = frList
def get_random_number(num_string):
num = float(num_string)
lower_bound = 10**(int(math.log10(num)))
upper_bound = 10**(int(math.log10(num)) + 1)
random_num = random.uniform(lower_bound, upper_bound)
random_num_string = str(random_num)
return random_num_string[0:len(num_string)]
def randomize_frq(frq):
test_list = frq.question.split()
for num in range(len(test_list)):
if '[' in test_list[num]:
num_string = test_list[num][1:-1]
random_num = get_random_number(num_string)
test_list[num] = random_num
new_frq = frq
new_frq.question = " ".join(test_list)
return new_frq
def randomize_mcq(mcq):
correct_ans_string = mcq.choice_list[mcq.correct_ans]
test_list = mcq.choice_list
random.shuffle(test_list)
new_mcq = mcq
new_mcq.choice_list = test_list
new_mcq.correct_ans = test_list.index(correct_ans_string)
return new_mcq
def randomize_test(test):
new_test = test
for num in range(len(new_test.mcList)):
new_test.mcList[num] = randomize_mcq(new_test.mcList[num])
for num in range(len(new_test.frList)):
new_test.frList[num] = randomize_frq(new_test.frList[num])
new_mc = new_test.mcList
random.shuffle(new_mc)
new_test.mcList = new_mc
new_fr = new_test.frList
random.shuffle(new_fr)
new_test.frList = new_fr
return new_test
def parse_test(test_text):
questions = test_text.split("\n\n")
test_return = Test([], [])
for question in questions:
q_list = question.split("\n")
if len(q_list) > 1:
question_parts = q_list[0].split('. ')
question_parts.pop(0)
the_question = ". ".join(question_parts)
the_choices = []
correct_choice = 100
for num in range(1, len(q_list)):
the_choices.append(q_list[num].split('. ')[1])
if "*" in q_list[num]:
correct_choice = num - 1
test_return.mcList.append(MCQuestion(
the_question, the_choices, correct_choice))
else:
question_parts = q_list[0].split('. ')
question_parts.pop(0)
the_question = ". ".join(question_parts)
test_return.frList.append(FRQuestion(the_question))
return test_return
def parse_document(filename):
print("--------------------------------- " + filename +
" ------------------------------------------------------")
document = Document('upload/' + filename)
test_string = ""
for p in document.paragraphs:
test_string += p.text + "\n"
# print(test_string)
return parse_test(test_string[:-1])
def doc_to_final(filename, test_name, index):
letter_list = ['A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
original_test = parse_document(filename)
document = Document()
print("///////////////////////////////////////////////////////")
style = document.styles['Normal']
font = style.font
font.name = 'Calibri (Body)'
font.size = Pt(12)
answer_key_list = []
document.add_heading(test_name + ' Class ' + str(index+1))
the_test = randomize_test(original_test)
current_question = 1
for mcq in the_test.mcList:
document.add_paragraph(str(current_question) + ". " + mcq.question)
current_question += 1
current_choice = 0
for choice in mcq.choice_list:
document.add_paragraph(
'\t' + letter_list[current_choice] + '. ' + choice)
current_choice += 1
answer_key_list.append(letter_list[mcq.correct_ans])
document.add_paragraph()
for frq in the_test.frList:
document.add_paragraph(str(current_question) + ". " + frq.question)
current_question += 1
document.add_paragraph()
document.add_page_break()
document.add_heading(test_name + ' Class ' + str(index+1))
for num in range(len(answer_key_list)):
document.add_paragraph(str(num + 1) + ". " + answer_key_list[num])
document.save('DB/' + test_name + 'class' + str(index+1) + '.docx')
def doc_to_doc(filename, num_copies, test_name):
letter_list = ['A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
original_test = parse_document(filename)
document = Document()
print("///////////////////////////////////////////////////////")
style = document.styles['Normal']
font = style.font
font.name = 'Calibri (Body)'
font.size = Pt(12)
for num in range(num_copies):
answer_key_list = []
document.add_heading(test_name + " Form " + str(num + 1), 0)
the_test = randomize_test(original_test)
current_question = 1
for mcq in the_test.mcList:
document.add_paragraph(str(current_question) + ". " + mcq.question)
current_question += 1
current_choice = 0
for choice in mcq.choice_list:
document.add_paragraph(
'\t' + letter_list[current_choice] + '. ' + choice)
current_choice += 1
answer_key_list.append(letter_list[mcq.correct_ans])
document.add_paragraph()
for frq in the_test.frList:
document.add_paragraph(str(current_question) + ". " + frq.question)
current_question += 1
document.add_paragraph()
document.add_page_break()
document.add_heading("Answer Key Form " + str(num + 1))
for num in range(len(answer_key_list)):
document.add_paragraph(str(num + 1) + ". " + answer_key_list[num])
document.add_page_break()
document.save('static/' + test_name + '.docx')
# mongo.save_file(test_name + '.docx',)