Improve validation

This commit is contained in:
IamTheFij 2020-04-05 13:22:53 -07:00
parent 9f1f6d0970
commit 9049e457a4
3 changed files with 22 additions and 8 deletions

View File

@ -13,7 +13,7 @@ from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, BooleanField, PasswordField
from wtforms.validators import DataRequired, Length, Optional
from wtforms.validators import DataRequired, Length, Optional, ValidationError
import flask_sqlalchemy
@ -98,8 +98,14 @@ class AddWordForm(FlaskForm):
class PlayGameForm(FlaskForm):
draw_word = SubmitField(label='Draw word')
class ResetGameForm(FlaskForm):
reset_game = SubmitField(label='Reset game')
reset_confirm = BooleanField(label='Confirm reset')
reset_confirm = BooleanField(
label='Confirm reset',
validators=[DataRequired()],
)
@app.route('/', methods=['GET', 'POST'])
@ -126,6 +132,7 @@ def game(game_uuid):
game_login = GameLoginForm()
word_form = AddWordForm()
play_form = PlayGameForm()
reset_form = ResetGameForm()
try:
game = Game.by_uuid(game_uuid)
except flask_sqlalchemy.orm.exc.NoResultFound:
@ -145,16 +152,18 @@ def game(game_uuid):
# We just drew a new word!
if play_form.draw_word.data:
words = game.get_remaining_words()
if words:
if words and len(words) > 0:
word = choice(words)
word.is_picked = True
else:
word = "There are no words. Add some and try again!"
word = None
# Reset the game
if play_form.reset_game.data and play_form.reset_confirm.data:
if reset_form.reset_game.data and reset_form.validate():
for unpick_word in game.words:
unpick_word.is_picked = False
reset_form.reset_confirm.data = False
db.session.add(game)
db.session.commit()
@ -165,6 +174,7 @@ def game(game_uuid):
game_login=game_login,
word_form=word_form,
play_form=play_form,
reset_form=reset_form,
game=game,
word=word,
words=words,

Binary file not shown.

View File

@ -6,12 +6,16 @@
{% if game.password is not none and not session['logged_in'][game.uuid] %}
{{ render_form(game_login) }}
{% else %}
<h3>To share this game, just share the url to this page</h3>
<p><em>To share this game, just share the url to this page</em></p>
{% if word is not none %}
<div><span>Your word is: {{ word.text }}</span></div>
<p>Your word is: {{ word.text }}</p>
{% endif %}
<div><span>Words remaining: {{ words|length }}</span></div>
<p>Words remaining: {{ words|length }}</p>
{{ render_form(play_form) }}
<p></p>
<h4>Put words back</h4>
<p>Make all drawn words pickable again</p>
{{ render_form(reset_form) }}
<h1>Add new word</h1>
{{ render_form(word_form) }}
{% endif %}