ska-referee-test/ref-test/quiz/views.py
viveksantayana 2064ac508a Finished data upload
Refactored to move security package inside common
Moved data folder to process root.
2021-11-28 02:30:46 +00:00

59 lines
2.0 KiB
Python

from flask import Blueprint, render_template, request, redirect, jsonify
from datetime import datetime
from uuid import uuid4
from main import db
from common.security import encrypt
views = Blueprint(
'quiz_views',
__name__,
static_url_path='',
template_folder='templates',
static_folder='static'
)
@views.route('/')
@views.route('/home/')
def home():
return render_template('/quiz/index.html')
@views.route('/start/', methods = ['GET', 'POST'])
def start():
from .forms import StartQuiz
form = StartQuiz()
if request.method == 'GET':
return render_template('/quiz/start-quiz.html', form=form)
if request.method == 'POST':
if form.validate_on_submit():
name = {
'first_name': request.form.get('first_name'),
'surname': request.form.get('surname')
}
email = request.form.get('email')
club = request.form.get('club')
test_code = request.form.get('test_code').replace('', '')
user_code = request.form.get('user_code')
user_code = None if user_code == '' else user_code
if not db.tests.find_one({'test_code': test_code}):
return jsonify({'error': 'The exam code you entered is invalid.'}), 400
attempt = {
'_id': uuid4().hex,
'name': encrypt(name),
'email': encrypt(email),
'club': encrypt(club),
'test-code': test_code,
'user_code': user_code,
'start_time': datetime.utcnow(),
'status': 'started'
}
if db.results.insert(attempt):
return jsonify({ 'success': f'Exam started at started {attempt["start_time"].strftime("%H:%M:%S")}.' })
else:
errors = [*form.errors]
return jsonify({ 'error': errors}), 400
@views.route('/privacy/')
def privacy():
return render_template('/quiz/privacy.html')