Admin dashboard improvements

This commit is contained in:
Tim Van Baak 2020-01-24 13:11:50 -08:00
parent f7aa7f2471
commit 9004f60a0c
5 changed files with 48 additions and 30 deletions

View File

@ -6,6 +6,8 @@
"email": null,
"password": null,
"created": null,
"last_login": null,
"last_activity": null,
"new_password_required": true,
"is_admin": false
}

View File

@ -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'))

View File

@ -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
lexicons = []
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)
for name, lid in index.items():
lexicons.append(LexiconModel.by(lid=lid))
return render_template('home/admin.html', users=users, lexicons=lexicons)
return bp

View File

@ -1,18 +1,19 @@
{% extends "page_2col.html" %}
{% import 'macros.html' as macros %}
{% block title %}Admin | Amanuensis{% endblock %}
{% block header %}<h2>Amanuensis - Admin Dashboard</h2>{% endblock %}
{% block sb_topline %}<b>{{ current_user.displayname }}</b>{% endblock %}
{% block sb_logout %}<a href="{{ url_for('auth.logout') }}">Log out</a>{% endblock %}
{% block sb_home %}<a href="{{ url_for('home.home') }}">Home</a>{% endblock %}
{% set template_sidebar_rows = [self.sb_topline(), self.sb_logout(), self.sb_home()] %}
{% set template_sidebar_rows = [self.sb_home()] %}
{% block main %}
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>{{ form.lexiconName.label }}<br>{{ form.lexiconName(size=32) }}</p>
<p>{{ form.configText.label }}<br>{{ form.configText(rows=20) }}</p>
<p>{{ form.submit() }}</p>
</form>
<p>Users:</p>
{% for user in users %}
{{ macros.dashboard_user_item(user) }}
{% endfor %}
<p>Lexicons:</p>
{% for lexicon in lexicons %}
{{ macros.dashboard_lexicon_item(lexicon) }}
{% endfor %}
{% endblock %}
{% set template_content_blocks = [self.main()] %}

View File

@ -0,0 +1,18 @@
{% macro dashboard_lexicon_item(lexicon) %}
<div class="dashboard-lexicon-item dashboard-lexicon-{{ lexicon.status() }}">
<p>
<span class="dashboard-lexicon-item-title">
<a href="{{ url_for('lexicon.session', name=lexicon.name) }}">Lexicon {{ lexicon.name }}</a>
</span>
[{{ lexicon.status().capitalize() }}]
</p>
<p><i>{{ lexicon.prompt }}</i></p>
</div>
{% endmacro %}
{% macro dashboard_user_item(user) %}
<div class="dashboard-lexicon-item">
<p><b>{{ user.username }}</b> {% if user.username != user.displayname %} / {{ user.displayname }}{% endif %} ({{user.uid}})</p>
<p>Last activity: {{ user.last_activity }} &mdash; Last login: {{ user.last_login }}</p>
</div>
{% endmacro %}