-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_series.py
329 lines (288 loc) · 15.2 KB
/
test_series.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
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
329
from flask import flash, render_template, Blueprint, url_for, redirect, request, jsonify, g, abort
from forms import *
from uuid import uuid1
from time import time
import memoir
import urllib2
import json
import utility
test_series = Blueprint('test_series', __name__, template_folder='templates')
@test_series.route('/tests')
def tests():
try:
user = g.user.user_doc
except:
flash('You need to be logged in to take tests.', 'error')
return redirect(url_for('questions'))
return render_template('tests.html', title='Tests', user=user, name=g.user.name, role=g.user.role, user_id=g.user.id, APP_ROOT=memoir.APP_ROOT)
@test_series.route('/getcat')
def getcat():
if g.user.id == 'u1':
tech = request.args.get('lang')
if tech == 'C':
return jsonify({
'basics': 'Basics',
'control_flow': 'Control Flow',
'oae': 'Operators and Expressions',
'funcs': 'Functions',
'arrays': 'Arrays',
'pointers': 'Pointers',
'strings': 'Strings',
'sue': 'Structures, Unions and Eums',
'io': 'IO',
'cli': 'Command Line Arguments',
'bitop': 'Bitwise Operators',
'typedef': 'Typedef',
'const': 'Const',
'memalloc': 'Memory Allocation',
'vararg': 'Variable Arguments',
'lib': 'Library Functions',
'threading': 'Multithreading',
'macros': 'Macros'
})
elif tech == 'C++':
return jsonify({
'basics': 'Basics',
'control_flow': 'Control Flow',
'oae': 'Operators and Expressions',
'funcs': 'Functions',
'arrays': 'Arrays',
'pointers': 'Pointers',
'strings': 'Strings',
'sue': 'Structures, Unions and Eums',
'io': 'IO',
'cli': 'Command Line Arguments',
'bitop': 'Bitwise Operators',
'typedef': 'Typedef',
'const': 'Const',
'memalloc': 'Memory Allocation',
'vararg': 'Variable Arguments',
'lib': 'Library Functions',
'threading': 'Multithreading',
'classes': 'Classes',
'inheritance': 'Inheritance',
'polymorphism': 'Polymorphism',
'exceptions': 'Exceptions',
'rtti': 'RTTI',
'templates': 'Templates',
'stl': 'STL',
'references': 'References'
})
elif tech == 'Java':
return jsonify({
'basics': 'Basics',
'control_flow': 'Control Flow',
'oae': 'Operators and Expressions',
'funcs': 'Functions',
'arrays': 'Arrays',
'strings': 'Strings',
'io': 'IO',
'cli': 'Command Line Arguments',
'bitop': 'Bitwise Operators',
'memalloc': 'Memory Allocation',
'lib': 'Library Functions',
'threading': 'Multithreading',
'classes': 'Classes',
'inheritance': 'Inheritance',
'polymorphism': 'Polymorphism',
'exceptions': 'Exceptions',
'generics': 'Generics',
'packages': 'Packages',
'interfaces': 'Interfaces',
'networking': 'Network Programming'
})
elif tech == 'Python':
return jsonify({
'basics': 'Basics',
'control_flow': 'Control Flow',
'oae': 'Operators and Expressions',
'funcs': 'Functions',
'ds': 'Data Structures',
'modules': 'Modules',
'io': 'IO',
'exceptions': 'Exceptions',
'classes': 'Classes',
'at': 'Advanced Topics',
'lib': 'Python Library'
})
elif tech == 'Perl':
return jsonify({
'basics': 'Basics',
'control_flow': 'Control Flow',
'oae': 'Operators and Expressions',
'funcs': 'Built-In Functions',
'ds': 'Data Structures',
'modules': 'Modules',
'io': 'IO',
'classes': 'Classes',
'at': 'Advanced Topics',
'lib': 'Perl Library',
'regex': 'Regular Expressions'
})
else:
return jsonify({"success": False})
@test_series.route('/add_objective_question', methods=['GET', 'POST'])
def add_objective_question():
oqForm = OQForm(request.form)
class ChoiceForm(Form):
tech = SelectField('Technology', choices=[('c', 'C'), ('cpp', 'C++'), ('java', 'Java'), ('perl', 'Perl'),
('python', 'Python')])
cat = SelectField('Category', choices=[('arrays', 'Arrays'), ('basics', 'Basics'), ('bitop', 'Bitwise Operators'),
('cli', 'Command Line Arguments'), ('const', 'Const'), ('control_flow', 'Control Flow'),
('funcs', 'Functions'), ('io', 'IO'), ('lib', 'Library Functions'), ('macros', 'Macros'), ('memalloc', 'Memory Allocation'),
('oae', 'Operators and Expressions'), ('pointers', 'Pointers'), ('strings', 'Strings'),
('sue', 'Structures, Unions and Eums'), ('threading', 'Multithreading'), ('typedef', 'Typedef'),
('vararg', 'Variable Arguments')])
option = RadioField('What type of question do you want?', choices=[('Single choice', 'Single Choice'),
('Multiple choice', 'Multiple choice')])
description = TextAreaField('', [validators.Length(min=20, max=5000), validators.DataRequired()])
option_1 = StringField('Question', [validators.Length(min=1, max=200), validators.DataRequired()])
option_2 = StringField('Question', [validators.Length(min=1, max=200), validators.DataRequired()])
option_3 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
option_4 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
option_5 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
option_6 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
answers = StringField('Answers', [validators.Length(min=1, max=200), validators.DataRequired()])
questionForm = ChoiceForm(request.form)
choices = []
if g.user.id == 'u1':
if oqForm.validate_on_submit() and request.method == 'POST':
for i in range(0, int(oqForm.oq_answers.data)):
choices.append(unicode(i+1))
return render_template('create_oq.html', title='Create Objective Question', form=questionForm, options=choices,
ppage=True, name=g.user.name, role=g.user.role, user_id=g.user.id, APP_ROOT=memoir.APP_ROOT)
if questionForm.validate_on_submit() and request.method == 'POST':
tech = questionForm.tech.data
cat = questionForm.cat.data
option = questionForm.option.data
option_1 = questionForm.option_1.data
option_2 = questionForm.option_2.data
question = {}
question['tech'] = tech
question['cat'] = cat
question['content'] = {}
question['content']['description'] = questionForm.description.data
question['answers'] = questionForm.answers.data
if option == 'Single choice':
question['content']['sc'] = True
else:
question['content']['mc'] = True
question['content']['options'] = []
question['content']['options'].append(option_1)
question['content']['options'].append(option_2)
if questionForm.option_3.data != "":
question['content']['options'].append(questionForm.option_3.data)
if questionForm.option_4.data != "":
question['content']['options'].append(questionForm.option_4.data)
if questionForm.option_5.data != "":
question['content']['options'].append(questionForm.option_5.data)
if questionForm.option_6.data != "":
question['content']['options'].append(questionForm.option_6.data)
question['content']['ts'] = int(time())
question['updated'] = question['content']['ts']
question['content']['ip'] = request.remote_addr
question['qid'] = 'tq-' + unicode(uuid1()) # tq stands for test question. prefix is used for increasing period
# before uuid will repeat
question['_type'] = 'tq'
# print unicode(question['qid'])
memoir.mb.add(question['qid'], question)
return redirect(url_for('administration'))
return render_template('oq.html', title='Create Objective Question', form=oqForm, ppage=True, name=g.user.name, role=g.user.role,
user_id=g.user.id, APP_ROOT=memoir.APP_ROOT)
else:
return redirect(url_for('login'))
@test_series.route('/browse_objective_questions', defaults={'page': 1}, methods=['GET', 'POST'])
@test_series.route('/browse_objective_questions/page/<int:page>')
def browse_objective_questions(page=None):
boqForm = BOQForm(request.form)
if g.user.id == 'u1':
if boqForm.validate_on_submit() and request.method == 'POST':
skip = (page - 1) * memoir.QUESTIONS_PER_PAGE
tech = boqForm.tech.data
cat = boqForm.cat.data
questions = urllib2.urlopen(memoir.DB_URL + 'memoir/_design/dev_kunjika/_view/get_by_ts?limit=' +
unicode(memoir.QUESTIONS_PER_PAGE) + '&skip=' + unicode(skip) + '&key="' +
urllib2.quote(unicode(tech)) + '"&reduce=false').read()
count = urllib2.urlopen(memoir.DB_URL + 'memoir/_design/dev_kunjika/_view/get_by_ts?key="' +
urllib2.quote(unicode(tech)) + '"&reduce=true').read()
count = json.loads(count)['rows']
if len(count) != 0:
count = count[0]['value']
else:
count = 0
questions = json.loads(questions)
qids = []
if len(questions) > 0:
for row in questions['rows']:
qids.append(unicode(row['id']))
if len(qids) > 0:
val_res = memoir.mb.get_multi(qids)
questions_list = []
for qid in qids:
questions_list.append(val_res[unicode(qid)].value)
questions_list = [question for question in questions_list if question['cat'] == cat]
# count = len(questions_list)
if not questions_list and page != 1:
abort(404)
pagination = utility.Pagination(page, memoir.QUESTIONS_PER_PAGE, int(count))
return render_template('browse.html', title='Questions', qpage=True, questions=questions_list,
pagination=pagination, name=g.user.name, role=g.user.role,
user_id=g.user.id, tech=tech, cat=cat, APP_ROOT=memoir.APP_ROOT)
return render_template('browse_form.html', title='Browse Question Form', form=boqForm, ppage=True, name=g.user.name, role=g.user.role,
user_id=g.user.id, APP_ROOT=memoir.APP_ROOT)
return redirect(url_for('login'))
@test_series.route('/edit_test/<element>', methods=['GET', 'POST'])
def edit_test(element):
question = memoir.mb.get(element).value
options = len(question['content']['options'])
class ChoiceForm(Form):
option = RadioField('What type of question do you want?', choices=[('Single choice', 'Single Choice'),
('Multiple choice', 'Multiple choice')])
description = TextAreaField('', [validators.Length(min=20, max=5000), validators.DataRequired()])
option_1 = StringField('Question', [validators.Length(min=1, max=200), validators.DataRequired()])
option_2 = StringField('Question', [validators.Length(min=1, max=200), validators.DataRequired()])
option_3 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
option_4 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
option_5 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
option_6 = StringField('Question', [validators.Length(min=1, max=200), validators.Optional()])
answers = StringField('Answers', [validators.Length(min=1, max=200), validators.DataRequired()])
form = ChoiceForm(request.form)
choices = []
for i in range(0, options):
choices.append(unicode(i+1))
if g.user.id == 'u1':
if form.validate_on_submit() and request.method == 'POST':
# print "editing test question"
option = form.option.data
option_1 = form.option_1.data
option_2 = form.option_2.data
question['content'] = {}
question['content']['description'] = form.description.data
question['answers'] = form.answers.data
if option == 'Single choice':
question['content']['sc'] = True
question['content']['mc'] = False
else:
question['content']['mc'] = True
question['content']['sc'] = False
question['content']['options'] = []
question['content']['options'].append(option_1)
question['content']['options'].append(option_2)
if form.option_3.data != "":
question['content']['options'].append(form.option_3.data)
if form.option_4.data != "":
question['content']['options'].append(form.option_4.data)
if form.option_5.data != "":
question['content']['options'].append(form.option_5.data)
if form.option_6.data != "":
question['content']['options'].append(form.option_6.data)
question['content']['ts'] = int(time())
question['updated'] = question['content']['ts']
question['content']['ip'] = request.remote_addr
memoir.mb.replace(question['qid'], question) # tq stands for test question. prefix is used for increasing period
# before uuid will repeat
return redirect(url_for('test_series.browse_objective_questions'))
return render_template('edit_test.html', title='Edit Objective Question', form=form, ppage=True, name=g.user.name, role=g.user.role,
user_id=g.user.id, question=question, options=zip(choices, question['content']['options']), APP_ROOT=memoir.APP_ROOT)
else:
return render_template('login')