Add /session/edit/

This commit is contained in:
Tim Van Baak 2020-01-20 13:14:00 -08:00
parent 79016265d9
commit c982364041
4 changed files with 66 additions and 9 deletions

View File

@ -1,7 +1,7 @@
from functools import wraps
import json
from flask import Blueprint, render_template, url_for, redirect
from flask import Blueprint, render_template, url_for, redirect, flash
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
from wtforms import TextAreaField, SubmitField, StringField
@ -20,6 +20,7 @@ def admin_required(route):
@wraps(route)
def admin_route(*args, **kwargs):
if not current_user.is_admin:
flash("Access is forbidden")
return redirect(url_for('home.home'))
return route(*args, **kwargs)
return admin_route

View File

@ -1,15 +1,21 @@
import json
from flask import Blueprint, render_template, url_for, redirect
from flask import Blueprint, render_template, url_for, redirect, g, flash
from flask_login import login_required, current_user
# from flask_wtf import FlaskForm
# from wtforms import TextAreaField, SubmitField, StringField
from flask_wtf import FlaskForm
from wtforms import TextAreaField, SubmitField
import config
from config.loader import ReadOnlyOrderedDict
import user
import lexicon
class LexiconConfigForm(FlaskForm):
configText = TextAreaField("Config file")
submit = SubmitField("Submit")
def get_bp():
"""Create a blueprint for lexicon pages"""
bp = Blueprint('lexicon', __name__, url_prefix='/lexicon/<name>')
@ -32,6 +38,39 @@ def get_bp():
lex = lexicon.LexiconModel.by(name=name)
return render_template('lexicon/session.html', lexicon=lex)
@bp.route('/session/edit/', methods=['GET', 'POST'])
@login_required
def session_edit(name):
# Restrict to editor
lex = lexicon.LexiconModel.by(name=name)
if not current_user.id == lex.editor:
flash("Access is forbidden")
return redirect(url_for('lexicon.session', name=name))
form = LexiconConfigForm()
# Load the config for the lexicon on load
if not form.is_submitted():
with config.json_ro(lex.config_path) as cfg:
form.configText.data = json.dumps(cfg, indent=2)
return render_template("lexicon/session_edit.html", lexicon=lex, form=form)
if form.validate():
# Check input is valid json
try:
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)
# Check input has all the required fields
# TODO
# Write the new config
with config.open_ex(lex.config_path, mode='w') as f:
json.dump(cfg, f, indent='\t')
flash("Config updated")
return render_template("lexicon/session_edit.html", lexicon=lex, form=form)
@bp.route('/statistics/', methods=['GET'])
@login_required
def stats(name):

View File

@ -2,10 +2,13 @@
{% block title %}Session | {{ lexicon_title }}{% endblock %}
{% block main %}
<h1>Session</h1>
{% set lexicons = current_user.lexicons_in() %}
{% endblock %}
{% set template_content_blocks = [self.main()] %}
{% if current_user.id == 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 %}
{% set template_content_blocks = [self.editor_edit()] + template_content_blocks %}
{% endif %}

View File

@ -0,0 +1,14 @@
{% extends "lexicon/lexicon.html" %}
{% block title %}Edit | {{ lexicon_title }}{% endblock %}
{% block main %}
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>{{ form.configText.label }}<br>{{ form.configText(rows=20) }}</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()] %}