Add lexicon create page shell

This commit is contained in:
Tim Van Baak 2020-01-26 23:15:43 -08:00
parent aaac4b6568
commit 6f326f91bd
4 changed files with 97 additions and 2 deletions

View File

@ -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/<name>/session/settings/"""
configText = TextAreaField("Config file")
submit = SubmitField("Submit")

View File

@ -8,6 +8,7 @@ from wtforms import TextAreaField, SubmitField, StringField
from amanuensis.config import json_ro from amanuensis.config import json_ro
from amanuensis.lexicon import LexiconModel from amanuensis.lexicon import LexiconModel
from amanuensis.server.forms import LexiconCreateForm
from amanuensis.server.helpers import admin_required from amanuensis.server.helpers import admin_required
from amanuensis.user import UserModel from amanuensis.user import UserModel
@ -20,7 +21,7 @@ def get_bp():
def home(): def home():
return render_template('home/home.html') return render_template('home/home.html')
@bp.route('/admin/', methods=['GET', 'POST']) @bp.route('/admin/', methods=['GET'])
@admin_required @admin_required
def admin(): def admin():
users = [] users = []
@ -35,4 +36,17 @@ def get_bp():
return render_template('home/admin.html', users=users, lexicons=lexicons) 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 "<br>".join([lexicon_name, editor_name, prompt])
return render_template('home/create.html', form=form)
return bp return bp

View File

@ -4,7 +4,8 @@
{% block header %}<h2>Amanuensis - Admin Dashboard</h2>{% endblock %} {% block header %}<h2>Amanuensis - Admin Dashboard</h2>{% endblock %}
{% block sb_home %}<a href="{{ url_for('home.home') }}">Home</a>{% endblock %} {% block sb_home %}<a href="{{ url_for('home.home') }}">Home</a>{% endblock %}
{% set template_sidebar_rows = [self.sb_home()] %} {% block sb_create %}<a href="{{ url_for('home.admin_create') }}">Create a lexicon</a>{% endblock %}
{% set template_sidebar_rows = [self.sb_home(), self.sb_create()] %}
{% block main %} {% block main %}
<p>Users:</p> <p>Users:</p>

View File

@ -0,0 +1,31 @@
{% extends "page_2col.html" %}
{% import 'macros.html' as macros %}
{% block title %}Admin | Amanuensis{% endblock %}
{% block header %}<h2>Amanuensis - Create a Lexicon</h2>{% endblock %}
{% block sb_home %}<a href="{{ url_for('home.home') }}">Home</a>{% endblock %}
{% block sb_admin %}<a href="{{ url_for('home.admin') }}">Admin</a>{% endblock %}
{% set template_sidebar_rows = [self.sb_home(), self.sb_admin()] %}
{% block main %}
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>{{ form.lexiconName.label }}<br>{{ form.lexiconName(size=32) }}
{% for error in form.lexiconName.errors %}
<br><span style="color: #ff0000">{{ error }}</span>
{% endfor %}</p>
<p>{{ form.editorName.label }}<br>{{ form.editorName(size=32) }}
{% for error in form.editorName.errors %}
<br><span style="color: #ff0000">{{ error }}</span>
{% endfor %}</p>
<p>{{ form.promptText.label }}<br>{{ form.promptText(class_="fullwidth") }}</p>
<p>{{ form.submit() }}</p>
</form>
{% for message in get_flashed_messages() %}
<span style="color: #ff0000">{{ message }}</span><br>
{% endfor %}
{% endblock %}
{% set template_content_blocks = [self.main()] %}