From f086a6e32bff337028e94d1ddcf2fac7486920f8 Mon Sep 17 00:00:00 2001 From: viveksantayana Date: Wed, 1 Dec 2021 08:26:08 +0000 Subject: [PATCH] Streamlined post form handlers for admin console --- ref-test/admin/models/tests.py | 2 +- ref-test/admin/static/css/style.css | 2 +- ref-test/admin/static/js/script.js | 530 ++---------------- .../admin/templates/admin/auth/account.html | 4 +- .../admin/templates/admin/auth/login.html | 2 +- .../admin/templates/admin/auth/register.html | 4 +- .../admin/templates/admin/auth/reset.html | 4 +- .../templates/admin/auth/update-password.html | 4 +- .../templates/admin/settings/delete-user.html | 4 +- .../templates/admin/settings/questions.html | 10 +- .../templates/admin/settings/update-user.html | 4 +- .../admin/templates/admin/settings/users.html | 4 +- ref-test/admin/templates/admin/tests.html | 12 +- ref-test/admin/views.py | 39 +- ref-test/common/data_tools.py | 17 +- ref-test/quiz/static/js/quiz.js | 60 +- ref-test/quiz/static/js/script.js | 51 +- ref-test/quiz/views.py | 6 +- 18 files changed, 158 insertions(+), 601 deletions(-) diff --git a/ref-test/admin/models/tests.py b/ref-test/admin/models/tests.py index 2ba8573..6605ea8 100644 --- a/ref-test/admin/models/tests.py +++ b/ref-test/admin/models/tests.py @@ -14,7 +14,7 @@ class Test: self._id = _id self.start_date = start_date self.expiry_date = expiry_date - self.time_limit = None if time_limit == 'none' or time_limit == '' else int(time_limit) + self.time_limit = None if time_limit == 'none' or time_limit == '' or time_limit == None else int(time_limit) self.creator = creator self.dataset = dataset diff --git a/ref-test/admin/static/css/style.css b/ref-test/admin/static/css/style.css index d719dc2..6672d33 100644 --- a/ref-test/admin/static/css/style.css +++ b/ref-test/admin/static/css/style.css @@ -20,7 +20,7 @@ body { padding-bottom: 40px; } -.form-signin { +.form-display { width: 100%; max-width: 420px; padding: 15px; diff --git a/ref-test/admin/static/js/script.js b/ref-test/admin/static/js/script.js index ad4cd34..23cfe3c 100644 --- a/ref-test/admin/static/js/script.js +++ b/ref-test/admin/static/js/script.js @@ -13,392 +13,37 @@ for(let i = 0; i< dropdownItems.length; i++) { } } -// Form Processing Scripts -$('form[name=form-register]').submit(function(event) { +// General Post Method Form Processing Script +$('form.form-post').submit(function(event) { var $form = $(this); - var alert = document.getElementById('alert-box'); var data = $form.serialize(); - - alert.innerHTML = ''; + var url = $(this).attr('action'); + var rel_success = $(this).data('rel-success'); $.ajax({ - url: window.location.pathname, + url: url, type: 'POST', data: data, dataType: 'json', success: function(response) { - window.location.href = "/admin/login/"; + window.location.href = rel_success; }, 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(); -}); - -$('form[name=form-login]').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/dashboard/"; - }, - 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(); -}); - -$('form[name=form-reset]').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(); -}); - -$('form[name=form-update-password]').submit(function(event) { - - var $form = $(this); - var alert = document.getElementById('alert-box'); - var data = $form.serialize(); - console.log(data) - 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(); -}); - -$('form[name=form-create-user]').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.reload(); - }, - 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(); -}); - -$('form[name=form-delete-user]').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/settings/users/'; - }, - 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(); -}); - -$('form[name=form-update-user]').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/settings/users'; - }, - 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(); -}); - -$('form[name=form-update-account]').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/dashboard/'; - }, - 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(); -}); - -$('form[name=form-create-test]').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/tests/'; - }, - 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 + ` - - `; - } - } + error_response(response); } }); event.preventDefault(); }); +// Form Upload Questions - Special case, needs to handle files. $('form[name=form-upload-questions]').submit(function(event) { var $form = $(this); - var alert = document.getElementById('alert-box'); var data = new FormData($form[0]); var file = $('input[name=data_file]')[0].files[0] data.append('file', file) - alert.innerHTML = ''; $.ajax({ url: window.location.pathname, @@ -410,153 +55,84 @@ $('form[name=form-upload-questions]').submit(function(event) { window.location.reload(); }, 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 + ` - - `; - } - } + error_response(response); } }); event.preventDefault(); }); - // Edit and Delete Test Button Handlers $('.delete-test').click(function(event) { - _id = $(this).data('_id') - + let _id = $(this).data('_id') $.ajax({ - url: `/admin/tests/delete/${_id}`, - type: 'GET', + url: `/admin/tests/delete/`, + type: 'POST', + data: JSON.stringify({'_id': _id}), + contentType: 'application/json', success: function(response) { window.location.href = '/admin/tests/'; }, - 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 + ` - - `; - } - } - } + error: function(response){ + error_response(response); + }, }); event.preventDefault(); }); -// Edit and Delete Dataset Button Handlers -$('.delete-question-dataset').click(function(event) { - - var alert = document.getElementById('alert-box'); - alert.innerHTML = ''; - - var filename = $(this).data('filename'); - var disabled = $(this).hasClass('disabled'); - - if ( !disabled ) { - $.ajax({ - url: `/admin/settings/questions/delete/${filename}`, - type: 'GET', - success: function(response) { - window.location.reload(); - }, - 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(); -}); - +// Edit Dataset Button Handlers $('.edit-question-dataset').click(function(event) { - var alert = document.getElementById('alert-box'); - alert.innerHTML = ''; - var filename = $(this).data('filename'); + var action = $(this).data('action'); var disabled = $(this).hasClass('disabled'); - if ( !disabled ) { + if ( !disabled ) { $.ajax({ - url: `/admin/settings/questions/default/${filename}`, - type: 'GET', + url: `/admin/settings/questions/${action}/`, + type: 'POST', + data: JSON.stringify({'filename': filename}), + contentType: 'application/json', success: function(response) { window.location.reload(); }, - 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 + ` - - `; - } - } - } + error: function(response){ + error_response(response); + }, }); }; event.preventDefault(); }); +function error_response(response) { + + var alert = $("#alert-box"); + alert.html(''); + + if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) { + alert.html(` + + `); + } else if (response.responseJSON.error instanceof Array) { + for (var i = 0; i < response.responseJSON.error.length; i ++) { + alert.html(` + + `); + } + } +} + // Dismiss Cookie Alert $('#dismiss-cookie-alert').click(function(event){ diff --git a/ref-test/admin/templates/admin/auth/account.html b/ref-test/admin/templates/admin/auth/account.html index 28ca8af..1043fc9 100644 --- a/ref-test/admin/templates/admin/auth/account.html +++ b/ref-test/admin/templates/admin/auth/account.html @@ -2,9 +2,9 @@ {% block content %}
-