-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
360 lines (266 loc) · 9.9 KB
/
server.py
File metadata and controls
360 lines (266 loc) · 9.9 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from flask import Flask, session
from flask import render_template, redirect
from flask import Response, request, jsonify
import json
import datetime
import uuid
import os
import sys
# Demo YouTube Video: https://youtu.be/1a27eqAXpLg
app = Flask(__name__)
env_key = os.environ.get('PITCHWIZARD_KEY')
print(env_key)
if env_key:
app.secret_key = env_key
else:
app.secret_key = "super_secret_key"
env_proxy = os.environ.get('PITCHWIZARD_PROXY')
if env_proxy:
try:
env_proxy = int(env_proxy)
except:
print("PITCHWIZARD_PROXY env variable should be 0 or 1")
sys.exit(1)
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)
lessons = []
questions = []
correct_answers = {}
# FUNCTIONS
def log_route(session, remote_addr, route_name):
current_time = datetime.datetime.now()
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
if 'session_timestamps' not in session:
session['session_timestamps'] = []
if 'page_entered' not in session:
session['page_entered'] = {}
session['page_entered'][route_name] = current_time
session['session_timestamps'].append((route_name, remote_addr, current_time))
with open("logs/page.txt", "a") as f:
f.write(f"({current_time}) User {session["session_id"]} ({remote_addr}) visited {route_name}.\n")
def log_quiz_finish(session, quiz_id, number_correct):
current_time = datetime.datetime.now()
if 'session_id' not in session:
session['session_id'] = str(uuid.uuid4())
user_answers = get_answers(session, quiz_id)
with open("logs/quiz.txt", "a") as f:
f.write(f"({current_time}) User {session["session_id"]} finished quiz {quiz_id} with answers {user_answers} and {number_correct} correct.\n")
def read_json():
global lessons
global questions
with open("data/learn.json", 'r') as f:
lessons = json.load(f)
with open("data/quiz.json", 'r') as f:
questions = json.load(f)
lessons = lessons['lessons']
def get_correct_answer(quiz_id, question_id):
global correct_answers
answer_key = correct_answers
res = None
new_answer_key = None
if answer_key:
new_answer_key = answer_key.copy()
if not new_answer_key:
new_answer_key = {}
if not quiz_id in new_answer_key:
new_answer_key[quiz_id] = {}
if question_id in new_answer_key[quiz_id]:
res = new_answer_key[quiz_id][question_id]
else:
res = None
correct_answers = new_answer_key
return res
def set_correct_answer(quiz_id, question_id, answer):
global correct_answers
answer_key = correct_answers
res = None
new_answer_key = None
if answer_key:
new_answer_key = answer_key.copy()
if not new_answer_key:
new_answer_key = {}
if not quiz_id in new_answer_key:
new_answer_key[quiz_id] = {}
new_answer_key[quiz_id][question_id] = answer
correct_answers = new_answer_key
def set_correct_answers_radio(quiz_id, question_id, question):
for option in question["options"]:
if option["optionCorrect"]:
set_correct_answer(quiz_id, question_id, option["optionId"])
def set_correct_answers_match(quiz_id, question_id, question):
corr_ans = {}
for option in question["options"]:
corr_ans[str(option["optionId"])] = option["optionId"]
set_correct_answer(quiz_id, question_id, corr_ans)
def set_correct_answers_sentence(quiz_id, question_id, question):
corr_ans = {}
for sentence in question["sentences"]:
for option in sentence["sentenceOptions"]:
if option["optionCorrect"] == True:
corr_ans[str(sentence["sentenceId"])] = option["optionId"]
set_correct_answer(quiz_id, question_id, corr_ans)
def set_correct_answers():
global questions
global correct_answers
for quiz_id in questions:
for question_id in questions[quiz_id]["questions"]:
question = questions[quiz_id]["questions"][question_id]
questionType = question["questionType"]
if questionType == "Identify":
set_correct_answers_radio(quiz_id, question_id, question)
elif questionType == "Match":
set_correct_answers_match(quiz_id, question_id, question)
elif questionType == "Select":
set_correct_answers_radio(quiz_id, question_id, question)
elif questionType == "Sentence":
set_correct_answers_sentence(quiz_id, question_id, question)
def print_answer_key(session):
answer_key = session.get("answer_key")
if answer_key:
print(answer_key)
else:
print(None)
def get_answers(session, quiz_id):
answer_key = session.get("answer_key")
if not answer_key:
return {}
if not quiz_id in answer_key:
return {}
return answer_key[quiz_id]
def get_answer(session, quiz_id, question_id):
quiz_answer_key = get_answers(session, quiz_id)
if question_id not in quiz_answer_key:
return None
return quiz_answer_key[question_id]
def set_answer(session, quiz_id, question_id, answer):
answer_key = session.get("answer_key")
res = None
new_answer_key = None
if answer_key:
new_answer_key = answer_key.copy()
if not new_answer_key:
new_answer_key = {}
if not quiz_id in new_answer_key:
new_answer_key[quiz_id] = {}
new_answer_key[quiz_id][question_id] = answer
session["answer_key"] = new_answer_key
def is_correct(user_answer, correct_answer):
if user_answer == correct_answer:
return True
return False
def calculate_number_correct(session, quiz_id):
correct_count = 0
for question_id in questions[quiz_id]["questions"]:
user_answer = get_answer(session, quiz_id, question_id)
correct_answer = get_correct_answer(quiz_id, question_id)
if not user_answer:
continue
if is_correct(user_answer, correct_answer):
correct_count += 1
return correct_count
# ROUTES
@app.route('/')
def home():
log_route(session, request.remote_addr, "/")
return render_template('home.html')
@app.route('/learn/<int:learn_id>')
def learn(learn_id):
log_route(session, request.remote_addr, f"/learn/{learn_id}")
learn_id = str(learn_id)
if learn_id not in lessons:
return render_template("404.html"), 404
lesson = lessons[learn_id]
return render_template('learn.html', lesson=lesson)
def render_quiz(session, quiz_id, question_id:int):
question_id = str(question_id)
if question_id == "0":
return render_template('quiz_welcome.html')
if quiz_id not in questions:
return render_template("404.html"), 404
if question_id not in questions[quiz_id]["questions"]:
return render_template("404.html"), 404
question = questions[quiz_id]["questions"][question_id]
user_answer = get_answer(session, quiz_id, question_id)
user_answered = False
if user_answer:
user_answered = True
correct_answer = None
if user_answered:
correct_answer = get_correct_answer(quiz_id, question_id)
correct = is_correct(user_answer, correct_answer)
next_route = None
if "nextRoute" in questions[quiz_id]:
next_route = questions[quiz_id]["nextRoute"]
return render_template('quiz.html',
question=question,
quiz_id=quiz_id,
question_id=question_id,
last_question=len(questions[quiz_id]["questions"]),
answered=user_answered,
answer=user_answer,
correct_answer=correct_answer,
correct=correct,
next_route=next_route)
@app.route('/quiz/<int:question_id>')
def quiz(question_id):
log_route(session, request.remote_addr, f"/quiz/{question_id}")
return render_quiz(session, "main", question_id)
@app.route('/quiz/<quiz_id>/<int:question_id>')
def quiz_general(quiz_id, question_id):
log_route(session, request.remote_addr, f"/quiz/{quiz_id}/{question_id}")
return render_quiz(session, quiz_id, question_id)
def render_quiz_results(session, quiz_id):
if quiz_id not in questions:
return render_template("404.html"), 404
total_questions = len(questions[quiz_id]["questions"])
number_correct = calculate_number_correct(session, quiz_id)
log_quiz_finish(session, quiz_id, number_correct)
return render_template('quiz_results.html',
quiz_id=quiz_id,
number_correct=number_correct,
total_questions=total_questions)
@app.route('/quiz_results')
def quiz_results():
log_route(session, request.remote_addr, "/quiz_results")
return render_quiz_results(session, "main")
@app.route('/quiz_results/<quiz_id>')
def quiz_results_general(quiz_id):
log_route(session, request.remote_addr, f"/quiz_results/{quiz_id}")
return render_quiz_results(session, quiz_id)
@app.errorhandler(404)
def http_error_handler(error):
log_route(session, request.remote_addr, "404")
return render_template("404.html"), 404
# AJAX FUNCTIONS
@app.route('/check_answer', methods=['GET', 'POST'])
def check_answer():
global questions
log_route(session, request.remote_addr, "/check_answer")
try:
json_data = request.get_json()
user_answer = json_data['user_answer']
quiz_id = json_data['quiz_id']
question_id = json_data['question_id']
set_answer(session, quiz_id, question_id, user_answer)
return jsonify(success=True)
except:
return jsonify(success=False)
@app.route('/clear_session', methods=['GET', 'POST'])
def clear_session():
log_route(session, request.remote_addr, "/clear_session")
try:
session.clear()
return jsonify(success=True)
except:
return jsonify(success=False)
# SETUP
if not os.path.exists("logs"):
os.makedirs("logs")
read_json()
set_correct_answers()
# DRIVER
if __name__ == '__main__':
app.run(debug = True)