Completed client side time adjustment handling
Corrected error display bug Removed redundant auth and models in quiz client app
This commit is contained in:
@ -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})
|
||||
|
@ -126,14 +126,16 @@ function error_response(response) {
|
||||
</div>
|
||||
`);
|
||||
} else if (response.responseJSON.error instanceof Array) {
|
||||
var output = ''
|
||||
for (var i = 0; i < response.responseJSON.error.length; i ++) {
|
||||
$alert.html(`
|
||||
output += `
|
||||
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
||||
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
|
||||
${response.responseJSON.error[i]}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
`);
|
||||
`;
|
||||
$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();
|
||||
});
|
@ -110,16 +110,16 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for entry in test.time_adjustments %}
|
||||
{% for key, value in test.time_adjustments.items() %}
|
||||
<tr>
|
||||
<td>
|
||||
{{ entry.user_code }}
|
||||
{{ key }}
|
||||
</td>
|
||||
<td>
|
||||
{{ entry.time_adjustment }}
|
||||
{{ value }}
|
||||
</td>
|
||||
<td>
|
||||
<a href="javascript::void(0);" class="btn btn-danger adjustment-delete" title="Delete Adjustment" data-_id="{{ entry._id }}" data-action="delete">
|
||||
<a href="javascript::void(0);" class="btn btn-danger adjustment-delete" title="Delete Adjustment" data-user_code="{{ key }}">
|
||||
<i class="bi bi-slash-circle-fill button-icon"></i>
|
||||
</a>
|
||||
</td>
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user