From 9004f60a0c27cc30bdbb7904893fb5187d96ef37 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 24 Jan 2020 13:11:50 -0800 Subject: [PATCH] Admin dashboard improvements --- amanuensis/resources/user.json | 2 ++ amanuensis/server/auth.py | 6 ++++- amanuensis/server/home.py | 33 +++++++++++----------------- amanuensis/templates/home/admin.html | 19 ++++++++-------- amanuensis/templates/macros.html | 18 +++++++++++++++ 5 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 amanuensis/templates/macros.html diff --git a/amanuensis/resources/user.json b/amanuensis/resources/user.json index 2ef557f..bb6505d 100644 --- a/amanuensis/resources/user.json +++ b/amanuensis/resources/user.json @@ -6,6 +6,8 @@ "email": null, "password": null, "created": null, + "last_login": null, + "last_activity": null, "new_password_required": true, "is_admin": false } \ No newline at end of file diff --git a/amanuensis/server/auth.py b/amanuensis/server/auth.py index 7896d0e..4ee6b3b 100644 --- a/amanuensis/server/auth.py +++ b/amanuensis/server/auth.py @@ -1,10 +1,12 @@ +import time + from flask import Blueprint, render_template, redirect, url_for, flash from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, BooleanField, SubmitField from wtforms.validators import DataRequired from flask_login import current_user, login_user, logout_user, login_required -from amanuensis.config import logger +from amanuensis.config import logger, json_rw from amanuensis.user import UserModel class LoginForm(FlaskForm): @@ -30,6 +32,8 @@ def get_bp(login_manager): if u is not None and u.check_password(form.password.data): remember_me = form.remember.data login_user(u, remember=remember_me) + with json_rw(u.config_path) as cfg: + cfg.last_login = int(time.time()) logger.info("Logged in user '{}' ({})".format( u.username, u.uid)) return redirect(url_for('home.home')) diff --git a/amanuensis/server/home.py b/amanuensis/server/home.py index 6cc57ea..b16d223 100644 --- a/amanuensis/server/home.py +++ b/amanuensis/server/home.py @@ -7,11 +7,9 @@ from flask_wtf import FlaskForm from wtforms import TextAreaField, SubmitField, StringField from amanuensis.config import json_ro +from amanuensis.lexicon import LexiconModel +from amanuensis.user import UserModel -class AdminDashboardForm(FlaskForm): - lexiconName = StringField("Lexicon name") - configText = TextAreaField("Config file") - submit = SubmitField("Submit") def admin_required(route): """Requires the user to be an admin""" @@ -23,6 +21,7 @@ def admin_required(route): return route(*args, **kwargs) return admin_route + def get_bp(): """Create a blueprint for pages outside of a specific lexicon""" bp = Blueprint('home', __name__, url_prefix='/home') @@ -34,22 +33,16 @@ def get_bp(): @bp.route('/admin/', methods=['GET', 'POST']) @admin_required def admin(): - form = AdminDashboardForm() - if not form.is_submitted(): - return render_template('home/admin.html', form=form) + users = [] + with json_ro('user', 'index.json') as index: + for name, uid in index.items(): + users.append(UserModel.by(uid=uid)) - if form.lexiconName.data: - lid = None - with json_ro('lexicon', 'index.json') as index: - lid = index.get(form.lexiconName.data) - if lid is not None: - with json_ro('lexicon', lid, 'config.json') as cfg: - form.configText.data = json.dumps(cfg, indent=2) - form.lexiconName.data = "" - elif form.configText.data: - return "Update config" - else: - pass - return render_template('home/admin.html', form=form) + lexicons = [] + with json_ro('lexicon', 'index.json') as index: + for name, lid in index.items(): + lexicons.append(LexiconModel.by(lid=lid)) + + return render_template('home/admin.html', users=users, lexicons=lexicons) return bp diff --git a/amanuensis/templates/home/admin.html b/amanuensis/templates/home/admin.html index 7f02ec0..4df6049 100644 --- a/amanuensis/templates/home/admin.html +++ b/amanuensis/templates/home/admin.html @@ -1,18 +1,19 @@ {% extends "page_2col.html" %} +{% import 'macros.html' as macros %} {% block title %}Admin | Amanuensis{% endblock %} {% block header %}

Amanuensis - Admin Dashboard

{% endblock %} -{% block sb_topline %}{{ current_user.displayname }}{% endblock %} -{% block sb_logout %}Log out{% endblock %} {% block sb_home %}Home{% endblock %} -{% set template_sidebar_rows = [self.sb_topline(), self.sb_logout(), self.sb_home()] %} +{% set template_sidebar_rows = [self.sb_home()] %} {% block main %} -
- {{ form.hidden_tag() }} -

{{ form.lexiconName.label }}
{{ form.lexiconName(size=32) }}

-

{{ form.configText.label }}
{{ form.configText(rows=20) }}

-

{{ form.submit() }}

-
+

Users:

+{% for user in users %} +{{ macros.dashboard_user_item(user) }} +{% endfor %} +

Lexicons:

+{% for lexicon in lexicons %} +{{ macros.dashboard_lexicon_item(lexicon) }} +{% endfor %} {% endblock %} {% set template_content_blocks = [self.main()] %} \ No newline at end of file diff --git a/amanuensis/templates/macros.html b/amanuensis/templates/macros.html new file mode 100644 index 0000000..9a41554 --- /dev/null +++ b/amanuensis/templates/macros.html @@ -0,0 +1,18 @@ +{% macro dashboard_lexicon_item(lexicon) %} +
+

+ + Lexicon {{ lexicon.name }} + + [{{ lexicon.status().capitalize() }}] +

+

{{ lexicon.prompt }}

+
+{% endmacro %} + +{% macro dashboard_user_item(user) %} +
+

{{ user.username }} {% if user.username != user.displayname %} / {{ user.displayname }}{% endif %} ({{user.uid}})

+

Last activity: {{ user.last_activity }} — Last login: {{ user.last_login }}

+
+{% endmacro %} \ No newline at end of file