diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index 12c7b1e..d9ed0f4 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -1,3 +1,4 @@ +from functools import wraps import json from flask import Blueprint, render_template, url_for, redirect, g, flash @@ -11,6 +12,17 @@ import user import lexicon +def lexicon_param(route): + @wraps(route) + def with_lexicon(name): + g.lexicon = lexicon.LexiconModel.by(name=name) + if g.lexicon is None: + flash("Couldn't find a lexicon with the name '{}'".format(name)) + return redirect(url_for("home.home")) + return route(name) + return with_lexicon + + class LexiconConfigForm(FlaskForm): configText = TextAreaField("Config file") submit = SubmitField("Submit") @@ -22,28 +34,28 @@ def get_bp(): @bp.route('/contents/', methods=['GET']) @login_required + @lexicon_param def contents(name): - lex = lexicon.LexiconModel.by(name=name) - return render_template('lexicon/contents.html', lexicon=lex) + return render_template('lexicon/contents.html') @bp.route('/rules/', methods=['GET']) @login_required + @lexicon_param def rules(name): - lex = lexicon.LexiconModel.by(name=name) - return render_template('lexicon/rules.html', lexicon=lex) + return render_template('lexicon/rules.html') @bp.route('/session/', methods=['GET']) @login_required + @lexicon_param def session(name): - lex = lexicon.LexiconModel.by(name=name) - return render_template('lexicon/session.html', lexicon=lex) + return render_template('lexicon/session.html') @bp.route('/session/edit/', methods=['GET', 'POST']) @login_required + @lexicon_param def session_edit(name): # Restrict to editor - lex = lexicon.LexiconModel.by(name=name) - if not current_user.id == lex.editor: + if not current_user.id == g.lexicon.editor: flash("Access is forbidden") return redirect(url_for('lexicon.session', name=name)) @@ -51,9 +63,9 @@ def get_bp(): # Load the config for the lexicon on load if not form.is_submitted(): - with config.json_ro(lex.config_path) as cfg: + with config.json_ro(g.lexicon.config_path) as cfg: form.configText.data = json.dumps(cfg, indent=2) - return render_template("lexicon/session_edit.html", lexicon=lex, form=form) + return render_template("lexicon/session_edit.html", form=form) if form.validate(): # Check input is valid json @@ -61,20 +73,22 @@ def get_bp(): cfg = json.loads(form.configText.data, object_pairs_hook=ReadOnlyOrderedDict) except: flash("Invalid JSON") - return render_template("lexicon/session_edit.html", lexicon=lex, form=form) + return render_template("lexicon/session_edit.html", form=form) # Check input has all the required fields # TODO # Write the new config - with config.open_ex(lex.config_path, mode='w') as f: + form.submit.submitted = False + with config.open_ex(g.lexicon.config_path, mode='w') as f: json.dump(cfg, f, indent='\t') flash("Config updated") + return redirect(url_for('lexicon.session_edit', name=name)) - return render_template("lexicon/session_edit.html", lexicon=lex, form=form) + return render_template("lexicon/session_edit.html", form=form) @bp.route('/statistics/', methods=['GET']) @login_required + @lexicon_param def stats(name): - lex = lexicon.LexiconModel.by(name=name) - return render_template('lexicon/statistics.html', lexicon=lex) + return render_template('lexicon/statistics.html') return bp diff --git a/amanuensis/templates/lexicon/contents.html b/amanuensis/templates/lexicon/contents.html index 14e64fe..6fda470 100644 --- a/amanuensis/templates/lexicon/contents.html +++ b/amanuensis/templates/lexicon/contents.html @@ -1,9 +1,9 @@ {% extends "lexicon/lexicon.html" %} -{% block title %}Session | {{ lexicon_title }}{% endblock %} +{% block title %}Index | {{ lexicon_title }}{% endblock %} {% block main %} -

Contents

+

Index

{% set lexicons = current_user.lexicons_in() %} diff --git a/amanuensis/templates/lexicon/lexicon.html b/amanuensis/templates/lexicon/lexicon.html index af26a7c..32fbe91 100644 --- a/amanuensis/templates/lexicon/lexicon.html +++ b/amanuensis/templates/lexicon/lexicon.html @@ -1,13 +1,13 @@ {% extends "page_2col.html" %} -{% if lexicon.title %} -{% set lexicon_title = lexicon.title %} +{% if g.lexicon.title %} +{% set lexicon_title = g.lexicon.title %} {% else %} -{% set lexicon_title = "Lexicon " + lexicon.name %} +{% set lexicon_title = "Lexicon " + g.lexicon.name %} {% endif %} {% block header %}

{{ lexicon_title }}

-

{{ lexicon.prompt }}

+

{{ g.lexicon.prompt }}

{% endblock %} {% block sb_logo %}{% endblock %} diff --git a/amanuensis/templates/lexicon/session.html b/amanuensis/templates/lexicon/session.html index e60ed53..8a33b16 100644 --- a/amanuensis/templates/lexicon/session.html +++ b/amanuensis/templates/lexicon/session.html @@ -6,7 +6,7 @@ {% endblock %} {% set template_content_blocks = [self.main()] %} -{% if current_user.id == lexicon.editor %} +{% if current_user.id == g.lexicon.editor %} {% block editor_edit %} Edit lexicon settings {% endblock %}