Added submission processing

This commit is contained in:
Vivek Santayana 2022-08-29 17:09:07 +01:00
parent 49b986834f
commit a064bd6b9f
2 changed files with 38 additions and 5 deletions

29
server/app/tools/quiz.py Normal file
View File

@ -0,0 +1,29 @@
from .data import load
def evaluate_answers(answers:list) -> dict:
playbooks = load('playbooks.json')
questions = load('questions.json')
scores = dict.fromkeys(playbooks,0)
for index, answer in enumerate(answers):
question = questions[index]
if type(answer) is list:
answer = answer[0:question['select']]
for _answer in answer:
for match in question['answers'][int(_answer)]['matches']: scores[match] += 1
else:
for match in question['answers'][int(answer)]['matches']: scores[match] += 1
return scores
def compile_results(results:dict) -> dict:
output = {
'all_playbooks': results.copy(),
'playbooks': [ ],
'score': max(results.values()),
'max_score': 0
}
for question in load('questions.json'):
output['max_score'] += question.pop('max', 1)
playbooks = load('playbooks.json')
for playbook, score in results.items():
if score == output['score']: output['playbooks'].append({playbook: playbooks[playbook].copy()})
return output

View File

@ -1,6 +1,7 @@
from .tools.data import check_file, load, render_questions from .tools.data import check_file, load
from .tools.quiz import compile_results, evaluate_answers, render_questions
from flask import Blueprint from flask import Blueprint, jsonify, request
from flask.helpers import abort from flask.helpers import abort
views = Blueprint( views = Blueprint(
@ -14,10 +15,13 @@ def _fetch(data_type):
if data_type == 'questions': return render_questions() if data_type == 'questions': return render_questions()
return load(f'{data_type}.json') return load(f'{data_type}.json')
@views.route('/submit', methods=['POST']) @views.route('/submit/', methods=['POST'])
def _submit(): def _submit():
pass answers = request.json
scores = evaluate_answers(answers)
results = compile_results(results=scores)
return jsonify(results)
@views.route('/results') @views.route('/results/')
def _results(): def _results():
pass pass