Made plays work with refactored changes

This commit is contained in:
Yarne Coppens 2024-08-22 09:55:31 +02:00
parent 929e45380b
commit 51cd2dda07
7 changed files with 51 additions and 47 deletions

View file

@ -43,7 +43,6 @@ class BoardGameBase(SQLModel):
class BoardGame(BoardGameBase, table=True): class BoardGame(BoardGameBase, table=True):
id: int = Field(primary_key=True) id: int = Field(primary_key=True)
type: BoardgameType = BoardgameType.BOARDGAME type: BoardgameType = BoardgameType.BOARDGAME
#plays: list["Play"] = Relationship(back_populates='boardgame')
designers: list["Designer"] = Relationship(back_populates="designed_boardgames", link_model=many_to_many_links.DesignerBoardGameLink) designers: list["Designer"] = Relationship(back_populates="designed_boardgames", link_model=many_to_many_links.DesignerBoardGameLink)
expansion_info: Optional["ExpansionInfo"] = Relationship(back_populates="boardgame") expansion_info: Optional["ExpansionInfo"] = Relationship(back_populates="boardgame")
@ -52,6 +51,8 @@ class BoardGame(BoardGameBase, table=True):
wishlist_info: Optional["WishlistInfo"] = Relationship(back_populates="boardgame") wishlist_info: Optional["WishlistInfo"] = Relationship(back_populates="boardgame")
plays: list["Play"] = Relationship(back_populates='boardgame')
model_config = { model_config = {
'arbitrary_types_allowed':True 'arbitrary_types_allowed':True
} }
@ -62,6 +63,14 @@ class BoardGamePublic(BoardGameBase):
expansion_info: Optional["ExpansionInfoPublicNoGame"] expansion_info: Optional["ExpansionInfoPublicNoGame"]
owned_info: Optional["OwnedInfoPublicNoGame"] owned_info: Optional["OwnedInfoPublicNoGame"]
wishlist_info: Optional["WishlistInfoPublicNoGame"] wishlist_info: Optional["WishlistInfoPublicNoGame"]
plays: list["PlayPublicNoGame"]
class BoardGamePublicNoPlays(BoardGameBase):
id: int
designers: list["DesignerPublicNoGames"]
expansion_info: Optional["ExpansionInfoPublicNoGame"]
owned_info: Optional["OwnedInfoPublicNoGame"]
wishlist_info: Optional["WishlistInfoPublicNoGame"]
class ExpansionInfo(SQLModel, table=True): class ExpansionInfo(SQLModel, table=True):
@ -172,7 +181,7 @@ class WishlistInfoPublicNoGame(SQLModel):
from src.classes.play_classes import Play, PlayPublic from src.classes.play_classes import Play, PlayPublicNoGame
from src.classes.people_classes import Designer, DesignerPublicNoGames from src.classes.people_classes import Designer, DesignerPublicNoGames
BoardGame.model_rebuild() BoardGame.model_rebuild()
# BoardGameExpansion.model_rebuild() # BoardGameExpansion.model_rebuild()

View file

