Handle lexicon loading in a wrapper
This commit is contained in:
parent
5e25c433ee
commit
563865f7ea
@ -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
|
||||
|
@ -1,9 +1,9 @@
|
||||
{% extends "lexicon/lexicon.html" %}
|
||||
{% block title %}Session | {{ lexicon_title }}{% endblock %}
|
||||
{% block title %}Index | {{ lexicon_title }}{% endblock %}
|
||||
|
||||
{% block main %}
|
||||
|
||||
<h1>Contents</h1>
|
||||
<h1>Index</h1>
|
||||
|
||||
{% set lexicons = current_user.lexicons_in() %}
|
||||
|
||||
|
@ -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 %}
|
||||
<h2>{{ lexicon_title }}</h2>
|
||||
<p><i>{{ lexicon.prompt }}</i></p>
|
||||
<p><i>{{ g.lexicon.prompt }}</i></p>
|
||||
{% endblock %}
|
||||
|
||||
{% block sb_logo %}{% endblock %}
|
||||
|
@ -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 %}
|
||||
<a href="{{ url_for('lexicon.session_edit', name=lexicon.name) }}" style="display:block; text-align:center;">Edit lexicon settings</a>
|
||||
{% endblock %}
|
||||
|
Loading…
Reference in New Issue
Block a user