From 81bd506e1cb78788fe00511fbdfa22407a8dc635 Mon Sep 17 00:00:00 2001 From: Burathar Date: Wed, 22 Jul 2020 18:31:14 +0200 Subject: [PATCH] fix change player role, dynamicly load addplayer roles --- app/main/forms.py | 4 ++-- app/main/routes.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index 84b60c1..4bb5392 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -51,12 +51,12 @@ class PlayerUpdateForm(FlaskForm): class PlayerAddForm(FlaskForm): name = StringField('Username', validators=[InputRequired(), Length(min=0, max=64)]) - role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[InputRequired()]) + role = SelectField('Player Role', coerce=int, validators=[InputRequired()]) submit_add = SubmitField('Create') class UserCreateForm(FlaskForm): name = StringField('Username', validators=[InputRequired(), Length(min=0, max=64)]) - role = SelectField('Player Role', choices=[('none', 'none'), ('owner', 'owner'), ('hunter', 'hunter'), ('bunny', 'bunny')], validators=[InputRequired()]) + role = SelectField('Player Role', coerce=int, validators=[InputRequired()]) submit_create = SubmitField('Create') class CatchBunnyForm(FlaskForm): diff --git a/app/main/routes.py b/app/main/routes.py index e2ff6f1..a4a4478 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -178,19 +178,23 @@ def add_player(game_name): game = Game.query.filter_by(name=game_name).first_or_404() if not game.owned_by(current_user): abort(403) + + form_add = PlayerAddForm() + form_add.role.choices = [(role.value, role.name) for role in Role] form_create = UserCreateForm() + form_create.role.choices = [(role.value, role.name) for role in Role] if form_add.submit_add.data and form_add.validate_on_submit(): user = User.query.filter_by(name=form_add.name.data).first_or_404() - game.players.append(GamePlayer(user=user, role=Role[form_create.role.data])) + game.players.append(GamePlayer(user=user, role=Role(form_add.role.data))) db.session.commit() return redirect(url_for('main.game_dashboard', game_name=game.name)) if form_create.submit_create.data and form_create.validate_on_submit(): user = User(name=form_create.name.data) user.set_auth_hash() - game.players.append(GamePlayer(user=user, role=Role[form_create.role.data])) + game.players.append(GamePlayer(user=user, role=Role(form_create.role.data))) db.session.commit() return redirect(url_for('main.game_dashboard', game_name=game.name)) @@ -224,11 +228,13 @@ def game_player(game_name, username): # pylint: disable=no-member form = PlayerUpdateForm(role=player.role.name) form.role.choices = [(role.value, role.name) for role in Role] - form.role.default = player.role.value - form.process() + if request.method == 'GET': + form.role.default = player.role.value + form.process() if form.validate_on_submit(): - player.role = Role[form.role.data] + player.role = Role(form.role.data) + print(form.role.data) db.session.commit() return redirect(url_for('main.game_dashboard', game_name=game.name)) return render_template('game_player.html', title=f'{user.name} in {game_name}', player=player, form=form, json=json, location_encoder=LocationEncoder)