diff --git a/amanuensis/server/forms.py b/amanuensis/server/forms.py index f140617..7c2e0f4 100644 --- a/amanuensis/server/forms.py +++ b/amanuensis/server/forms.py @@ -54,3 +54,9 @@ class LexiconConfigForm(FlaskForm): """/lexicon//session/settings/""" configText = TextAreaField("Config file") submit = SubmitField("Submit") + + +class LexiconJoinForm(FlaskForm): + """/lexicon//join/""" + password = StringField('Password') + submit = SubmitField("Submit") diff --git a/amanuensis/server/helpers.py b/amanuensis/server/helpers.py index 3bdbf64..e404a14 100644 --- a/amanuensis/server/helpers.py +++ b/amanuensis/server/helpers.py @@ -41,7 +41,9 @@ def lexicon_param(route): def admin_required(route): - """Requires the user to be an admin to load this page""" + """ + Requires the user to be an admin to load this page + """ @wraps(route) def admin_route(*args, **kwargs): if not current_user.is_admin: @@ -52,11 +54,44 @@ def admin_required(route): def player_required(route): - """Requires the user to be a player in the lexicon to load this page""" + """ + Requires the user to be a player in the lexicon to load this page + """ @wraps(route) def player_route(*args, **kwargs): if current_user.id not in g.lexicon.join.joined: flash("You must be a player to view this page") + return (redirect(url_for('lexicon.contents', name=g.lexicon.name)) + if g.lexicon.join.public + else redirect(url_for('home.home'))) + return route(*args, **kwargs) + return player_route + + +def player_required_if_not_public(route): + """ + Requires the user to be a player in the lexicon to load this page if the + lexicon has join.public = false + """ + @wraps(route) + def player_route(*args, **kwargs): + if ((not g.lexicon.join.public) + and current_user.id not in g.lexicon.join.joined): + flash("You must be a player to view this page") return redirect(url_for('home.home')) return route(*args, **kwargs) return player_route + + +def editor_required(route): + """ + Requires the user to be the editor of the current lexicon to load this + page + """ + @wraps(route) + def editor_route(*args, **kwargs): + if current_user.id != g.lexicon.editor: + flash("You must be the editor to view this page") + return redirect(url_for('lexicon.contents', name=g.lexicon.name)) + return route(*args, **kwargs) + return editor_route \ No newline at end of file diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index a3be654..87f17bf 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -5,35 +5,65 @@ from flask_login import login_required, current_user from amanuensis.config import json_ro, open_ex from amanuensis.config.loader import ReadOnlyOrderedDict -from amanuensis.server.forms import LexiconConfigForm -from amanuensis.server.helpers import lexicon_param +from amanuensis.lexicon.manage import valid_add, add_player +from amanuensis.server.forms import LexiconConfigForm, LexiconJoinForm +from amanuensis.server.helpers import ( + lexicon_param, player_required, editor_required, + player_required_if_not_public) def get_bp(): """Create a blueprint for lexicon pages""" bp = Blueprint('lexicon', __name__, url_prefix='/lexicon/') - @bp.route('/contents/', methods=['GET']) - @login_required + @bp.route("/join/", methods=['GET', 'POST']) @lexicon_param + @login_required + def join(name): + if not g.lexicon.join.open: + flash("This game isn't open for joining") + return redirect(url_for('home.home')) + + form = LexiconJoinForm() + + if form.validate_on_submit(): + # Gate on password if one is required + if (g.lexicon.join.password + and form.password.data != g.lexicon.join.password): + flash("Incorrect password") + print("redirecting") + return redirect(url_for("lexicon.join", name=name)) + # Gate on join validity + if valid_add(g.lexicon, current_user, form.password.data): + add_player(g.lexicon, current_user) + return redirect(url_for("lexicon.session", name=name)) + else: + flash("Could not join game") + return redirect(url_for("lexicon.join", name=name)) + + return render_template('lexicon/join.html', form=form) + + @bp.route('/contents/', methods=['GET']) + @lexicon_param + @player_required_if_not_public def contents(name): return render_template('lexicon/contents.html') @bp.route('/rules/', methods=['GET']) - @login_required @lexicon_param + @player_required_if_not_public def rules(name): return render_template('lexicon/rules.html') @bp.route('/session/', methods=['GET']) - @login_required @lexicon_param + @player_required def session(name): return render_template('lexicon/session.html') @bp.route('/session/settings/', methods=['GET', 'POST']) - @login_required @lexicon_param + @editor_required def settings(name): # Restrict to editor if not current_user.id == g.lexicon.editor: @@ -68,8 +98,8 @@ def get_bp(): return render_template("lexicon/settings.html", form=form) @bp.route('/statistics/', methods=['GET']) - @login_required @lexicon_param + @player_required_if_not_public def stats(name): return render_template('lexicon/statistics.html') diff --git a/amanuensis/templates/lexicon/contents.html b/amanuensis/templates/lexicon/contents.html index d9455e4..9b43b79 100644 --- a/amanuensis/templates/lexicon/contents.html +++ b/amanuensis/templates/lexicon/contents.html @@ -4,6 +4,10 @@ {% block main %} +{% for message in get_flashed_messages() %} +{{ message }}
+{% endfor %} +

Index

{% endblock %} diff --git a/amanuensis/templates/lexicon/join.html b/amanuensis/templates/lexicon/join.html new file mode 100644 index 0000000..c0a9776 --- /dev/null +++ b/amanuensis/templates/lexicon/join.html @@ -0,0 +1,19 @@ +{% extends "lexicon/lexicon.html" %} +{% block title %}Join | {{ lexicon_title }}{% endblock %} + +{% block main %} + +
+ {{ form.hidden_tag() }} + {% if g.lexicon.join.password %} +

{{ form.password.label }}
{{ form.password(size=32) }}

+ {% endif %} +

{{ form.submit() }}

+
+ +{% for message in get_flashed_messages() %} +{{ message }}
+{% endfor %} + +{% endblock %} +{% set template_content_blocks = [self.main()] %} \ No newline at end of file