Finished data upload
Refactored to move security package inside common Moved data folder to process root.
This commit is contained in:
		@@ -3,7 +3,7 @@ from flask.helpers import flash, url_for
 | 
			
		||||
from flask.json import jsonify
 | 
			
		||||
from .models.users import User
 | 
			
		||||
from uuid import uuid4
 | 
			
		||||
from security.database import decrypt_find_one, encrypted_update
 | 
			
		||||
from common.security.database import decrypt_find_one, encrypted_update
 | 
			
		||||
from werkzeug.security import check_password_hash
 | 
			
		||||
 | 
			
		||||
from main import db
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
from flask_wtf import FlaskForm
 | 
			
		||||
from flask_wtf.file import FileField, FileRequired, FileAllowed
 | 
			
		||||
from wtforms import StringField, PasswordField, BooleanField, DateField, SelectField
 | 
			
		||||
from wtforms.validators import InputRequired, Email, Length, EqualTo, Optional
 | 
			
		||||
from datetime import date, timedelta
 | 
			
		||||
@@ -53,4 +54,7 @@ class CreateTest(FlaskForm):
 | 
			
		||||
        ('120', '2 hours')
 | 
			
		||||
    ]
 | 
			
		||||
    expiry_date = DateField('Expiry Date', format="%Y-%m-%d", validators=[InputRequired()], default = date.today() + timedelta(days=1) )
 | 
			
		||||
    time_limit = SelectField('Time Limit', choices=time_options)
 | 
			
		||||
    time_limit = SelectField('Time Limit', choices=time_options)
 | 
			
		||||
 | 
			
		||||
class UploadDataForm(FlaskForm):
 | 
			
		||||
    data_file = FileField('Data File', validators=[FileRequired(), FileAllowed(['json'])])
 | 
			
		||||
@@ -5,7 +5,7 @@ from flask import flash, jsonify
 | 
			
		||||
import secrets
 | 
			
		||||
 | 
			
		||||
from main import db
 | 
			
		||||
from security import encrypt
 | 
			
		||||
from common.security import encrypt
 | 
			
		||||
 | 
			
		||||
class Test:
 | 
			
		||||
    def __init__(self, _id=None, start_date=None, expiry_date=None, time_limit=None, creator=None):
 | 
			
		||||
 
 | 
			
		||||
