From c09851333e7aee4044c394253c18a1e3fb7d99af Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 27 Apr 2020 22:04:13 -0700 Subject: [PATCH] Refactor lexicon.join handler --- amanuensis/server/lexicon/__init__.py | 30 +++++++++++++++------------ 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/amanuensis/server/lexicon/__init__.py b/amanuensis/server/lexicon/__init__.py index 7b19ef7..32d5b31 100644 --- a/amanuensis/server/lexicon/__init__.py +++ b/amanuensis/server/lexicon/__init__.py @@ -32,20 +32,24 @@ def join(name): form = LexiconJoinForm() - if form.validate_on_submit(): - # Gate on password if one is required - if (g.lexicon.cfg.join.password - and form.password.data != g.lexicon.cfg.join.password): - return redirect(url_for("lexicon.join", name=name)) - # Gate on join validity - if player_can_join_lexicon(current_user, g.lexicon, form.password.data): - add_player_to_lexicon(current_user, g.lexicon) - return redirect(url_for('session.session', name=name)) - else: - flash("Could not join game") - return redirect(url_for("home.home", name=name)) + if not form.validate_on_submit(): + # GET or POST with invalid form data + return render_template('lexicon.join.jinja', form=form) - return render_template('lexicon.join.jinja', form=form) + # POST with valid data + # If the game is passworded, check password + if (g.lexicon.cfg.join.password + and form.password.data != g.lexicon.cfg.join.password): + # Bad creds, try again + flash('Incorrect password') + return redirect(url_for('lexicon.join', name=name)) + # If the password was correct, check if the user can join + if player_can_join_lexicon(current_user, g.lexicon, form.password.data): + add_player_to_lexicon(current_user, g.lexicon) + return redirect(url_for('session.session', name=name)) + else: + flash('Could not join game') + return redirect(url_for('home.home', name=name)) @bp_lexicon.route('/contents/', methods=['GET'])