From 9049e457a40bcf92400ffeae42a315e314c6379d Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Sun, 5 Apr 2020 13:22:53 -0700 Subject: [PATCH] Improve validation --- fishbowl/app.py | 20 +++++++++++++++----- fishbowl/fishbowl.db | Bin 16384 -> 0 bytes fishbowl/templates/game.html | 10 +++++++--- 3 files changed, 22 insertions(+), 8 deletions(-) delete mode 100644 fishbowl/fishbowl.db diff --git a/fishbowl/app.py b/fishbowl/app.py index d7ec7fe..0e2be74 100644 --- a/fishbowl/app.py +++ b/fishbowl/app.py @@ -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, diff --git a/fishbowl/fishbowl.db b/fishbowl/fishbowl.db deleted file mode 100644 index 7f343893c9531ca3868ccc0a9a445068d47c3e63..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI(J#P~+7yw}3<%1+N7=$XsfUpEo6A93L<#HzmNW;;FrYSuVzLv;7pW#Hm;I4ud z#1BA-m4Cp_zz;x7Y)D9q`~oI6u+yY15+xH1=(XhJ-q)|~f#x6&uQLAQI3RLWWWBqKLlVSBL^)=a~4>MDDWuE)c@87h^e!}|&6@dlY+S?LC? z-Z;6HL=R%VKY2auEVsftWGT2qY6%}Lcy%QVItx9OtBn`0lQ6g%gh8(z+#o8qTijj@ z+DkIp-LcanHRBvH>!Y>yhR1vF4Oi+#DB*VzRT_fhODA@~uxyY^(kY z*cMIGk?Ewsw7k*FqI^Bs7#=wMJC#ANvpNV!O>J1O)aUf#iHoxuN;Y`>Bz?Fh@9l?l zJKG$^L-nGErh3fDuUbic-Kw$|j>voS5BL>k*p`>jKmY_l00ck)1V8`;KmY_l00cnb ze-p3@1++YegkjL8&3z`Uh9g|+F)!ib9`p_uIbs#6$UdKkrPwO z1?8!rDHpb`X4IXA8#NmyvurDJIBoi#8QaWn#-_!rf9H690|`C zuERaYbE(ZeS8iofVMa~0jjR`ZJg*`@(E?sXcpZPiZ}Cfc2@M2500ck)1V8`;KmY_l z00ck)1VG>bfwQ?~ZQ3b9K^09qHpr`@X@>AlTP{03|@C$^$;jj1; zevjYbH~6(oKm!2~009sH0T2KI5C8!X009sH0TB3W0*6aQG^>^5pDNNUUp-VrC&vMy z`D$?|yqRpS#hG3y6wz@x+MyeIHNP`-El$%U)@O1uESJiCxeAG9^nDSV(e_33CwqM^ AiU0rr diff --git a/fishbowl/templates/game.html b/fishbowl/templates/game.html index c916244..6f14694 100644 --- a/fishbowl/templates/game.html +++ b/fishbowl/templates/game.html @@ -6,12 +6,16 @@ {% if game.password is not none and not session['logged_in'][game.uuid] %} {{ render_form(game_login) }} {% else %} -

To share this game, just share the url to this page

+

To share this game, just share the url to this page

{% if word is not none %} -
Your word is: {{ word.text }}
+

Your word is: {{ word.text }}

{% endif %} -
Words remaining: {{ words|length }}
+

Words remaining: {{ words|length }}

{{ render_form(play_form) }} +

+

Put words back

+

Make all drawn words pickable again

+ {{ render_form(reset_form) }}

Add new word

{{ render_form(word_form) }} {% endif %}