@@ -350,13 +350,18 @@ $('form[name=form-update-account]').submit(function(event) {
 | 
			
		||||
    event.preventDefault();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
$('.delete-test').click(function(event) {
 | 
			
		||||
$('form[name=form-create-test]').submit(function(event) {
 | 
			
		||||
    
 | 
			
		||||
    _id = $(this).data('_id')
 | 
			
		||||
    var $form = $(this);
 | 
			
		||||
    var alert = document.getElementById('alert-box');
 | 
			
		||||
    var data = $form.serialize();
 | 
			
		||||
    alert.innerHTML = ''
 | 
			
		||||
 | 
			
		||||
    $.ajax({
 | 
			
		||||
        url: `/admin/tests/delete/${_id}`,
 | 
			
		||||
        type: 'GET',
 | 
			
		||||
        url: window.location.pathname,
 | 
			
		||||
        type: 'POST',
 | 
			
		||||
        data: data,
 | 
			
		||||
        dataType: 'json',
 | 
			
		||||
        success: function(response) {
 | 
			
		||||
            window.location.href = '/admin/tests/';
 | 
			
		||||
        },
 | 
			
		||||
@@ -386,20 +391,78 @@ $('.delete-test').click(function(event) {
 | 
			
		||||
    event.preventDefault();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Edit and Delete Test Button Handlers
 | 
			
		||||
 | 
			
		||||
$('form[name=form-create-test]').submit(function(event) {
 | 
			
		||||
$('form[name=form-upload-questions]').submit(function(event) {
 | 
			
		||||
    
 | 
			
		||||
    var $form = $(this);
 | 
			
		||||
    var alert = document.getElementById('alert-box');
 | 
			
		||||
    var data = $form.serialize();
 | 
			
		||||
    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,
 | 
			
		||||
        type: 'POST',
 | 
			
		||||
        data: data,
 | 
			
		||||
        dataType: 'json',
 | 
			
		||||
        processData: false,
 | 
			
		||||
        contentType: false,
 | 
			
		||||
        success: function(response) {
 | 
			
		||||
            if (typeof response.success === 'string' || response.success instanceof String) {
 | 
			
		||||
                alert.innerHTML = alert.innerHTML + `
 | 
			
		||||
                <div class="alert alert-success alert-dismissible fade show" role="alert">
 | 
			
		||||
                    <i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
 | 
			
		||||
                    ${response.success}
 | 
			
		||||
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
 | 
			
		||||
                </div>
 | 
			
		||||
                `;
 | 
			
		||||
            } else if (response.success instanceof Array) {
 | 
			
		||||
                for (var i = 0; i < response.success.length; i ++) {
 | 
			
		||||
                    alert.innerHTML = alert.innerHTML + `
 | 
			
		||||
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
 | 
			
		||||
                        <i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
 | 
			
		||||
                        ${response.success[i]}
 | 
			
		||||
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
 | 
			
		||||
                    </div>
 | 
			
		||||
                    `;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
        error: function(response) {
 | 
			
		||||
            if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) {
 | 
			
		||||
                alert.innerHTML = alert.innerHTML + `
 | 
			
		||||
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
 | 
			
		||||
                    <i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
 | 
			
		||||
                    ${response.responseJSON.error}
 | 
			
		||||
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
 | 
			
		||||
                </div>
 | 
			
		||||
                `;
 | 
			
		||||
            } else if (response.responseJSON.error instanceof Array) {
 | 
			
		||||
                for (var i = 0; i < response.responseJSON.error.length; i ++) {
 | 
			
		||||
                    alert.innerHTML = alert.innerHTML + `
 | 
			
		||||
                    <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>
 | 
			
		||||
                    `;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    event.preventDefault();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Edit and Delete Test Button Handlers
 | 
			
		||||
 | 
			
		||||
$('.delete-test').click(function(event) {
 | 
			
		||||
    
 | 
			
		||||
    _id = $(this).data('_id')
 | 
			
		||||
 | 
			
		||||
    $.ajax({
 | 
			
		||||
        url: `/admin/tests/delete/${_id}`,
 | 
			
		||||
        type: 'GET',
 | 
			
		||||
        success: function(response) {
 | 
			
		||||
            window.location.href = '/admin/tests/';
 | 
			
		||||
        },
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1,23 @@
 | 
			
		||||
{% extends "admin/components/base.html" %}
 | 
			
		||||
{% extends "admin/components/base.html" %}
 | 
			
		||||
{% block title %} SKA Referee Test | Upload Questions {% endblock %}
 | 
			
		||||
{% block content %}
 | 
			
		||||
    <!-- <h1>Upload Question Dataset</h1> -->
 | 
			
		||||
    <div class="form-container">
 | 
			
		||||
        <form name="form-upload-questions" method="post" action="#" class="form-signin" enctype="multipart/form-data">
 | 
			
		||||
            <h2 class="form-signin-heading">Upload Question Dataset</h2>
 | 
			
		||||
            {{ form.hidden_tag() }}
 | 
			
		||||
            {{ form.data_file() }}
 | 
			
		||||
            {% include "admin/components/client-alerts.html" %}
 | 
			
		||||
            <div class="container form-submission-button">
 | 
			
		||||
                <div class="row">
 | 
			
		||||
                    <div class="col text-center">
 | 
			
		||||
                        <button title="Create User" class="btn btn-md btn-success btn-block" type="submit">
 | 
			
		||||
                            <i class="bi bi-file-earmark-arrow-up-fill button-icon"></i>
 | 
			
		||||
                            Upload Dataset
 | 
			
		||||
                        </button>
 | 
			
		||||
                    </div>
 | 
			
		||||
                </div>
 | 
			
		||||
            </div>
 | 
			
		||||
        </form>
 | 
			
		||||
    </div>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
@@ -1,9 +1,10 @@
 | 
			
		||||
from flask import Blueprint, render_template, flash, redirect, request, jsonify, abort
 | 
			
		||||
from flask.helpers import url_for
 | 
			
		||||
from functools import wraps
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
from werkzeug.security import check_password_hash
 | 
			
		||||
from security.database import decrypt_find, decrypt_find_one
 | 
			
		||||
from common.security.database import decrypt_find, decrypt_find_one
 | 
			
		||||
from .models.users import User
 | 
			
		||||
from flask_mail import Message
 | 
			
		||||
from main import db
 | 
			
		||||
@@ -231,11 +232,27 @@ def update_user(_id:str):
 | 
			
		||||
            errors = [*form.user_password.errors, *form.email.errors, *form.password.errors, *form.password_reenter.errors]
 | 
			
		||||
            return jsonify({ 'error': errors}), 400
 | 
			
		||||
 | 
			
		||||
@views.route('/settings/questions/')
 | 
			
		||||
@views.route('/settings/questions/', methods=['GET', 'POST'])
 | 
			
		||||
@admin_account_required
 | 
			
		||||
@login_required
 | 
			
		||||
def questions():
 | 
			
		||||
    return render_template('/admin/settings/questions.html')
 | 
			
		||||
    from main import app
 | 
			
		||||
    from .models.forms import UploadDataForm
 | 
			
		||||
    from common.data_tools import check_json_format, validate_json_contents, store_data_file
 | 
			
		||||
    form = UploadDataForm()
 | 
			
		||||
    if request.method == 'GET':
 | 
			
		||||
        return render_template('/admin/settings/questions.html', form=form)
 | 
			
		||||
    if request.method == 'POST':
 | 
			
		||||
        if form.validate_on_submit():
 | 
			
		||||
            upload = form.data_file.data
 | 
			
		||||
            if not check_json_format(upload):
 | 
			
		||||
                return jsonify({ 'error': 'Invalid file selected. Please upload a JSON file.'}), 400
 | 
			
		||||
            if not validate_json_contents(upload):
 | 
			
		||||
                return jsonify({'error': 'The data in the file is invalid.'}), 400
 | 
			
		||||
            store_data_file(upload)
 | 
			
		||||
            return jsonify({ 'success': 'File uploaded.'}), 200
 | 
			
		||||
        errors = [*form.errors]
 | 
			
		||||
        return jsonify({ 'error': errors}), 400
 | 
			
		||||
 | 
			
		||||
@views.route('/settings/questions/upload/')
 | 
			
		||||
@admin_account_required
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user