diff --git a/ref-test/admin/templates/admin/components/navbar.html b/ref-test/admin/templates/admin/components/navbar.html index 7fa655f..a5b8d00 100644 --- a/ref-test/admin/templates/admin/components/navbar.html +++ b/ref-test/admin/templates/admin/components/navbar.html @@ -42,10 +42,10 @@ aria-labelledby="dropdown-account" >
  • - Manage Users + Users
  • - Manage Questions + Question Datasets
  • diff --git a/ref-test/admin/templates/admin/settings/index.html b/ref-test/admin/templates/admin/settings/index.html index 9624223..aca14a6 100644 --- a/ref-test/admin/templates/admin/settings/index.html +++ b/ref-test/admin/templates/admin/settings/index.html @@ -1 +1,93 @@ -{% extends "admin/components/base.html" %} \ No newline at end of file +{% extends "admin/components/base.html" %} +{% block title %}Settings — SKA Referee Test | Admin Console{% endblock %} + +{% block content %} +

    + Settings +

    +
    +
    +
    +
    +
    +
    Admin Users
    +
    + + + + + + + + + {% for user in users %} + + + + + {% endfor %} + +
    + Username + + Email Address +
    + {{ user.username }} + + {{ user.email }} +
    +
    + Manage Users +
    +
    +
    +
    +
    +
    +
    Question Datasets
    + {% if datasets %} +
    + + + + + + + + + {% for dataset in datasets %} + + + + + {% endfor %} + +
    + File Name + + Exams +
    + {{ dataset.filename }} + + {{ dataset.use }} +
    +
    + Manage Datasets + {% else %} +
    + There are currently no question datasets uploaded. +
    + Upload Dataset + {% endif %} +
    +
    +
    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/ref-test/admin/views.py b/ref-test/admin/views.py index 6bc2523..5ef7f96 100644 --- a/ref-test/admin/views.py +++ b/ref-test/admin/views.py @@ -16,7 +16,7 @@ import secrets from main import mail from datetime import datetime, date from .models.tests import Test -from common.data_tools import get_default_dataset, get_time_options, available_datasets +from common.data_tools import get_default_dataset, get_time_options, available_datasets, get_datasets views = Blueprint( 'admin_views', @@ -76,13 +76,26 @@ def disable_if_logged_in(function): @admin_account_required @login_required def home(): - return render_template('/admin/index.html') + tests = db.tests.find() + results = decrypt_find(db.entries, {}) + current_tests = [ test for test in tests if test['expiry_date'].date() >= date.today() and test['start_date'].date() <= date.today() ] + current_tests.sort(key= lambda x: x['expiry_date'], reverse=True) + upcoming_tests = [ test for test in tests if test['start_date'].date() > date.today()] + upcoming_tests.sort(key= lambda x: x['start_date']) + recent_results = [result for result in results if 'submission_time' in result ] + recent_results.sort(key= lambda x: x['submission_time'], reverse=True) + for result in recent_results: + result['percent'] = round(100*result['results']['score']/result['results']['max']) + return render_template('/admin/index.html', current_tests = current_tests[:5], upcomimg_tests = upcoming_tests[:5], recent_results = recent_results[:5]) @views.route('/settings/') @admin_account_required @login_required def settings(): - return render_template('/admin/settings/index.html') + users = decrypt_find(db.users, {}) + users.sort(key= lambda x: x['username']) + datasets = get_datasets() + return render_template('/admin/settings/index.html', users=users[:5], datasets=datasets[:5]) @views.route('/settings/users/', methods=['GET','POST']) @admin_account_required @@ -245,22 +258,7 @@ def questions(): from common.data_tools import check_json_format, validate_json_contents, store_data_file form = UploadDataForm() if request.method == 'GET': - files = glob(os.path.join(app.config["DATA_FILE_DIRECTORY"],'*.json')) - data = [] - if files: - for file in files: - filename = file.rsplit('/')[-1] - with open(file) as _file: - load = loads(_file.read()) - _author = load['meta']['author'] - author = decrypt_find_one(db.users, {'_id': _author})['username'] - data_element = { - 'filename': filename, - 'timestamp': datetime.strptime(load['meta']['timestamp'], '%Y-%m-%d %H%M%S'), - 'author': author, - 'use': len(load['meta']['tests']) - } - data.append(data_element) + data = get_datasets() default = get_default_dataset() return render_template('/admin/settings/questions.html', form=form, data=data, default=default) if request.method == 'POST': diff --git a/ref-test/common/data_tools.py b/ref-test/common/data_tools.py index 8226c52..97916bd 100644 --- a/ref-test/common/data_tools.py +++ b/ref-test/common/data_tools.py @@ -3,13 +3,12 @@ import pathlib from json import dump, loads from datetime import datetime, timedelta from glob import glob - -from flask.json import jsonify -from main import app from random import shuffle - from werkzeug.utils import secure_filename +from main import app, db +from .security.database import decrypt_find_one + def check_data_folder_exists(): if not os.path.exists(app.config['DATA_FILE_DIRECTORY']): pathlib.Path(app.config['DATA_FILE_DIRECTORY']).mkdir(parents='True', exist_ok='True') @@ -199,4 +198,23 @@ def get_time_options(): ('90', '1 hour 30 minutes'), ('120', '2 hours') ] - return time_options \ No newline at end of file + return time_options + +def get_datasets(): + files = glob(os.path.join(app.config["DATA_FILE_DIRECTORY"],'*.json')) + data = [] + if files: + for file in files: + filename = file.rsplit('/')[-1] + with open(file) as _file: + load = loads(_file.read()) + _author = load['meta']['author'] + author = decrypt_find_one(db.users, {'_id': _author})['username'] + data_element = { + 'filename': filename, + 'timestamp': datetime.strptime(load['meta']['timestamp'], '%Y-%m-%d %H%M%S'), + 'author': author, + 'use': len(load['meta']['tests']) + } + data.append(data_element) + return data \ No newline at end of file