Handle lexicon loading in a wrapper

This commit is contained in:
Tim Van Baak 2020-01-20 22:21:25 -08:00
parent 5e25c433ee
commit 563865f7ea
4 changed files with 36 additions and 22 deletions

View File

@ -1,3 +1,4 @@
from functools import wraps
import json import json
from flask import Blueprint, render_template, url_for, redirect, g, flash from flask import Blueprint, render_template, url_for, redirect, g, flash
@ -11,6 +12,17 @@ import user
import lexicon 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): class LexiconConfigForm(FlaskForm):
configText = TextAreaField("Config file") configText = TextAreaField("Config file")
submit = SubmitField("Submit") submit = SubmitField("Submit")
@ -22,28 +34,28 @@ def get_bp():
@bp.route('/contents/', methods=['GET']) @bp.route('/contents/', methods=['GET'])
@login_required @login_required
@lexicon_param
def contents(name): def contents(name):
lex = lexicon.LexiconModel.by(name=name) return render_template('lexicon/contents.html')
return render_template('lexicon/contents.html', lexicon=lex)
@bp.route('/rules/', methods=['GET']) @bp.route('/rules/', methods=['GET'])
@login_required @login_required
@lexicon_param
def rules(name): def rules(name):
lex = lexicon.LexiconModel.by(name=name) return render_template('lexicon/rules.html')
return render_template('lexicon/rules.html', lexicon=lex)
@bp.route('/session/', methods=['GET']) @bp.route('/session/', methods=['GET'])
@login_required @login_required
@lexicon_param
def session(name): def session(name):
lex = lexicon.LexiconModel.by(name=name) return render_template('lexicon/session.html')
return render_template('lexicon/session.html', lexicon=lex)
@bp.route('/session/edit/', methods=['GET', 'POST']) @bp.route('/session/edit/', methods=['GET', 'POST'])
@login_required @login_required
@lexicon_param
def session_edit(name): def session_edit(name):
# Restrict to editor # Restrict to editor
lex = lexicon.LexiconModel.by(name=name) if not current_user.id == g.lexicon.editor:
if not current_user.id == lex.editor:
flash("Access is forbidden") flash("Access is forbidden")
return redirect(url_for('lexicon.session', name=name)) return redirect(url_for('lexicon.session', name=name))
@ -51,9 +63,9 @@ def get_bp():
# Load the config for the lexicon on load # Load the config for the lexicon on load
if not form.is_submitted(): 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) 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(): if form.validate():
# Check input is valid json # Check input is valid json
@ -61,20 +73,22 @@ def get_bp():
cfg = json.loads(form.configText.data, object_pairs_hook=ReadOnlyOrderedDict) cfg = json.loads(form.configText.data, object_pairs_hook=ReadOnlyOrderedDict)
except: except:
flash("Invalid JSON") 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 # Check input has all the required fields
# TODO # TODO
# Write the new config # 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') json.dump(cfg, f, indent='\t')
flash("Config updated") 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']) @bp.route('/statistics/', methods=['GET'])
@login_required @login_required
@lexicon_param
def stats(name): def stats(name):
lex = lexicon.LexiconModel.by(name=name) return render_template('lexicon/statistics.html')
return render_template('lexicon/statistics.html', lexicon=lex)
return bp return bp

View File

@ -1,9 +1,9 @@
{% extends "lexicon/lexicon.html" %} {% extends "lexicon/lexicon.html" %}
{% block title %}Session | {{ lexicon_title }}{% endblock %} {% block title %}Index | {{ lexicon_title }}{% endblock %}
{% block main %} {% block main %}
<h1>Contents</h1> <h1>Index</h1>
{% set lexicons = current_user.lexicons_in() %} {% set lexicons = current_user.lexicons_in() %}

View File

@ -1,13 +1,13 @@
{% extends "page_2col.html" %} {% extends "page_2col.html" %}
{% if lexicon.title %} {% if g.lexicon.title %}
{% set lexicon_title = lexicon.title %} {% set lexicon_title = g.lexicon.title %}
{% else %} {% else %}
{% set lexicon_title = "Lexicon " + lexicon.name %} {% set lexicon_title = "Lexicon " + g.lexicon.name %}
{% endif %} {% endif %}
{% block header %} {% block header %}
<h2>{{ lexicon_title }}</h2> <h2>{{ lexicon_title }}</h2>
<p><i>{{ lexicon.prompt }}</i></p> <p><i>{{ g.lexicon.prompt }}</i></p>
{% endblock %} {% endblock %}
{% block sb_logo %}{% endblock %} {% block sb_logo %}{% endblock %}

View File

@ -6,7 +6,7 @@
{% endblock %} {% endblock %}
{% set template_content_blocks = [self.main()] %} {% set template_content_blocks = [self.main()] %}
{% if current_user.id == lexicon.editor %} {% if current_user.id == g.lexicon.editor %}
{% block editor_edit %} {% block editor_edit %}
<a href="{{ url_for('lexicon.session_edit', name=lexicon.name) }}" style="display:block; text-align:center;">Edit lexicon settings</a> <a href="{{ url_for('lexicon.session_edit', name=lexicon.name) }}" style="display:block; text-align:center;">Edit lexicon settings</a>
{% endblock %} {% endblock %}