Fix home.admin_create handler

This commit is contained in:
Tim Van Baak 2020-04-27 21:13:01 -07:00
parent a83a30efdb
commit e311202700
1 changed files with 16 additions and 11 deletions

View File

@ -3,7 +3,7 @@ from flask_login import login_required, current_user
from amanuensis.config import RootConfigDirectoryContext from amanuensis.config import RootConfigDirectoryContext
from amanuensis.lexicon import create_lexicon, load_all_lexicons from amanuensis.lexicon import create_lexicon, load_all_lexicons
from amanuensis.models import UserModel from amanuensis.models import UserModel, ModelFactory
from amanuensis.server.helpers import admin_required from amanuensis.server.helpers import admin_required
from amanuensis.user import load_all_users from amanuensis.user import load_all_users
@ -47,14 +47,19 @@ def admin():
def admin_create(): def admin_create():
form = LexiconCreateForm() form = LexiconCreateForm()
if form.validate_on_submit(): if not form.validate_on_submit():
# GET or POST with invalid form data
return render_template('home.create.jinja', form=form)
# POST with valid data
root: RootConfigDirectoryContext = current_app.config['root']
model_factory: ModelFactory = current_app.config['model_factory']
lexicon_name = form.lexiconName.data lexicon_name = form.lexiconName.data
editor_name = form.editorName.data editor_name = form.editorName.data
prompt = form.promptText.data prompt = form.promptText.data
editor = UserModel.by(name=editor_name) # Editor's existence was checked by form validators
lexicon = create_lexicon(lexicon_name, editor) editor = model_factory.user(editor_name)
lexicon = create_lexicon(root, lexicon_name, editor)
with lexicon.ctx.edit_config() as cfg: with lexicon.ctx.edit_config() as cfg:
cfg.prompt = prompt cfg.prompt = prompt
return redirect(url_for('session.session', name=lexicon_name)) return redirect(url_for('session.session', name=lexicon_name))
return render_template('home.create.jinja', form=form)