From a064bd6b9f564a0601d5524c377c75cd8bbc5a79 Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Mon, 29 Aug 2022 17:09:07 +0100 Subject: [PATCH] Added submission processing --- server/app/tools/quiz.py | 29 +++++++++++++++++++++++++++++ server/app/views.py | 14 +++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 server/app/tools/quiz.py diff --git a/server/app/tools/quiz.py b/server/app/tools/quiz.py new file mode 100644 index 0000000..ccbdd67 --- /dev/null +++ b/server/app/tools/quiz.py @@ -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 \ No newline at end of file diff --git a/server/app/views.py b/server/app/views.py index 3e7b947..2bf37bb 100644 --- a/server/app/views.py +++ b/server/app/views.py @@ -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 views = Blueprint( @@ -14,10 +15,13 @@ def _fetch(data_type): if data_type == 'questions': return render_questions() return load(f'{data_type}.json') -@views.route('/submit', methods=['POST']) +@views.route('/submit/', methods=['POST']) 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(): pass \ No newline at end of file