Browse Source

Resolve issue #17 and implement deletegame

testing
Burathar 5 years ago
parent
commit
703af9b4e3
  1. 22
      app/main/routes.py
  2. 6
      app/models/game.py
  3. 2
      app/models/game_player.py
  4. 3
      app/models/user.py

22
app/main/routes.py

@ -32,13 +32,27 @@ def create_game():
form = CreateGameForm() form = CreateGameForm()
if form.validate_on_submit(): 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(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.add(game)
db.session.commit() db.session.commit()
flash(f"'{game.name}' had been created!") flash(f"'{game.name}' had been created!")
return redirect(url_for('main.game_dashboard', game_name=game.name)) return redirect(url_for('main.game_dashboard', game_name=game.name))
return render_template('create_game.html', title='Create Game', form=form) return render_template('create_game.html', title='Create Game', form=form)
@bp.route('/game/<game_name>/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/<game_name>/dashboard') @bp.route('/game/<game_name>/dashboard')
@login_required @login_required
def game_dashboard(game_name): def game_dashboard(game_name):
@ -70,8 +84,9 @@ def add_player(game_name):
form_create = UserCreateForm() form_create = UserCreateForm()
if form_add.submit_add.data and form_add.validate_on_submit(): 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])) 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)) return redirect(url_for('main.game_dashboard', game_name=game.name))
if form_create.submit_create.data and form_create.validate_on_submit(): if form_create.submit_create.data and form_create.validate_on_submit():
@ -92,9 +107,12 @@ def remove_player(game_name, username):
abort(403) abort(403)
user = User.query.filter(and_(User.name == username, User.games.contains(game))).first_or_404() user = User.query.filter(and_(User.name == username, User.games.contains(game))).first_or_404()
game.players.remove(user) game.players.remove(user)
if not user.last_login:
db.session.delete(user)
db.session.commit() db.session.commit()
return redirect(url_for('main.game_dashboard', game_name=game.name)) return redirect(url_for('main.game_dashboard', game_name=game.name))
@bp.route('/game/<game_name>/user/<username>', methods=['GET', 'POST'])
@bp.route('/game/<game_name>/player/<username>', methods=['GET', 'POST']) @bp.route('/game/<game_name>/player/<username>', methods=['GET', 'POST'])
@login_required @login_required
def game_player(game_name, username): def game_player(game_name, username):

6
app/models/game.py

@ -22,11 +22,13 @@ class Game(db.Model):
objectives = db.relationship( objectives = db.relationship(
'Objective', 'Objective',
lazy='select', lazy='select',
backref=db.backref('game', lazy='joined')) backref=db.backref('game', lazy='joined'),
cascade="save-update, merge, delete, delete-orphan")
notifications = db.relationship( notifications = db.relationship(
'Notification', 'Notification',
lazy='select', 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): def last_player_locations(self):
return [user.last_location(self) for user in self.players if user.locations] return [user.last_location(self) for user in self.players if user.locations]

2
app/models/game_player.py

@ -43,7 +43,7 @@ class GamePlayer(db.Model):
player_caught_by_players = db.relationship( player_caught_by_players = db.relationship(
'PlayerCaughtPlayer', 'PlayerCaughtPlayer',
back_populates='caught_player', 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]) foreign_keys=[PlayerCaughtPlayer.caught_player_id])
caught_by_players = association_proxy('player_caught_by_players', 'game_player', caught_by_players = association_proxy('player_caught_by_players', 'game_player',
creator=lambda game_player: PlayerCaughtPlayer(catching_player=game_player)) creator=lambda game_player: PlayerCaughtPlayer(catching_player=game_player))

3
app/models/user.py

@ -28,7 +28,8 @@ class User(UserMixin, db.Model):
locations = db.relationship( locations = db.relationship(
'Location', 'Location',
lazy='select', 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): def set_password(self, password):
self.password_hash = generate_password_hash(password) self.password_hash = generate_password_hash(password)

Loading…
Cancel
Save