diff --git a/app/models/game_player.py b/app/models/game_player.py index 0850c34..f54c190 100644 --- a/app/models/game_player.py +++ b/app/models/game_player.py @@ -5,7 +5,6 @@ from app import db from .role import Role from .notification_player import NotificationPlayer from .player_found_objective import PlayerFoundObjective -from .player_caught_player import PlayerCaughtPlayer class GamePlayer(db.Model): __tablename__ = 'game_player' @@ -32,18 +31,5 @@ class GamePlayer(db.Model): found_objectives = association_proxy('player_found_objectives', 'objective', creator=lambda objective: PlayerFoundObjective(objective=objective)) - player_caught_players = db.relationship( - 'PlayerCaughtPlayer', - back_populates='catching_player', - cascade='save-update, merge, delete, delete-orphan', - foreign_keys=[PlayerCaughtPlayer.catching_player_id]) - caught_players = association_proxy('player_caught_players', 'game_player', - creator=lambda game_player: PlayerCaughtPlayer(caught_player=game_player)) - - player_caught_by_players = db.relationship( - 'PlayerCaughtPlayer', - back_populates='caught_player', - 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)) + caught_by_players = association_proxy('player_caught_by_players', 'caught_player') + caught_players = association_proxy('player_caught_players', 'catching_player') diff --git a/app/models/player_caught_player.py b/app/models/player_caught_player.py index 4de2854..d096973 100644 --- a/app/models/player_caught_player.py +++ b/app/models/player_caught_player.py @@ -1,6 +1,7 @@ from sqlalchemy.sql import func from app import db +from .game_player import GamePlayer class PlayerCaughtPlayer(db.Model): __tablename__ = 'player_caught_player' @@ -9,6 +10,18 @@ class PlayerCaughtPlayer(db.Model): caught_player_id = db.Column(db.Integer, db.ForeignKey('game_player.id'), nullable=False) photo_reference = db.Column(db.String(128), unique=True, nullable=False) timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) - catching_player = db.relationship('GamePlayer', back_populates='player_caught_by_players', foreign_keys=[catching_player_id]) - caught_player = db.relationship('GamePlayer', back_populates='player_caught_players', foreign_keys=[caught_player_id]) - \ No newline at end of file + catching_player = db.relationship('GamePlayer', primaryjoin=(catching_player_id == GamePlayer.id), + backref=db.backref('player_caught_players', cascade='save-update, merge, delete, delete-orphan')) + caught_player = db.relationship('GamePlayer', primaryjoin=(caught_player_id == GamePlayer.id), + backref=db.backref('player_caught_by_players', cascade='save-update, merge, delete, delete-orphan')) + +''' +This relation doesn't work as well as the others, and must be used as folowed: +g = Game.query.first() +p1 = User.query[2].player_in(g) +p2 = User.query[3].player_in(g) +pc = PlayerCaughtPlayer(caught_player=p2, catching_player=p1, photo_reference='reference') +db.session.add(pc) +db.session.commit() + +''' \ No newline at end of file diff --git a/app/models/user.py b/app/models/user.py index a30a324..9cff91a 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -61,19 +61,26 @@ class User(UserMixin, db.Model): return max(self.locations_during_game(game), key=lambda location: location.timestamp) def role_in_game(self, game): - '''returns the role as Role enum of player in given game. Returns None if player does not participate in game''' - # pylint: disable=not-an-iterable - gameplayers = [gameplayer for gameplayer in self.user_games if gameplayer.game == game] - if not gameplayers: + '''Returns the role as Role enum of player in given game. Returns None if player does not participate in game''' + gameplayer = self.player_in(game) + if not gameplayer: return None - return gameplayers[0].role + return gameplayer.role def owns_game_played_by(self, player): - '''self is an owner of a game the player participates in''' + '''Self is an owner of a game the player participates in''' return self in [gameplayer.user for gameplayers in [game.game_players for game in player.games] for gameplayer in gameplayers if gameplayer.role == Role.owner] + def player_in(self, game): + # pylint: disable=not-an-iterable + '''Returns GamePlayer object for given game, or None if user does not participate in given game''' + gameplayers = [gameplayer for gameplayer in self.user_games if gameplayer.game == game] + if not gameplayers: + return None + return gameplayers[0] + @staticmethod def delete_orphans(): User.query.filter(~User.user_games.any()).delete() diff --git a/migrations/versions/fb12ee70fe66_reset_migrations.py b/migrations/versions/c2c2f3cf0c14_reset_migrations.py similarity index 98% rename from migrations/versions/fb12ee70fe66_reset_migrations.py rename to migrations/versions/c2c2f3cf0c14_reset_migrations.py index 9ff004d..5f53e13 100644 --- a/migrations/versions/fb12ee70fe66_reset_migrations.py +++ b/migrations/versions/c2c2f3cf0c14_reset_migrations.py @@ -1,8 +1,8 @@ """reset migrations -Revision ID: fb12ee70fe66 +Revision ID: c2c2f3cf0c14 Revises: -Create Date: 2020-07-19 00:47:38.970210 +Create Date: 2020-07-19 04:46:04.972644 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'fb12ee70fe66' +revision = 'c2c2f3cf0c14' down_revision = None branch_labels = None depends_on = None