From c0cdc5ca502529466a0c2df0a9b7043022aac13b Mon Sep 17 00:00:00 2001 From: viveksantayana Date: Sat, 4 Dec 2021 17:14:28 +0000 Subject: [PATCH] Completed client side time adjustment handling Corrected error display bug Removed redundant auth and models in quiz client app --- ref-test/admin/models/tests.py | 13 ++++--- ref-test/admin/static/js/script.js | 37 ++++++++++---------- ref-test/admin/templates/admin/test.html | 8 ++--- ref-test/admin/views.py | 6 ++-- ref-test/main.py | 2 -- ref-test/quiz/auth.py | 6 ---- ref-test/quiz/models.py | 0 ref-test/quiz/static/js/quiz.js | 14 +++++++- ref-test/quiz/static/js/script.js | 12 ++++--- ref-test/quiz/templates/quiz/client.html | 2 +- ref-test/quiz/templates/quiz/start-quiz.html | 2 +- ref-test/quiz/views.py | 16 ++++++--- 12 files changed, 64 insertions(+), 54 deletions(-) delete mode 100644 ref-test/quiz/auth.py delete mode 100644 ref-test/quiz/models.py diff --git a/ref-test/admin/models/tests.py b/ref-test/admin/models/tests.py index a1b3397..edaaa70 100644 --- a/ref-test/admin/models/tests.py +++ b/ref-test/admin/models/tests.py @@ -41,18 +41,17 @@ class Test: return jsonify({'error': f'Could not create exam. An error occurred.'}), 400 def add_time_adjustment(self, time_adjustment): + user_code = secrets.token_hex(3).upper() adjustment = { - '_id': uuid4().hex, - 'user_code': secrets.token_hex(3).upper(), - 'time_adjustment': time_adjustment + user_code: time_adjustment } - if db.tests.find_one_and_update({'_id': self._id}, {'$push': {'time_adjustments': adjustment}},upsert=False): - flash(f'Time adjustment for {adjustment["time_adjustment"]} minutes has been added. This can be enabled using the user code {adjustment["user_code"]}.') + if db.tests.find_one_and_update({'_id': self._id}, {'$set': {'time_adjustments': adjustment}},upsert=False): + flash(f'Time adjustment for {time_adjustment} minutes has been added. This can be enabled using the user code {user_code}.') return jsonify({'success': adjustment}) return jsonify({'error': 'Failed to add the time adjustment. An error occurred.'}), 400 - def remove_time_adjustment(self, _id): - if db.tests.find_one_and_update({'_id': self._id}, {'$pull': {'time_adjustments': {'_id': _id} }}): + def remove_time_adjustment(self, user_code): + if db.tests.find_one_and_update({'_id': self._id}, {'$unset': {f'time_adjustments.{user_code}': {}}}): message = 'Time adjustment has been deleted.' flash(message, 'success') return jsonify({'success': message}) diff --git a/ref-test/admin/static/js/script.js b/ref-test/admin/static/js/script.js index 184374a..00c3b45 100644 --- a/ref-test/admin/static/js/script.js +++ b/ref-test/admin/static/js/script.js @@ -126,14 +126,16 @@ function error_response(response) { `); } else if (response.responseJSON.error instanceof Array) { + var output = '' for (var i = 0; i < response.responseJSON.error.length; i ++) { - $alert.html(` + output += ` - `); + `; + $alert.html(output); } } @@ -205,25 +207,22 @@ $('.result-action-buttons').click(function(event){ // Script for Deleting Time Adjustment $('.adjustment-delete').click(function(event){ - var _id = $(this).data('_id'); + var user_code = $(this).data('user_code'); var location = window.location.href; - location.replace('#', '') + location = location.replace('#', '') - console.log(location) - - console.log(_id) - $.ajax({ - url: location + 'delete-adjustment/', - type: 'POST', - data: JSON.stringify({'_id': _id}), - contentType: 'application/json', - success: function(response) { - window.location.reload(); - }, - error: function(response){ - error_response(response); - }, - }); + $.ajax({ + url: location + 'delete-adjustment/', + type: 'POST', + data: JSON.stringify({'user_code': user_code}), + contentType: 'application/json', + success: function(response) { + window.location.reload(); + }, + error: function(response){ + error_response(response); + }, + }); event.preventDefault(); }); \ No newline at end of file diff --git a/ref-test/admin/templates/admin/test.html b/ref-test/admin/templates/admin/test.html index 6636cbc..ff845a9 100644 --- a/ref-test/admin/templates/admin/test.html +++ b/ref-test/admin/templates/admin/test.html @@ -110,16 +110,16 @@ - {% for entry in test.time_adjustments %} + {% for key, value in test.time_adjustments.items() %} - {{ entry.user_code }} + {{ key }} - {{ entry.time_adjustment }} + {{ value }} - + diff --git a/ref-test/admin/views.py b/ref-test/admin/views.py index 86bdf17..1ab078b 100644 --- a/ref-test/admin/views.py +++ b/ref-test/admin/views.py @@ -417,7 +417,7 @@ def view_test(_id): return render_template('/admin/test.html', test = test, form = form) if request.method == 'POST': if form.validate_on_submit(): - time = request.form.get('time') + time = int(request.form.get('time')) return Test(_id=_id).add_time_adjustment(time) return jsonify({'error': form.time.errors }), 400 @@ -425,8 +425,8 @@ def view_test(_id): @admin_account_required @login_required def delete_adjustment(_id): - adjustment_id = request.get_json()['_id'] - return Test(_id=_id).remove_time_adjustment(adjustment_id) + user_code = request.get_json()['user_code'] + return Test(_id=_id).remove_time_adjustment(user_code) @views.route('/results/') @admin_account_required diff --git a/ref-test/main.py b/ref-test/main.py index d2f4eb3..47b77d1 100644 --- a/ref-test/main.py +++ b/ref-test/main.py @@ -43,10 +43,8 @@ if __name__ == '__main__': from admin.auth import auth as admin_auth from admin.results import results from quiz.views import views as quiz_views - from quiz.auth import auth as quiz_auth app.register_blueprint(quiz_views, url_prefix = '/') - app.register_blueprint(quiz_auth, url_prefix = '/') app.register_blueprint(admin_views, url_prefix = '/admin/') app.register_blueprint(admin_auth, url_prefix = '/admin/') app.register_blueprint(results, url_prefix = '/admin/results/') diff --git a/ref-test/quiz/auth.py b/ref-test/quiz/auth.py deleted file mode 100644 index 3633fd7..0000000 --- a/ref-test/quiz/auth.py +++ /dev/null @@ -1,6 +0,0 @@ -from flask import Blueprint - -auth = Blueprint( - 'quiz_auth', - __name__, -) \ No newline at end of file diff --git a/ref-test/quiz/models.py b/ref-test/quiz/models.py deleted file mode 100644 index e69de29..0000000 diff --git a/ref-test/quiz/static/js/quiz.js b/ref-test/quiz/static/js/quiz.js index 00b4055..b5f101a 100644 --- a/ref-test/quiz/static/js/quiz.js +++ b/ref-test/quiz/static/js/quiz.js @@ -163,6 +163,18 @@ $("#btn-start-quiz").click(function(event){ time_remaining = get_time_remaining(); clock = setInterval(timer, 1000); } + if (response.time_adjustment > 0) { + const $alert = $("#alert-box"); + $alert.html( + ` + ` + ); + $alert.focus(); + } }, error: function(response) { error_response(response); @@ -507,7 +519,7 @@ function timer() { if (time_remaining > 0) { var timer_display = ''; if (hours > 0) { - timer_display = `${hours.toString()}: `; + timer_display = `${hours.toString()}:`; } if (minutes > 0 || hours > 0) { if (minutes < 10) { diff --git a/ref-test/quiz/static/js/script.js b/ref-test/quiz/static/js/script.js index 09c7d0e..7cd1a5b 100644 --- a/ref-test/quiz/static/js/script.js +++ b/ref-test/quiz/static/js/script.js @@ -37,11 +37,11 @@ $('form[name=form-quiz-start]').submit(function(event) { function error_response(response) { - var alert = $("#alert-box"); - alert.html(''); + const $alert = $("#alert-box"); + $alert.html(''); if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) { - alert.html(` + $alert.html(` `); } else if (response.responseJSON.error instanceof Array) { + var output = '' for (var i = 0; i < response.responseJSON.error.length; i ++) { - alert.html(` + output += ` - `); + `; + $alert.html(output); } } } diff --git a/ref-test/quiz/templates/quiz/client.html b/ref-test/quiz/templates/quiz/client.html index 8ace2bf..b9357d6 100644 --- a/ref-test/quiz/templates/quiz/client.html +++ b/ref-test/quiz/templates/quiz/client.html @@ -8,6 +8,7 @@ {% endblock %} {% block content %} +

Adjust Display Settings

@@ -255,7 +256,6 @@ Submit Exam
-
{% endblock %} {% block script %}