From 6f326f91bd61b0874b39bc73bf6d732e3655bccb Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 26 Jan 2020 23:15:43 -0800 Subject: [PATCH] Add lexicon create page shell --- amanuensis/server/forms.py | 49 +++++++++++++++++++++++++++ amanuensis/server/home.py | 16 ++++++++- amanuensis/templates/home/admin.html | 3 +- amanuensis/templates/home/create.html | 31 +++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 amanuensis/server/forms.py create mode 100644 amanuensis/templates/home/create.html diff --git a/amanuensis/server/forms.py b/amanuensis/server/forms.py new file mode 100644 index 0000000..30239d7 --- /dev/null +++ b/amanuensis/server/forms.py @@ -0,0 +1,49 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField +from wtforms.validators import DataRequired, ValidationError + +from amanuensis.config import json_ro + + +# Custom validators +def user(exists=True): + template = 'User "{{}}" {}'.format("not found" if exists else "already exists") + should_exist = bool(exists) + def validate_user(form, field): + with json_ro('user', 'index.json') as index: + if (field.data in index.keys()) != should_exist: + raise ValidationError(template.format(field.data)) + return validate_user + + +def lexicon(exists=True): + template = 'Lexicon "{{}}" {}'.format("not found" if exists else "already exists") + should_exist = bool(exists) + def validate_lexicon(form, field): + with json_ro('lexicon', 'index.json') as index: + if (field.data in index.keys()) != should_exist: + raise ValidationError(template.format(field.data)) + return validate_lexicon + + +# Forms +class LoginForm(FlaskForm): + """/auth/login/""" + username = StringField('Username', validators=[DataRequired()]) + password = PasswordField('Password', validators=[DataRequired()]) + remember = BooleanField('Stay logged in') + submit = SubmitField('Log in') + + +class LexiconCreateForm(FlaskForm): + """/admin/create/""" + lexiconName = StringField('Lexicon name', validators=[DataRequired(), lexicon(exists=False)]) + editorName = StringField('Username of editor', validators=[DataRequired(), user(exists=True)]) + promptText = TextAreaField("Prompt") + submit = SubmitField('Create') + + +class LexiconConfigForm(FlaskForm): + """/lexicon//session/settings/""" + configText = TextAreaField("Config file") + submit = SubmitField("Submit") \ No newline at end of file diff --git a/amanuensis/server/home.py b/amanuensis/server/home.py index 116add3..bb4ef59 100644 --- a/amanuensis/server/home.py +++ b/amanuensis/server/home.py @@ -8,6 +8,7 @@ from wtforms import TextAreaField, SubmitField, StringField from amanuensis.config import json_ro from amanuensis.lexicon import LexiconModel +from amanuensis.server.forms import LexiconCreateForm from amanuensis.server.helpers import admin_required from amanuensis.user import UserModel @@ -20,7 +21,7 @@ def get_bp(): def home(): return render_template('home/home.html') - @bp.route('/admin/', methods=['GET', 'POST']) + @bp.route('/admin/', methods=['GET']) @admin_required def admin(): users = [] @@ -35,4 +36,17 @@ def get_bp(): return render_template('home/admin.html', users=users, lexicons=lexicons) + @bp.route("/admin/create/", methods=['GET', 'POST']) + @admin_required + def admin_create(): + form = LexiconCreateForm() + + if form.validate_on_submit(): + lexicon_name = form.lexiconName.data + editor_name = form.editorName.data + prompt = form.promptText.data + return "
".join([lexicon_name, editor_name, prompt]) + + return render_template('home/create.html', form=form) + return bp diff --git a/amanuensis/templates/home/admin.html b/amanuensis/templates/home/admin.html index 4df6049..c564161 100644 --- a/amanuensis/templates/home/admin.html +++ b/amanuensis/templates/home/admin.html @@ -4,7 +4,8 @@ {% block header %}

Amanuensis - Admin Dashboard

{% endblock %} {% block sb_home %}Home{% endblock %} -{% set template_sidebar_rows = [self.sb_home()] %} +{% block sb_create %}Create a lexicon{% endblock %} +{% set template_sidebar_rows = [self.sb_home(), self.sb_create()] %} {% block main %}

Users:

diff --git a/amanuensis/templates/home/create.html b/amanuensis/templates/home/create.html new file mode 100644 index 0000000..19884ae --- /dev/null +++ b/amanuensis/templates/home/create.html @@ -0,0 +1,31 @@ +{% extends "page_2col.html" %} +{% import 'macros.html' as macros %} +{% block title %}Admin | Amanuensis{% endblock %} +{% block header %}

Amanuensis - Create a Lexicon

{% endblock %} + +{% block sb_home %}Home{% endblock %} +{% block sb_admin %}Admin{% endblock %} +{% set template_sidebar_rows = [self.sb_home(), self.sb_admin()] %} + +{% block main %} +
+ {{ form.hidden_tag() }} +

{{ form.lexiconName.label }}
{{ form.lexiconName(size=32) }} + {% for error in form.lexiconName.errors %} +
{{ error }} + {% endfor %}

+ +

{{ form.editorName.label }}
{{ form.editorName(size=32) }} + {% for error in form.editorName.errors %} +
{{ error }} + {% endfor %}

+ +

{{ form.promptText.label }}
{{ form.promptText(class_="fullwidth") }}

+ +

{{ form.submit() }}

+
+{% for message in get_flashed_messages() %} +{{ message }}
+{% endfor %} +{% endblock %} +{% set template_content_blocks = [self.main()] %} \ No newline at end of file