From 703af9b4e3918242bd279be59535cc75906ab1ad Mon Sep 17 00:00:00 2001 From: Burathar Date: Sun, 19 Jul 2020 02:32:22 +0200 Subject: [PATCH] Resolve issue #17 and implement deletegame --- app/main/routes.py | 22 ++++++++++++++++++++-- app/models/game.py | 6 ++++-- app/models/game_player.py | 2 +- app/models/user.py | 3 ++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/main/routes.py b/app/main/routes.py index d27ddae..b697d9f 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -32,13 +32,27 @@ def create_game(): form = CreateGameForm() if form.validate_on_submit(): game = Game(name=form.game_name.data, start_time=form.start_time.data, end_time=form.end_time.data) - game.game_players.append(GamePlayer(game_player=current_user, role=Role['owner'])) + game.game_players.append(GamePlayer(user=current_user, role=Role['owner'])) db.session.add(game) db.session.commit() flash(f"'{game.name}' had been created!") return redirect(url_for('main.game_dashboard', game_name=game.name)) return render_template('create_game.html', title='Create Game', form=form) +@bp.route('/game//delete') +@login_required +def delete_game(game_name): + game = Game.query.filter_by(name=game_name).first_or_404() + if not game.owned_by(current_user): + abort(403) + db.session.delete(game) + for user in game.players: + if not user.last_login: + db.session.delete(user) + db.session.commit() + flash(f"Game '{game.name}' has been deleted!") + return redirect(url_for('main.index')) + @bp.route('/game//dashboard') @login_required def game_dashboard(game_name): @@ -70,8 +84,9 @@ def add_player(game_name): form_create = UserCreateForm() if form_add.submit_add.data and form_add.validate_on_submit(): - user = User.query.filter_by(form_add.name.data).first_or_404() + user = User.query.filter_by(name=form_add.name.data).first_or_404() game.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)) if form_create.submit_create.data and form_create.validate_on_submit(): @@ -92,9 +107,12 @@ def remove_player(game_name, username): abort(403) user = User.query.filter(and_(User.name == username, User.games.contains(game))).first_or_404() game.players.remove(user) + if not user.last_login: + db.session.delete(user) db.session.commit() return redirect(url_for('main.game_dashboard', game_name=game.name)) +@bp.route('/game//user/', methods=['GET', 'POST']) @bp.route('/game//player/', methods=['GET', 'POST']) @login_required def game_player(game_name, username): diff --git a/app/models/game.py b/app/models/game.py index 21d9d9b..661617a 100644 --- a/app/models/game.py +++ b/app/models/game.py @@ -22,11 +22,13 @@ class Game(db.Model): objectives = db.relationship( 'Objective', lazy='select', - backref=db.backref('game', lazy='joined')) + backref=db.backref('game', lazy='joined'), + cascade="save-update, merge, delete, delete-orphan") notifications = db.relationship( 'Notification', lazy='select', - backref=db.backref('game', lazy='joined')) + backref=db.backref('game', lazy='joined'), + cascade="save-update, merge, delete, delete-orphan") def last_player_locations(self): return [user.last_location(self) for user in self.players if user.locations] diff --git a/app/models/game_player.py b/app/models/game_player.py index 7987e86..0850c34 100644 --- a/app/models/game_player.py +++ b/app/models/game_player.py @@ -43,7 +43,7 @@ class GamePlayer(db.Model): player_caught_by_players = db.relationship( 'PlayerCaughtPlayer', back_populates='caught_player', - cascade='save-update, merge, delete, delete-orphan', + cascade='save-update, merge, delete, delete-orphan',# zorgt dit voor cirkel? foreign_keys=[PlayerCaughtPlayer.caught_player_id]) caught_by_players = association_proxy('player_caught_by_players', 'game_player', creator=lambda game_player: PlayerCaughtPlayer(catching_player=game_player)) diff --git a/app/models/user.py b/app/models/user.py index 47350f9..a30a324 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -28,7 +28,8 @@ class User(UserMixin, db.Model): locations = db.relationship( 'Location', lazy='select', - backref=db.backref('player', lazy='joined')) + backref=db.backref('player', lazy='joined'), + cascade="save-update, merge, delete, delete-orphan") def set_password(self, password): self.password_hash = generate_password_hash(password)