From 7719f879ba7518433d82f395c1adceadba0a714b Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Sat, 24 Aug 2024 17:39:13 +0200 Subject: [PATCH] Started work on Player class --- src/classes/boardgame_classes.py | 60 +------------------------------ src/classes/many_to_many_links.py | 5 +-- src/classes/play_classes.py | 13 ++++--- src/classes/player_classes.py | 14 ++++++++ src/modules/db_connection.py | 13 ++++++- 5 files changed, 36 insertions(+), 69 deletions(-) create mode 100644 src/classes/player_classes.py diff --git a/src/classes/boardgame_classes.py b/src/classes/boardgame_classes.py index fb0ac91..84e300e 100644 --- a/src/classes/boardgame_classes.py +++ b/src/classes/boardgame_classes.py @@ -31,15 +31,6 @@ class BoardGameBase(SQLModel): 'validate_assignment':True } -# class OwnedBoardGameBase(BoardGameBase): -# price_paid: float -# acquisition_date: date -# acquired_from: str - -# class WishlistBoardGameBase(BoardGameBase): -# wishlist_priority: int - - class BoardGame(BoardGameBase, table=True): id: int = Field(primary_key=True) type: BoardgameType = BoardgameType.BOARDGAME @@ -135,55 +126,6 @@ class WishlistInfoPublicNoGame(SQLModel): wishlist_priority: int -# class BoardGamePublic(BoardGameBase): -# id: int -# type: BoardgameType -# plays: list["Play"] -# designers: list["Designer"] - -# class BoardGameNoPlays(BoardGameBase): -# id: int -# designers: list["Designer"] -# type: BoardgameType = BoardgameType.BOARDGAME - - -# class BoardGameExpansionPublic(BoardGameBase): -# id: int -# expansion_for: int -# type: BoardgameType -# plays: list["Play"] -# designers: list["Designer"] - -# class BoardGameExpansionNoPlays(BoardGameBase): -# id: int -# expansion_for: int -# designers: list["Designer"] -# type: BoardgameType = BoardgameType.BOARDGAMEEXPANSION - -# class OwnedBoardGame(OwnedBoardGameBase, table=True): -# id: int = Field(primary_key=True) -# type: BoardgameType = BoardgameType.OWNEDBOARDGAME - -# class OwnedBoardGameExpansion(OwnedBoardGameBase, table=True): -# id: int = Field(primary_key=True) -# expansion_for: int = Field(default=None, foreign_key="boardgame.id") -# type: BoardgameType = BoardgameType.OWNEDBOARDGAMEEXPANSION - -# class WishlistBoardGame(WishlistBoardGameBase, table=True): -# id: int = Field(primary_key=True) -# type: BoardgameType = BoardgameType.WISHLISTBOARDGAME - -# class WishlistBoardGameExpansion(WishlistBoardGameBase, table=True): -# id: int = Field(primary_key=True) -# expansion_for: int = Field(default=None, foreign_key="boardgame.id") -# type: BoardgameType = BoardgameType.WISHLISTBOARDGAMEEXPANSION - - - - from src.classes.play_classes import Play, PlayPublicNoGame from src.classes.people_classes import Designer, DesignerPublicNoGames -BoardGame.model_rebuild() -# BoardGameExpansion.model_rebuild() -# BoardGameNoPlays.model_rebuild() -# BoardGameExpansionNoPlays.model_rebuild() \ No newline at end of file +BoardGame.model_rebuild() \ No newline at end of file diff --git a/src/classes/many_to_many_links.py b/src/classes/many_to_many_links.py index 0c93201..d731458 100644 --- a/src/classes/many_to_many_links.py +++ b/src/classes/many_to_many_links.py @@ -3,7 +3,4 @@ from sqlmodel import Field,SQLModel class DesignerBoardGameLink(SQLModel, table=True): boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id", primary_key=True) designer_id: int | None = Field(default=None, foreign_key="designer.id", primary_key=True) - -# class DesignerBoardGameExpansionLink(SQLModel, table=True): -# boardgame_id: int | None = Field(default=None, foreign_key="boardgameexpansion.id", primary_key=True) -# designer_id: int | None = Field(default=None, foreign_key="designer.id", primary_key=True) \ No newline at end of file + \ No newline at end of file diff --git a/src/classes/play_classes.py b/src/classes/play_classes.py index 9ede2a1..451b851 100644 --- a/src/classes/play_classes.py +++ b/src/classes/play_classes.py @@ -3,7 +3,6 @@ from typing import Union from datetime import date class PlayPlayerBase(SQLModel): - name: str username: str score: Union[float, None] first_play : bool @@ -13,14 +12,19 @@ class PlayPlayerBase(SQLModel): class PlayPlayer(PlayPlayerBase, table=True): id: int | None = Field(default=None, primary_key=True) + name: str = Field(default=None, foreign_key="player.name") play: "Play" = Relationship(back_populates="players") + player: "Player" = Relationship(back_populates="playplayers") + class PlayPlayerPublic(PlayPlayerBase): id: int - + name: str + player: "PlayerPublicNoPlayPlayers" class PlayPlayerPublicWithPlay(PlayPlayerPublic): + name: str play: "PlayPublic" @@ -52,6 +56,5 @@ class PlayPublicNoGame(PlayBase): from src.classes.boardgame_classes import BoardGame, BoardGamePublicNoPlays -from src.classes.people_classes import Designer -Play.model_rebuild() -# PlayPublicWithPlayers.model_rebuild() \ No newline at end of file +from src.classes.player_classes import Player, PlayerPublic, PlayerPublicNoPlayPlayers +Play.model_rebuild() \ No newline at end of file diff --git a/src/classes/player_classes.py b/src/classes/player_classes.py new file mode 100644 index 0000000..5a54308 --- /dev/null +++ b/src/classes/player_classes.py @@ -0,0 +1,14 @@ +from sqlmodel import SQLModel, Field, Relationship +from src.classes.play_classes import PlayPlayer + +class Player(SQLModel, table=True): + name: str | None = Field(default=None, primary_key=True) + + playplayers: list[PlayPlayer] = Relationship(back_populates="player") + + +class PlayerPublic(SQLModel): + name: str + +class PlayerPublicNoPlayPlayers(SQLModel): + name: str \ No newline at end of file diff --git a/src/modules/db_connection.py b/src/modules/db_connection.py index e2af2e8..413ba60 100644 --- a/src/modules/db_connection.py +++ b/src/modules/db_connection.py @@ -7,7 +7,7 @@ import copy critical_function_lock = Lock() -from src.classes import boardgame_classes, play_classes, people_classes +from src.classes import boardgame_classes, play_classes, people_classes, player_classes sqlite_url = definitions.SQLITE_URL @@ -188,6 +188,17 @@ def add_multiple_plays(session: Session, play_list: list[play_classes.Play]): with critical_function_lock: for play in play_list: + + for playplayer in play.players: + + is_player_present = len(session.exec(select(player_classes.Player).where(player_classes.Player.name == playplayer.name)).all()) != 0 + + if not is_player_present: + new_player = player_classes.Player(name=playplayer.name) + session.add(new_player) + + + is_play_present = len(session.exec(select(play_classes.Play).where(play_classes.Play.id == play.id)).all()) != 0 if not is_play_present: