From ce53e95c6bf6758fbfe5eb8059c5113e7b81c5c1 Mon Sep 17 00:00:00 2001 From: Burathar Date: Fri, 3 Jul 2020 23:38:00 +0200 Subject: [PATCH] Rename documentation/drm to database, convert secondary tables to association tables --- app/models.py | 158 +++++++++++------- app/routes.py | 3 +- app/templates/create_game.html | 0 app/templates/index.html | 25 +-- .../database_schema1.1.drawio | 0 .../{drm => database}/database_schema1.1.png | Bin .../database_schema1.2.drawio | 0 .../{drm => database}/database_schema1.2.png | Bin .../database_schema1.3.drawio | 0 .../{drm => database}/database_schema1.3.png | Bin .../entity_relationship_diagram_v1.0.drawio | 0 .../entity_relationship_diagram_v1.0.png | Bin .../entity_relationship_diagram_v1.1.drawio | 0 .../entity_relationship_diagram_v1.1.png | Bin .../entity_relationship_diagram_v1.2.drawio | 0 .../entity_relationship_diagram_v1.2.png | Bin .../entity_relationship_diagram_v1.3.drawio | 0 .../entity_relationship_diagram_v1.3.png | Bin ...06_update_up_to_1_3_association_tables.py} | 16 +- the_hunt.py | 4 +- 20 files changed, 126 insertions(+), 80 deletions(-) create mode 100644 app/templates/create_game.html rename documentation/{drm => database}/database_schema1.1.drawio (100%) rename documentation/{drm => database}/database_schema1.1.png (100%) rename documentation/{drm => database}/database_schema1.2.drawio (100%) rename documentation/{drm => database}/database_schema1.2.png (100%) rename documentation/{drm => database}/database_schema1.3.drawio (100%) rename documentation/{drm => database}/database_schema1.3.png (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.0.drawio (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.0.png (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.1.drawio (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.1.png (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.2.drawio (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.2.png (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.3.drawio (100%) rename documentation/{drm => database}/entity_relationship_diagram_v1.3.png (100%) rename migrations/versions/{01c009738a1f_update_to_1_3.py => 68009992b106_update_up_to_1_3_association_tables.py} (89%) diff --git a/app/models.py b/app/models.py index 6589e69..ce130d8 100644 --- a/app/models.py +++ b/app/models.py @@ -1,48 +1,65 @@ +from enum import Enum from werkzeug.security import generate_password_hash, check_password_hash from app import app, db, login +from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.sql import func from secrets import token_hex from flask_login import UserMixin -game_player = db.Table('game_player', - db.Column('game_id', db.Integer, db.ForeignKey('game.id'), nullable=False), - db.Column('player_id', db.Integer, db.ForeignKey('player.id'), nullable=False), - db.PrimaryKeyConstraint('game_id', 'player_id'), - db.Column('role', db.String(16)) - ) - -player_found_objective = db.Table('player_found_objective', - db.Column('objective_id', db.Integer, db.ForeignKey('objective.id'), nullable=False, server_default='-1'), - db.Column('player_id', db.Integer, db.ForeignKey('player.id'), nullable=False, server_default='-1'), - db.PrimaryKeyConstraint('objective_id', 'player_id'), - db.Column('timestamp', db.DateTime, server_default=func.now(), nullable=False) - ) - -player_caught_player = db.Table('player_caught_player', - db.Column('catching_player_id', db.Integer, db.ForeignKey('player.id')), - db.Column('caught_player_id', db.Integer, db.ForeignKey('player.id')), - db.Column('photo_reference', db.String(128), unique=True, nullable=False), - db.Column('timestamp', db.DateTime, server_default=func.now(), nullable=False) - ) - -notification_player = db.Table('notification_player', - db.Column('notification_id', db.Integer, db.ForeignKey('notification.id')), - db.Column('player_id', db.Integer, db.ForeignKey('player.id')), - db.PrimaryKeyConstraint('notification_id', 'player_id'), - db.Column('been_shown', db.Boolean, server_default='True', nullable=False) - ) +class GameState(Enum): + initiated = 1 + published = 2 + started = 3 + interrupted = 4 + finished = 5 + +class GamePlayer(db.Model): + __tablename__ = 'game_player' + game_id = db.Column(db.Integer, db.ForeignKey('game.id'), primary_key=True, nullable=False) + player_id = db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False) + role = db.Column(db.String(16)) + game = db.relationship('Game', back_populates='game_players') + player = db.relationship('Player', back_populates='player_games') + +class PlayerFoundObjective(db.Model): + __tablename__ = 'player_found_objective' + objective_id = db.Column(db.Integer, db.ForeignKey('objective.id'), primary_key=True, nullable=False, server_default='-1') + player_id = db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False, server_default='-1') + timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) + player = db.relationship('Player', back_populates='player_found_objectives') + objective = db.relationship('Objective', back_populates='objective_found_by') + +class NotificationPlayer(db.Model): + __tablename__ = 'notification_player' + notification_id = db.Column(db.Integer, db.ForeignKey('notification.id'), primary_key=True, nullable=False) + player_id= db.Column(db.Integer, db.ForeignKey('player.id'), primary_key=True, nullable=False) + been_shown = db.Column(db.Boolean, server_default='True', nullable=False) + notification = db.relationship('Notification', back_populates='notification_recipients') + recipient = db.relationship('Player', back_populates='player_notifications') + +class PlayerCaughtPlayer(db.Model): + __tablename__ = 'player_caught_player' + id = db.Column(db.Integer, primary_key=True, autoincrement=True, server_default='-1') + catching_player_id = db.Column(db.Integer, db.ForeignKey('player.id'), nullable=False) + caught_player_id = db.Column(db.Integer, db.ForeignKey('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('Player', back_populates='player_caught_by_players', foreign_keys=[catching_player_id]) + caught_player = db.relationship('Player', back_populates='player_caught_players', foreign_keys=[caught_player_id]) class Game(db.Model): __tablename__ = 'game' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), index=True, unique=True, nullable=False) - state = db.Column(db.Integer, server_default='1') + state = db.Column(db.Enum(GameState), server_default=GameState(1).name) start_time = db.Column(db.DateTime) end_time = db.Column(db.DateTime) - players = db.relationship( - 'Player', - secondary=game_player, - back_populates='games') + game_players = db.relationship( + 'GamePlayer', + back_populates='game', + cascade="save-update, merge, delete, delete-orphan") + players = association_proxy('game_players', 'player', + creator=lambda player: GamePlayer(player=player)) # to enable game.players.append(player) objectives = db.relationship( 'Objective', lazy='select', @@ -58,28 +75,48 @@ class Player(UserMixin, db.Model): name = db.Column(db.String(64), unique=True, nullable=False) auth_hash = db.Column(db.String(32), unique=True, nullable=True) password_hash = db.Column(db.String(128)) - games = db.relationship( - 'Game', - secondary=game_player, - back_populates='players') - objectives_found = db.relationship( - 'Objective', - secondary=player_found_objective, - back_populates='players') - caught_players = db.relationship( - 'Player', - secondary=player_caught_player, - primaryjoin=(player_caught_player.c.catching_player_id == id), - secondaryjoin=(player_caught_player.c.caught_player_id == id), - backref=db.backref('caught_by', lazy='dynamic'), lazy='dynamic') + + player_games = db.relationship( + 'GamePlayer', + back_populates='player', + cascade='save-update, merge, delete, delete-orphan') + games = association_proxy('player_games', 'game', + creator=lambda game: GamePlayer(game=game)) + + player_found_objectives = db.relationship( + 'PlayerFoundObjective', + back_populates='player', + cascade='save-update, merge, delete, delete-orphan') + found_objectives = association_proxy('player_found_objectives', 'objective', + creator=lambda objective: PlayerFoundObjective(objective=objective)) + + player_notifications = db.relationship( + 'NotificationPlayer', + back_populates='recipient', + cascade='save-update, merge, delete, delete-orphan') + notifications = association_proxy('player_notifications', 'notification', + creator=lambda notification: NotificationPlayer(notification=notification)) + + 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', 'player', + creator=lambda player: PlayerCaughtPlayer(caught_player=player)) + + player_caught_by_players = db.relationship( + 'PlayerCaughtPlayer', + back_populates='caught_player', + cascade='save-update, merge, delete, delete-orphan', + foreign_keys=[PlayerCaughtPlayer.caught_player_id]) + caught_by_players = association_proxy('player_caught_by_players', 'player', + creator=lambda player: PlayerCaughtPlayer(catching_player=player)) + locations = db.relationship( 'Location', lazy='select', backref=db.backref('player', lazy='joined')) - notifications = db.relationship( - 'Notification', - secondary=notification_player, - back_populates='recipients') def set_password(self, password): self.password_hash = generate_password_hash(password) @@ -102,10 +139,12 @@ class Objective(db.Model): hash = db.Column(db.String(128), unique=True, nullable=False) longitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None)) # maybe check asdecimal and decimal_return_scale later? latitude = db.Column(db.Numeric(precision=15, scale=10, asdecimal=False, decimal_return_scale=None)) - players = db.relationship( - 'Player', - secondary=player_found_objective, - back_populates='objectives_found') + objective_found_by = db.relationship( + 'PlayerFoundObjective', + back_populates='objective', + cascade='save-update, merge, delete, delete-orphan') + found_by = association_proxy('objective_found_by', 'player', + creator=lambda player: PlayerFoundObjective(player=player)) class Location(db.Model): __tablename__ = 'location' @@ -122,9 +161,12 @@ class Notification(db.Model): message = db.Column(db.Text, nullable=False) type = db.Column(db.String(64), nullable=False, default='General') timestamp = db.Column(db.DateTime, server_default=func.now(), nullable=False) - recipients = db.relationship( - 'Player', - secondary=notification_player, - back_populates='notifications') + + notification_recipients = db.relationship( + 'NotificationPlayer', + back_populates='notification', + cascade='save-update, merge, delete, delete-orphan') + recipients = association_proxy('notification_recipients', 'player', + creator=lambda player: NotificationPlayer(recipient=player)) #start_time = db.Column(db.DateTime, server_default=func.now()) diff --git a/app/routes.py b/app/routes.py index c065776..d0f0d9c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -8,8 +8,7 @@ from app.forms import LoginForm, RegistrationForm @app.route('/index', methods=['GET']) @login_required def index(): - message="Hello, World" - return render_template("index.html", title='Home', message=message) + return render_template("index.html", title='Home') @app.route('/login', methods=['GET', 'POST']) def login(): diff --git a/app/templates/create_game.html b/app/templates/create_game.html new file mode 100644 index 0000000..e69de29 diff --git a/app/templates/index.html b/app/templates/index.html index 69e0cb7..d92fb17 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -2,39 +2,40 @@ {% block app_content %}

Hi, {{ current_user.name }}!

-

{{ message }}

My games:

+{% if current_user.games %}
- - + + - - - - - - - {% for game in current_user.games %} - + {% endfor %}
NameStateGame NameCurrent State Start Time End Time My Role
GamenameRunningyesterdaytomorrowbitch
{{ game.name }} {{ game.state }} {{ game.start_time }} {{ game.end_time }}bitch + {% for gameplayer in current_user.player_games %} + {% if gameplayer.game == game %}{{ gameplayer.role }}{% endif %} + {% endfor %} +
- +{% else %} + +{% endif %} {% endblock %} \ No newline at end of file diff --git a/documentation/drm/database_schema1.1.drawio b/documentation/database/database_schema1.1.drawio similarity index 100% rename from documentation/drm/database_schema1.1.drawio rename to documentation/database/database_schema1.1.drawio diff --git a/documentation/drm/database_schema1.1.png b/documentation/database/database_schema1.1.png similarity index 100% rename from documentation/drm/database_schema1.1.png rename to documentation/database/database_schema1.1.png diff --git a/documentation/drm/database_schema1.2.drawio b/documentation/database/database_schema1.2.drawio similarity index 100% rename from documentation/drm/database_schema1.2.drawio rename to documentation/database/database_schema1.2.drawio diff --git a/documentation/drm/database_schema1.2.png b/documentation/database/database_schema1.2.png similarity index 100% rename from documentation/drm/database_schema1.2.png rename to documentation/database/database_schema1.2.png diff --git a/documentation/drm/database_schema1.3.drawio b/documentation/database/database_schema1.3.drawio similarity index 100% rename from documentation/drm/database_schema1.3.drawio rename to documentation/database/database_schema1.3.drawio diff --git a/documentation/drm/database_schema1.3.png b/documentation/database/database_schema1.3.png similarity index 100% rename from documentation/drm/database_schema1.3.png rename to documentation/database/database_schema1.3.png diff --git a/documentation/drm/entity_relationship_diagram_v1.0.drawio b/documentation/database/entity_relationship_diagram_v1.0.drawio similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.0.drawio rename to documentation/database/entity_relationship_diagram_v1.0.drawio diff --git a/documentation/drm/entity_relationship_diagram_v1.0.png b/documentation/database/entity_relationship_diagram_v1.0.png similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.0.png rename to documentation/database/entity_relationship_diagram_v1.0.png diff --git a/documentation/drm/entity_relationship_diagram_v1.1.drawio b/documentation/database/entity_relationship_diagram_v1.1.drawio similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.1.drawio rename to documentation/database/entity_relationship_diagram_v1.1.drawio diff --git a/documentation/drm/entity_relationship_diagram_v1.1.png b/documentation/database/entity_relationship_diagram_v1.1.png similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.1.png rename to documentation/database/entity_relationship_diagram_v1.1.png diff --git a/documentation/drm/entity_relationship_diagram_v1.2.drawio b/documentation/database/entity_relationship_diagram_v1.2.drawio similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.2.drawio rename to documentation/database/entity_relationship_diagram_v1.2.drawio diff --git a/documentation/drm/entity_relationship_diagram_v1.2.png b/documentation/database/entity_relationship_diagram_v1.2.png similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.2.png rename to documentation/database/entity_relationship_diagram_v1.2.png diff --git a/documentation/drm/entity_relationship_diagram_v1.3.drawio b/documentation/database/entity_relationship_diagram_v1.3.drawio similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.3.drawio rename to documentation/database/entity_relationship_diagram_v1.3.drawio diff --git a/documentation/drm/entity_relationship_diagram_v1.3.png b/documentation/database/entity_relationship_diagram_v1.3.png similarity index 100% rename from documentation/drm/entity_relationship_diagram_v1.3.png rename to documentation/database/entity_relationship_diagram_v1.3.png diff --git a/migrations/versions/01c009738a1f_update_to_1_3.py b/migrations/versions/68009992b106_update_up_to_1_3_association_tables.py similarity index 89% rename from migrations/versions/01c009738a1f_update_to_1_3.py rename to migrations/versions/68009992b106_update_up_to_1_3_association_tables.py index 9870626..66b70c8 100644 --- a/migrations/versions/01c009738a1f_update_to_1_3.py +++ b/migrations/versions/68009992b106_update_up_to_1_3_association_tables.py @@ -1,8 +1,8 @@ -"""update to 1.3 +"""update up to 1.3+association tables -Revision ID: 01c009738a1f +Revision ID: 68009992b106 Revises: -Create Date: 2020-07-03 13:12:00.463246 +Create Date: 2020-07-03 23:14:41.035088 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = '01c009738a1f' +revision = '68009992b106' down_revision = None branch_labels = None depends_on = None @@ -21,7 +21,7 @@ def upgrade(): op.create_table('game', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(length=64), nullable=False), - sa.Column('state', sa.Integer(), server_default='1', nullable=True), + sa.Column('state', sa.Enum('initiated', 'published', 'started', 'interrupted', 'finished', name='gamestate'), server_default='initiated', nullable=True), sa.Column('start_time', sa.DateTime(), nullable=True), sa.Column('end_time', sa.DateTime(), nullable=True), sa.PrimaryKeyConstraint('id') @@ -74,12 +74,14 @@ def upgrade(): sa.UniqueConstraint('hash') ) op.create_table('player_caught_player', - sa.Column('catching_player_id', sa.Integer(), nullable=True), - sa.Column('caught_player_id', sa.Integer(), nullable=True), + sa.Column('id', sa.Integer(), server_default='-1', autoincrement=True, nullable=False), + sa.Column('catching_player_id', sa.Integer(), nullable=False), + sa.Column('caught_player_id', sa.Integer(), nullable=False), sa.Column('photo_reference', sa.String(length=128), nullable=False), sa.Column('timestamp', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False), sa.ForeignKeyConstraint(['catching_player_id'], ['player.id'], ), sa.ForeignKeyConstraint(['caught_player_id'], ['player.id'], ), + sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('photo_reference') ) op.create_table('notification_player', diff --git a/the_hunt.py b/the_hunt.py index 283f855..58edea0 100644 --- a/the_hunt.py +++ b/the_hunt.py @@ -4,4 +4,6 @@ from app.models import Game, Player, Objective, Location, Notification @app.shell_context_processor def make_shell_context(): return {'db': db, 'Game' : Game, 'Player' : Player, 'Objective' : Objective, - 'Location' : Location, 'Notification' : Notification} + 'Location' : Location, 'Notification' : Notification, 'GamePlayer' : GamePlayer, + 'PlayerFoundObjective' : PlayerFoundObjective, 'NotificationPlayer' : NotificationPlayer, + 'PlayerCaughtPlayer' : PlayerCaughtPlayer, 'GameState' : GameState}