@ -8,13 +8,13 @@ class PlayPlayerBase(SQLModel):
score: Union[float, None] score: Union[float, None]
first_play : bool first_play : bool
has_won : bool has_won : bool
#play_id : int = Field(default=None, foreign_key="play.id") play_id : int = Field(default=None, foreign_key="play.id")
class PlayPlayer(PlayPlayerBase, table=True): class PlayPlayer(PlayPlayerBase, table=True):
id: int | None = Field(default=None, primary_key=True) id: int | None = Field(default=None, primary_key=True)
#play: "Play" = Relationship(back_populates="players") play: "Play" = Relationship(back_populates="players")
class PlayPlayerPublic(PlayPlayerBase): class PlayPlayerPublic(PlayPlayerBase):
id: int id: int
@ -25,8 +25,7 @@ class PlayPlayerPublicWithPlay(PlayPlayerPublic):
class PlayBase(SQLModel): class PlayBase(SQLModel):
#boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id") boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id")
#expansion_id: int | None = Field(default=None, foreign_key="boardgameexpansion.id")
play_date: date play_date: date
duration: int #In minutes duration: int #In minutes
ignore_for_stats : bool ignore_for_stats : bool
@ -36,25 +35,23 @@ class PlayBase(SQLModel):
class Play(PlayBase, table=True): class Play(PlayBase, table=True):
id: int | None = Field(default=None, primary_key=True) id: int | None = Field(default=None, primary_key=True)
#players: list[PlayPlayer] = Relationship(back_populates="play") players: list[PlayPlayer] = Relationship(back_populates="play")
#boardgame: Union["BoardGame", None] = Relationship(back_populates="plays") boardgame: "BoardGame" = Relationship(back_populates="plays")
#boardgameexpansion: Union["BoardGameExpansion", None] = Relationship(back_populates="plays")
model_config = { model_config = {
'validate_assignment':True 'validate_assignment':True
} }
class PlayPublic(PlayBase): class PlayPublic(PlayBase):
id: int players: list[PlayPlayerPublic]
boardgame: "BoardGamePublicNoPlays"
# class PlayPublicWithPlayers(PlayPublic): class PlayPublicNoGame(PlayPublic):
# players: list[PlayPlayerPublic] = [] players: list[PlayPlayerPublic] = []
# boardgame: Union["BoardGameNoPlays", None]
# boardgameexpansion: Union["BoardGameExpansionNoPlays", None]
from src.classes.boardgame_classes import BoardGame#, BoardGameExpansion, BoardGameNoPlays, BoardGameExpansionNoPlays from src.classes.boardgame_classes import BoardGame, BoardGamePublicNoPlays
from src.classes.people_classes import Designer from src.classes.people_classes import Designer
Play.model_rebuild() Play.model_rebuild()
# PlayPublicWithPlayers.model_rebuild() # PlayPublicWithPlayers.model_rebuild()

View file

@ -16,4 +16,4 @@ def filter_non_expansions_out(play_list: list[play_classes.Play]):
def filter_only_specific_boardgame(boardgame_id: int, play_list: list[play_classes.Play]): def filter_only_specific_boardgame(boardgame_id: int, play_list: list[play_classes.Play]):
return list(filter(lambda x: x.boardgame_id == boardgame_id or x.expansion_id == boardgame_id, play_list)) return list(filter(lambda x: x.boardgame_id == boardgame_id, play_list))

View file

