-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (43 loc) · 1.67 KB
/
app.py
File metadata and controls
72 lines (43 loc) · 1.67 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
import random
import uuid
from flask import Flask, render_template, request, g
from abc_code import db, score
app = Flask(__name__)
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route('/')
def index():
questions = db.query_db('select * from questions')
test_idxs = random.sample(set(range(0, len(questions))), 3)
test_set = list()
for idx in test_idxs:
test_set.append(questions[idx])
return render_template('questions.html',
questions=test_set)
@app.route('/scores')
def all_scores():
scores = db.query_db('select * from scores')
return render_template('scores.html',
scores=scores)
@app.route('/complete', methods=['POST'])
def complete():
# map answers to question id at the end of form key name
answers = dict((key.replace('answer_', ''),
request.form.getlist(key)) for key in request.form.keys())
# get in memory map of all questions and points for displaying results
questions = db.query_db('select * from questions')
questions_map = dict()
for question in questions:
questions_map[question['id']] = (question['question'], question['points'])
# collect score for each question in test scores list
test_scores = []
test_id = str(uuid.uuid4())[:5]
for question_id in answers:
answer = answers[question_id][0]
test_scores.append(score.score_answer(question_id, answer, test_id))
# send test score list to view
return render_template('complete.html',
scores=test_scores)