From d3df5fe2d3438da9d201a7321e4ded9eac56f7b4 Mon Sep 17 00:00:00 2001 From: viveksantayana Date: Thu, 25 Nov 2021 23:12:20 +0000 Subject: [PATCH] Quiz registration form --- ref-test/quiz/forms.py | 2 +- ref-test/quiz/static/js/script.js | 44 +++++++++++++++- ref-test/quiz/templates/quiz/privacy.html | 25 +++++++++ ref-test/quiz/templates/quiz/start-quiz.html | 4 +- ref-test/quiz/views.py | 53 ++++++++++++++------ ref-test/security/__init__.py | 21 ++++++-- 6 files changed, 125 insertions(+), 24 deletions(-) diff --git a/ref-test/quiz/forms.py b/ref-test/quiz/forms.py index c0f508f..27574a1 100644 --- a/ref-test/quiz/forms.py +++ b/ref-test/quiz/forms.py @@ -7,5 +7,5 @@ class StartQuiz(FlaskForm): surname = StringField('Surname', validators=[InputRequired(), Length(max=30)]) email = StringField('Email Address', validators=[InputRequired(), Email(message='You must enter a valid email address.'), Length(max=50)]) club = StringField('Affiliated Club (Optional)', validators=[Optional(), Length(max=50)]) - auth_code = StringField('Exam Code', validators=[InputRequired(), Length(min=14, max=14)]) + test_code = StringField('Exam Code', validators=[InputRequired(), Length(min=14, max=14)]) user_code = StringField('User Code (Optional)', validators=[Optional(), Length(min=6, max=6)]) \ No newline at end of file diff --git a/ref-test/quiz/static/js/script.js b/ref-test/quiz/static/js/script.js index 1919828..279761c 100644 --- a/ref-test/quiz/static/js/script.js +++ b/ref-test/quiz/static/js/script.js @@ -3,11 +3,53 @@ $(document).ready(function() { $("body").css("font-family", "opendyslexic3regular") }); - $('.auth-code-input').keyup(function() { + $('.test-code-input').keyup(function() { var input = $(this).val().split("-").join("").split("—").join(""); // remove hyphens and mdashes if (input.length > 0) { input = input.match(new RegExp('.{1,4}', 'g')).join("—"); } $(this).val(input); }); +}); + +$('form[name=form-quiz-start]').submit(function(event) { + + var $form = $(this); + var alert = document.getElementById('alert-box'); + var data = $form.serialize(); + + alert.innerHTML = '' + + $.ajax({ + url: window.location.pathname, + type: 'POST', + data: data, + dataType: 'json', + success: function(response) { + window.location.href = "/admin/login/"; + }, + error: function(response) { + if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) { + alert.innerHTML = alert.innerHTML + ` + + `; + } else if (response.responseJSON.error instanceof Array) { + for (var i = 0; i < response.responseJSON.error.length; i ++) { + alert.innerHTML = alert.innerHTML + ` + + `; + } + } + } + }); + + event.preventDefault(); }); \ No newline at end of file diff --git a/ref-test/quiz/templates/quiz/privacy.html b/ref-test/quiz/templates/quiz/privacy.html index e69de29..8e117df 100644 --- a/ref-test/quiz/templates/quiz/privacy.html +++ b/ref-test/quiz/templates/quiz/privacy.html @@ -0,0 +1,25 @@ +{% extends "quiz/components/base.html" %} + +{% block content %} +

Privacy Policy

+ + +{% endblock %} \ No newline at end of file diff --git a/ref-test/quiz/templates/quiz/start-quiz.html b/ref-test/quiz/templates/quiz/start-quiz.html index e2918db..6987f60 100644 --- a/ref-test/quiz/templates/quiz/start-quiz.html +++ b/ref-test/quiz/templates/quiz/start-quiz.html @@ -23,8 +23,8 @@ {{ form.club.label }}
- {{ form.auth_code(class_="form-control auth-code-input", placeholder="Enter Exam Code") }} - {{ form.auth_code.label }} + {{ form.test_code(class_="form-control test-code-input", placeholder="Enter Exam Code") }} + {{ form.test_code.label }}
{{ form.user_code(class_="form-control", placeholder="Enter User Code") }} diff --git a/ref-test/quiz/views.py b/ref-test/quiz/views.py index 71ee15f..3f0bb35 100644 --- a/ref-test/quiz/views.py +++ b/ref-test/quiz/views.py @@ -1,4 +1,9 @@ -from flask import Blueprint, render_template +from flask import Blueprint, render_template, request, redirect, jsonify +from datetime import datetime +from uuid import uuid4 + +from main import db +from security import encrypt views = Blueprint( 'quiz_views', @@ -17,20 +22,38 @@ def home(): def start(): from .forms import StartQuiz form = StartQuiz() - return render_template('/quiz/start-quiz.html', form=form) + 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 f"""

Privacy Policy

- -