@ -140,17 +140,17 @@ def get_wishlist_collection(priority: int = 0, query: BoardgameFilterParams = De
return to_return_boardgames return to_return_boardgames
# @app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers]) @app.get("/plays", response_model=list[play_classes.PlayPublic])
# def get_plays(query: PlayFilterParams = Depends(), boardgame_id: int = -1, session: Session = Depends(get_session)): def get_plays(query: PlayFilterParams = Depends(), boardgame_id: int = -1, session: Session = Depends(get_session)):
# requested_plays = data_connection.get_plays(session) requested_plays = data_connection.get_plays(session)
# requested_plays = query.do_filtering(requested_plays) requested_plays = query.do_filtering(requested_plays)
# if boardgame_id > -1: if boardgame_id > -1:
# requested_plays = play_filters.filter_only_specific_boardgame(boardgame_id, requested_plays) requested_plays = play_filters.filter_only_specific_boardgame(boardgame_id, requested_plays)
# return requested_plays return requested_plays
@app.get('/players', response_model=list[play_classes.PlayPlayerPublic]) @app.get('/players', response_model=list[play_classes.PlayPlayerPublic])

View file

@ -224,13 +224,13 @@ def convert_play_xml_to_play(play_xml: ET.Element) -> play_classes.Play:
for play_player_xml in play_xml.find('players'): for play_player_xml in play_xml.find('players'):
playplayer_list.append(convert_playplayer_xml_to_playplayer(play_player_xml)) playplayer_list.append(convert_playplayer_xml_to_playplayer(play_player_xml))
id_key = "boardgame_id" # id_key = "boardgame_id"
for subtype in play_xml.find('item').find('subtypes'): # for subtype in play_xml.find('item').find('subtypes'):
if subtype.get('value') == 'boardgameexpansion': # if subtype.get('value') == 'boardgameexpansion':
id_key = "expansion_id" # id_key = "expansion_id"
play_dict = { play_dict = {
id_key : int(play_xml.find('item').get('objectid')), "boardgame_id" : int(play_xml.find('item').get('objectid')),
"players" : playplayer_list, "players" : playplayer_list,
"play_date" : play_date, "play_date" : play_date,
"duration" : int(play_xml.get('length')), "duration" : int(play_xml.get('length')),
@ -330,15 +330,21 @@ def get_plays() -> list[play_classes.Play]:
all_plays : list[play_classes.Play] = [] all_plays : list[play_classes.Play] = []
for page in tqdm( # for page in tqdm(
range(amount_of_pages_needed), # range(amount_of_pages_needed),
desc="Getting plays from BGG", # desc="Getting plays from BGG",
unit="requests"): # unit="requests"):
url = 'https://boardgamegeek.com/xmlapi2/plays?username={}&page={}'.format(auth_manager.username, page + 1) # url = 'https://boardgamegeek.com/xmlapi2/plays?username={}&page={}'.format(auth_manager.username, page + 1)
plays_page_xml_object = url_to_xml_object(url) # plays_page_xml_object = url_to_xml_object(url)
for play_xml in plays_page_xml_object: # for play_xml in plays_page_xml_object:
new_play = convert_play_xml_to_play(play_xml) # new_play = convert_play_xml_to_play(play_xml)
all_plays.append(new_play) # all_plays.append(new_play)
url = 'https://boardgamegeek.com/xmlapi2/plays?username={}&page=1'.format(auth_manager.username)
plays_page_xml_object = url_to_xml_object(url)
for play_xml in plays_page_xml_object:
new_play = convert_play_xml_to_play(play_xml)
all_plays.append(new_play)
return all_plays return all_plays

View file

@ -96,15 +96,14 @@ def get_plays(session: Session) -> list[play_classes.Play]:
#Making sure all played board games are in table 'boardgames' #Making sure all played board games are in table 'boardgames'
#list + set to remove duplicates #list + set to remove duplicates
played_boardgame_ids = list(set([play.boardgame_id for play in plays_from_db])) played_boardgame_ids = list(set([play.boardgame_id for play in plays_from_db]))
played_expansion_ids = list(set([play.expansion_id for play in plays_from_db]))
#Remove None's (played board games don't have expansion id and vice versa) #Remove None's (played board games don't have expansion id and vice versa)
played_boardgame_ids = list(filter(lambda x: x != None, played_boardgame_ids)) played_boardgame_ids = list(filter(lambda x: x != None, played_boardgame_ids))
played_expansion_ids = list(filter(lambda x: x != None, played_expansion_ids))
assert len(list(filter(lambda x: x == None, played_boardgame_ids))) == 0, plays_from_db assert len(list(filter(lambda x: x == None, played_boardgame_ids))) == 0, plays_from_db
#get_multiple_boardgames(session, played_boardgame_ids + played_expansion_ids) #Make sure to add all board games that are played
get_multiple_boardgames(session, played_boardgame_ids)
return plays_from_db return plays_from_db

View file

@ -107,13 +107,6 @@ def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> tuple
boardgames = results.all() boardgames = results.all()
statement = select(boardgame_classes.BoardGameExpansion).where(boardgame_classes.BoardGameExpansion.id.in_(boardgame_ids))
results = session.exec(statement)
expansions = results.all()
boardgames += expansions
missing_boardgame_ids = list(filter(lambda x: x not in [boardgame.id for boardgame in boardgames], boardgame_ids)) missing_boardgame_ids = list(filter(lambda x: x not in [boardgame.id for boardgame in boardgames], boardgame_ids))
return boardgames, missing_boardgame_ids return boardgames, missing_boardgame_ids