From 98bc78e9a6d1a8984c6d59e145cd36ddbe9f85fd Mon Sep 17 00:00:00 2001 From: Burathar Date: Fri, 10 Jul 2020 23:28:06 +0200 Subject: [PATCH] Add delete player button --- app/models.py | 8 +++++++- app/routes.py | 14 ++++++++++++-- app/templates/game_dashboard.html | 8 ++++++-- app/templates/objective.html | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/models.py b/app/models.py index 7a939e2..d55e301 100644 --- a/app/models.py +++ b/app/models.py @@ -144,7 +144,7 @@ class Player(UserMixin, db.Model): def check_password(self, password): return check_password_hash(self.password_hash, password) - def last_location(self, game = None): + def last_location(self, game=None): if not self.locations: return None if game is None: @@ -154,6 +154,12 @@ class Player(UserMixin, db.Model): return max((location for location in self.locations if location.timestamp > game_start and location.timestamp < game_end), key=lambda location: location.timestamp) + + @staticmethod + def delete_orphans(): + Player.query.filter(~Player.player_games.any()).delete() + db.session.commit() + @login.user_loader def load_user(id): return Player.query.get(int(id)) diff --git a/app/routes.py b/app/routes.py index 1d1c03c..e10e106 100644 --- a/app/routes.py +++ b/app/routes.py @@ -117,9 +117,19 @@ def add_player(game_name): game.game_players.append(GamePlayer(player=player, role=Role[form_create.role.data])) db.session.commit() return redirect(url_for('game_dashboard', game_name=game.name)) - + return render_template('add_player.html', title=f'Add Player for {game_name}', form_add=form_add, form_create=form_create, game=game) +@app.route('/game//removeplayer/') +@login_required +def remove_player(game_name, player_name): + game = Game.query.filter_by(name=game_name).first_or_404() + if not is_game_owner(game): abort(403) + player = Player.query.filter(and_(Player.name == player_name, Player.games.contains(game))).first_or_404() + game.players.remove(player) + db.session.commit() + return redirect(url_for('game_dashboard', game_name=game.name)) + def generate_objective_qr_code(objective): qr = qrcode.QRCode( version=None, @@ -163,7 +173,7 @@ def objective(objective_hash): @app.route('/objective//delete', methods=['GET']) @login_required -def objective_delete(objective_hash): +def delete_objective(objective_hash): objective = Objective.query.filter_by(hash = objective_hash).first_or_404() if not is_objective_owner(objective): abort(403) if is_objective_owner(objective): diff --git a/app/templates/game_dashboard.html b/app/templates/game_dashboard.html index b05ac40..f55fe8f 100644 --- a/app/templates/game_dashboard.html +++ b/app/templates/game_dashboard.html @@ -21,6 +21,7 @@ Bunnies Caught Been Caught Last location + @@ -36,7 +37,10 @@ {% with location = player.last_location(game) %} {% if location %}{{ moment(location.timestamp).fromNow()}}: {% endif %} {{ location }} - {% endwith %} + {% endwith %} + + + {% endfor %} @@ -65,7 +69,7 @@ {{ objective.longitude }} {{ objective.found_by|length }} {{ objective.hash }} - + diff --git a/app/templates/objective.html b/app/templates/objective.html index 3d791f5..139ac7c 100644 --- a/app/templates/objective.html +++ b/app/templates/objective.html @@ -34,7 +34,7 @@ {{ wtf.form_field(form.submit, class='btn btn-primary') }} {% if objective.hash %} - + {% endif %}