Started work on the refactoring
This commit is contained in:
parent
616d40fce0
commit
f9ab7bf535
10 changed files with 161 additions and 169 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from datetime import date
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
from src.classes import many_to_many_links
|
||||
|
||||
|
|
@ -30,69 +31,111 @@ class BoardGameBase(SQLModel):
|
|||
'validate_assignment':True
|
||||
}
|
||||
|
||||
class OwnedBoardGameBase(BoardGameBase):
|
||||
price_paid: float
|
||||
acquisition_date: date
|
||||
acquired_from: str
|
||||
# class OwnedBoardGameBase(BoardGameBase):
|
||||
# price_paid: float
|
||||
# acquisition_date: date
|
||||
# acquired_from: str
|
||||
|
||||
class WishlistBoardGameBase(BoardGameBase):
|
||||
wishlist_priority: int
|
||||
# class WishlistBoardGameBase(BoardGameBase):
|
||||
# wishlist_priority: int
|
||||
|
||||
|
||||
class BoardGame(BoardGameBase, table=True):
|
||||
id: int = Field(primary_key=True)
|
||||
type: BoardgameType = BoardgameType.BOARDGAME
|
||||
plays: list["Play"] = Relationship(back_populates='boardgame')
|
||||
#plays: list["Play"] = Relationship(back_populates='boardgame')
|
||||
designers: list["Designer"] = Relationship(back_populates="designed_boardgames", link_model=many_to_many_links.DesignerBoardGameLink)
|
||||
expansion_info: Optional["ExpansionInfo"] = Relationship(back_populates="boardgame")
|
||||
owned_info: Optional["OwnedInfo"] = Relationship(back_populates="boardgame")
|
||||
wishlist_info: Optional["WishlistInfo"] = Relationship(back_populates="boardgame")
|
||||
|
||||
model_config = {
|
||||
'arbitrary_types_allowed':True
|
||||
}
|
||||
|
||||
class BoardGamePublic(BoardGameBase):
|
||||
id: int
|
||||
type: BoardgameType
|
||||
plays: list["Play"]
|
||||
designers: list["Designer"]
|
||||
expansion_info: Optional["ExpansionInfo"]
|
||||
owned_info: Optional["OwnedInfo"]
|
||||
wishlist_info: Optional["WishlistInfo"]
|
||||
|
||||
class BoardGameNoPlays(BoardGameBase):
|
||||
id: int
|
||||
designers: list["Designer"]
|
||||
type: BoardgameType = BoardgameType.BOARDGAME
|
||||
|
||||
class BoardGameExpansion(BoardGameBase, table=True):
|
||||
id: int = Field(primary_key=True)
|
||||
expansion_for: int = Field(default=None, foreign_key="boardgame.id")
|
||||
type: BoardgameType = BoardgameType.BOARDGAMEEXPANSION
|
||||
plays: list["Play"] = Relationship(back_populates='boardgameexpansion')
|
||||
designers: list["Designer"] = Relationship(back_populates="designed_expansions", link_model=many_to_many_links.DesignerBoardGameExpansionLink)
|
||||
class ExpansionInfo(BoardGameBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id")
|
||||
boardgame: BoardGame = Relationship(
|
||||
#for one-on-one relationship
|
||||
sa_relationship_kwargs={'uselist': False},
|
||||
back_populates="expansion_info"
|
||||
)
|
||||
|
||||
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 OwnedInfo(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id")
|
||||
boardgame: BoardGame = Relationship(
|
||||
#for one-on-one relationship
|
||||
sa_relationship_kwargs={'uselist': False},
|
||||
back_populates="owned_info"
|
||||
)
|
||||
price_paid: float
|
||||
acquisition_date: date
|
||||
acquired_from: str
|
||||
|
||||
class OwnedBoardGame(OwnedBoardGameBase, table=True):
|
||||
id: int = Field(primary_key=True)
|
||||
type: BoardgameType = BoardgameType.OWNEDBOARDGAME
|
||||
class WishlistInfo(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id")
|
||||
boardgame: BoardGame = Relationship(
|
||||
#for one-on-one relationship
|
||||
sa_relationship_kwargs={'uselist': False},
|
||||
back_populates="wishlist_info"
|
||||
)
|
||||
wishlist_priority: int
|
||||
|
||||
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 BoardGamePublic(BoardGameBase):
|
||||
# id: int
|
||||
# type: BoardgameType
|
||||
# plays: list["Play"]
|
||||
# designers: list["Designer"]
|
||||
|
||||
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
|
||||
# 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
|
||||
|
||||
|
||||
|
||||
|
|
@ -100,6 +143,6 @@ class WishlistBoardGameExpansion(WishlistBoardGameBase, table=True):
|
|||
from src.classes.play_classes import Play, PlayPublic
|
||||
from src.classes.people_classes import Designer
|
||||
BoardGame.model_rebuild()
|
||||
BoardGameExpansion.model_rebuild()
|
||||
BoardGameNoPlays.model_rebuild()
|
||||
BoardGameExpansionNoPlays.model_rebuild()
|
||||
# BoardGameExpansion.model_rebuild()
|
||||
# BoardGameNoPlays.model_rebuild()
|
||||
# BoardGameExpansionNoPlays.model_rebuild()
|
||||
|
|
@ -4,6 +4,6 @@ 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)
|
||||
# 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)
|
||||
|
|
@ -6,4 +6,4 @@ class Designer(SQLModel, table=True):
|
|||
id: int = Field(primary_key=True)
|
||||
name: str
|
||||
designed_boardgames: list[boardgame_classes.BoardGame] | None = Relationship(back_populates="designers", link_model=many_to_many_links.DesignerBoardGameLink)
|
||||
designed_expansions: list[boardgame_classes.BoardGameExpansion] | None = Relationship(back_populates="designers", link_model=many_to_many_links.DesignerBoardGameExpansionLink)
|
||||
# designed_expansions: list[boardgame_classes.BoardGameExpansion] | None = Relationship(back_populates="designers", link_model=many_to_many_links.DesignerBoardGameExpansionLink)
|
||||
|
|
@ -8,13 +8,13 @@ class PlayPlayerBase(SQLModel):
|
|||
score: Union[float, None]
|
||||
first_play : 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):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
play: "Play" = Relationship(back_populates="players")
|
||||
#play: "Play" = Relationship(back_populates="players")
|
||||
|
||||
class PlayPlayerPublic(PlayPlayerBase):
|
||||
id: int
|
||||
|
|
@ -25,8 +25,8 @@ class PlayPlayerPublicWithPlay(PlayPlayerPublic):
|
|||
|
||||
|
||||
class PlayBase(SQLModel):
|
||||
boardgame_id: int | None = Field(default=None, foreign_key="boardgame.id")
|
||||
expansion_id: int | None = Field(default=None, foreign_key="boardgameexpansion.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
|
||||
duration: int #In minutes
|
||||
ignore_for_stats : bool
|
||||
|
|
@ -36,9 +36,9 @@ class PlayBase(SQLModel):
|
|||
class Play(PlayBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
players: list[PlayPlayer] = Relationship(back_populates="play")
|
||||
boardgame: Union["BoardGame", None] = Relationship(back_populates="plays")
|
||||
boardgameexpansion: Union["BoardGameExpansion", None] = Relationship(back_populates="plays")
|
||||
#players: list[PlayPlayer] = Relationship(back_populates="play")
|
||||
#boardgame: Union["BoardGame", None] = Relationship(back_populates="plays")
|
||||
#boardgameexpansion: Union["BoardGameExpansion", None] = Relationship(back_populates="plays")
|
||||
|
||||
model_config = {
|
||||
'validate_assignment':True
|
||||
|
|
@ -48,13 +48,13 @@ class PlayPublic(PlayBase):
|
|||
id: int
|
||||
|
||||
|
||||
class PlayPublicWithPlayers(PlayPublic):
|
||||
players: list[PlayPlayerPublic] = []
|
||||
boardgame: Union["BoardGameNoPlays", None]
|
||||
boardgameexpansion: Union["BoardGameExpansionNoPlays", None]
|
||||
# class PlayPublicWithPlayers(PlayPublic):
|
||||
# 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#, BoardGameExpansion, BoardGameNoPlays, BoardGameExpansionNoPlays
|
||||
from src.classes.people_classes import Designer
|
||||
Play.model_rebuild()
|
||||
PlayPublicWithPlayers.model_rebuild()
|
||||
# PlayPublicWithPlayers.model_rebuild()
|
||||
|
|
@ -11,10 +11,7 @@ class NumberStatistic(StatisticBase):
|
|||
result: float
|
||||
|
||||
class GamesStatistic(StatisticBase):
|
||||
result: list[Union[
|
||||
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
||||
boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion,
|
||||
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]]
|
||||
result: list[boardgame_classes.BoardGame]
|
||||
|
||||
model_config = {
|
||||
'validate_assignment':True
|
||||
|
|
|
|||
|
|
@ -2,20 +2,16 @@ from typing import Union
|
|||
|
||||
from src.classes import boardgame_classes
|
||||
|
||||
def filter_expansions_out(to_filter_boardgames: list[Union[
|
||||
list[boardgame_classes.BoardGame], list[boardgame_classes.BoardGameExpansion],
|
||||
list[boardgame_classes.OwnedBoardGame], list[boardgame_classes.OwnedBoardGameExpansion],
|
||||
list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]]):
|
||||
def filter_expansions_out(to_filter_boardgames: list[boardgame_classes.BoardGame]):
|
||||
|
||||
filtered_boardgames = list(filter(lambda x: type(x) in [boardgame_classes.BoardGame, boardgame_classes.OwnedBoardGame, boardgame_classes.WishlistBoardGame], to_filter_boardgames))
|
||||
filtered_boardgames = list(filter(lambda x: type(x) in [boardgame_classes.BoardGame], to_filter_boardgames))
|
||||
|
||||
return filtered_boardgames
|
||||
|
||||
def filter_non_expansions_out(to_filter_boardgames: list[Union[
|
||||
list[boardgame_classes.BoardGame], list[boardgame_classes.BoardGameExpansion],
|
||||
list[boardgame_classes.OwnedBoardGame], list[boardgame_classes.OwnedBoardGameExpansion],
|
||||
list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]]):
|
||||
def filter_non_expansions_out(to_filter_boardgames: list[boardgame_classes.BoardGame]):
|
||||
|
||||
filtered_boardgames = list(filter(lambda x: type(x) in [boardgame_classes.BoardGameExpansion, boardgame_classes.OwnedBoardGameExpansion, boardgame_classes.WishlistBoardGameExpansion], to_filter_boardgames))
|
||||
#filtered_boardgames = list(filter(lambda x: type(x) in [boardgame_classes.BoardGameExpansion, boardgame_classes.OwnedBoardGameExpansion, boardgame_classes.WishlistBoardGameExpansion], to_filter_boardgames))
|
||||
|
||||
filtered_boardgames = to_filter_boardgames
|
||||
|
||||
return filtered_boardgames
|
||||
22
src/main.py
22
src/main.py
|
|
@ -98,12 +98,12 @@ def refresh():
|
|||
return {"Status": "Already refreshing"}
|
||||
|
||||
|
||||
@app.get("/boardgame", response_model=Union[boardgame_classes.BoardGamePublic, boardgame_classes.BoardGameExpansionPublic])
|
||||
@app.get("/boardgame", response_model=boardgame_classes.BoardGamePublic)
|
||||
def get_boardgame_by_id(id: int, session: Session = Depends(get_session)):
|
||||
requested_boardgame = data_connection.get_boardgame(session, id)
|
||||
return requested_boardgame
|
||||
|
||||
@app.get("/owned", response_model=list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]])
|
||||
@app.get("/owned", response_model=list[boardgame_classes.BoardGame])
|
||||
def get_owned_collection(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||
to_return_boardgames = data_connection.get_user_owned_collection(session)
|
||||
|
||||
|
|
@ -113,7 +113,7 @@ def get_owned_collection(query: BoardgameFilterParams = Depends(), session: Sess
|
|||
|
||||
return to_return_boardgames
|
||||
|
||||
@app.get('/collection', response_model=list[Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion]])
|
||||
@app.get('/collection', response_model=list[boardgame_classes.BoardGame])
|
||||
def get_collection(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||
to_return_boardgames = data_connection.get_user_collection(session)
|
||||
|
||||
|
|
@ -123,7 +123,7 @@ def get_collection(query: BoardgameFilterParams = Depends(), session: Session =
|
|||
|
||||
return to_return_boardgames
|
||||
|
||||
@app.get("/wishlist", response_model=list[Union[boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]])
|
||||
@app.get("/wishlist", response_model=list[boardgame_classes.BoardGame])
|
||||
def get_wishlist_collection(priority: int = 0, query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||
|
||||
to_return_boardgames = data_connection.get_user_wishlist_collection(session, priority)
|
||||
|
|
@ -135,17 +135,17 @@ def get_wishlist_collection(priority: int = 0, query: BoardgameFilterParams = De
|
|||
return to_return_boardgames
|
||||
|
||||
|
||||
@app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers])
|
||||
def get_plays(query: PlayFilterParams = Depends(), boardgame_id: int = -1, session: Session = Depends(get_session)):
|
||||
# @app.get("/plays", response_model=list[play_classes.PlayPublicWithPlayers])
|
||||
# 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:
|
||||
requested_plays = play_filters.filter_only_specific_boardgame(boardgame_id, requested_plays)
|
||||
# if boardgame_id > -1:
|
||||
# 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])
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ def url_to_xml_object(url: HttpUrl) -> ET.Element:
|
|||
root = ET.fromstring(r.content)
|
||||
return root
|
||||
|
||||
def get_boardgame(boardgame_id: int) -> Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion]:
|
||||
def get_boardgame(boardgame_id: int) -> boardgame_classes.BoardGame:
|
||||
url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id)
|
||||
boardgame_xml_object : ET.Element = url_to_xml_object(url)
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ def get_multiple_boardgames(boardgame_ids: list[int]) -> list[boardgame_classes.
|
|||
|
||||
|
||||
#Requires single boardgame XML 'item' from bgg api on /thing
|
||||
def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion]:
|
||||
def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> boardgame_classes.BoardGame:
|
||||
|
||||
boardgame_type = boardgame_xml.get('type')
|
||||
|
||||
|
|
@ -122,7 +122,7 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> Union[boardgame_class
|
|||
|
||||
return boardgame
|
||||
|
||||
def convert_collection_xml_to_owned_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]:
|
||||
def convert_collection_xml_to_owned_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.BoardGame:
|
||||
|
||||
boardgame_type = collection_boardgame_xml.get('subtype')
|
||||
|
||||
|
|
@ -164,7 +164,7 @@ def convert_collection_xml_to_owned_boardgame(boardgame_extra_info: boardgame_cl
|
|||
|
||||
return boardgame
|
||||
|
||||
def convert_collection_xml_to_wishlist_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.WishlistBoardGame:
|
||||
def convert_collection_xml_to_wishlist_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.BoardGame:
|
||||
|
||||
boardgame_type = collection_boardgame_xml.get('subtype')
|
||||
|
||||
|
|
@ -280,7 +280,7 @@ def get_boardgames_from_collection_url(collection_url: str, boardgame_type: boar
|
|||
|
||||
return collection_list
|
||||
|
||||
def get_user_collection() -> list[Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion]]:
|
||||
def get_user_collection() -> list[boardgame_classes.BoardGame]:
|
||||
url_no_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
url_only_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&stats=1&subtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
|
||||
|
|
@ -291,7 +291,7 @@ def get_user_collection() -> list[Union[boardgame_classes.BoardGame, boardgame_c
|
|||
|
||||
return boardgames
|
||||
|
||||
def get_user_owned_collection() -> list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]]:
|
||||
def get_user_owned_collection() -> list[boardgame_classes.BoardGame]:
|
||||
url_no_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
url_only_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&subtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
|
||||
|
|
@ -302,7 +302,7 @@ def get_user_owned_collection() -> list[Union[boardgame_classes.OwnedBoardGame,
|
|||
|
||||
return owned_boardgames
|
||||
|
||||
def get_user_wishlist_collection() -> Union[list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]:
|
||||
def get_user_wishlist_collection() -> list[boardgame_classes.BoardGame]:
|
||||
url_no_expanions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&wishlist=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
url_only_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&wishlist=1&stats=1&subtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ from src.classes import boardgame_classes, play_classes
|
|||
def get_db_engine():
|
||||
return db_connection.get_engine()
|
||||
|
||||
def get_boardgame(session: Session, boardgame_id: int) -> Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion]:
|
||||
def get_boardgame(session: Session, boardgame_id: int) -> boardgame_classes.BoardGame:
|
||||
#Will check if it already exists in db, then it will get it from there
|
||||
|
||||
boardgame_in_db = db_connection.get_boardgame(session, boardgame_id=boardgame_id)
|
||||
|
|
@ -21,16 +21,6 @@ def get_boardgame(session: Session, boardgame_id: int) -> Union[boardgame_classe
|
|||
to_return_boardgame = boardgame_in_db
|
||||
else:
|
||||
to_return_boardgame = bgg_connection.get_boardgame(boardgame_id)
|
||||
boardgame_new_linked_designers = []
|
||||
# for designer in to_return_boardgame.designers:
|
||||
# designer_in_db = db_connection.get_designer(session, designer.id)
|
||||
# if designer_in_db == None:
|
||||
# db_connection.add_designer(session, designer)
|
||||
# designer_in_db = db_connection.get_designer(session, designer.id)
|
||||
|
||||
# boardgame_new_linked_designers.append(designer_in_db)
|
||||
|
||||
# to_return_boardgame.designers = boardgame_new_linked_designers
|
||||
db_connection.add_boardgame(session, to_return_boardgame)
|
||||
to_return_boardgame = db_connection.get_boardgame(session, boardgame_id)
|
||||
|
||||
|
|
@ -38,82 +28,59 @@ def get_boardgame(session: Session, boardgame_id: int) -> Union[boardgame_classe
|
|||
return to_return_boardgame
|
||||
|
||||
|
||||
def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> list[Union[boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion]]:
|
||||
def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> list[boardgame_classes.BoardGame]:
|
||||
boardgames_in_db, boardgame_ids_missing = db_connection.get_multiple_boardgames(session, boardgame_ids=boardgame_ids)
|
||||
|
||||
if len(boardgame_ids_missing) != 0:
|
||||
missing_boardgames = bgg_connection.get_multiple_boardgames(boardgame_ids_missing)
|
||||
|
||||
# for boardgame in missing_boardgames:
|
||||
# db_connection.add_multiple_designers(session, boardgame.designers)
|
||||
# boardgame.designers, missing_designer_ids = db_connection.get_multiple_designers(session, [designer.id for designer in boardgame.designers])
|
||||
# assert len(missing_designer_ids) == 0, "This list should be empty"
|
||||
|
||||
# missing_boardgames_linked_people = []
|
||||
|
||||
# for boardgame in missing_boardgames:
|
||||
# boardgame_new_linked_designers = []
|
||||
# for designer in boardgame.designers:
|
||||
# designer_in_db = db_connection.get_designer(session, designer.id)
|
||||
# if designer_in_db == None:
|
||||
# db_connection.add_designer(session, designer)
|
||||
# designer_in_db = db_connection.get_designer(session, designer.id)
|
||||
|
||||
# boardgame_new_linked_designers.append(designer_in_db)
|
||||
|
||||
# boardgame.designers = boardgame_new_linked_designers
|
||||
# session.refresh(boardgame)
|
||||
db_connection.add_multiple_boardgames(session, missing_boardgames)
|
||||
|
||||
boardgames_in_db, boardgame_ids_missing = db_connection.get_multiple_boardgames(session, boardgame_ids=boardgame_ids)
|
||||
|
||||
return boardgames_in_db
|
||||
|
||||
def get_user_collection(session: Session) -> list[Union[boardgame_classes.BoardGame, boardgame_classes.OwnedBoardGame]]:
|
||||
def get_user_collection(session: Session) -> list[boardgame_classes.BoardGame]:
|
||||
boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session, boardgame_classes.BoardGame)
|
||||
boardgame_expansions_from_db: list[boardgame_classes.BoardGameExpansion] = db_connection.get_all_boardgames(session, boardgame_classes.BoardGameExpansion)
|
||||
|
||||
if len(boardgames_from_db) == 0 and len(boardgame_expansions_from_db) == 0:
|
||||
if len(boardgames_from_db) == 0: # and len(boardgame_expansions_from_db) == 0:
|
||||
boardgames = bgg_connection.get_user_collection()
|
||||
|
||||
#db_connection.add_boardgame(session, boardgame)
|
||||
db_connection.add_multiple_boardgames(session, boardgames)
|
||||
|
||||
boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session, boardgame_classes.BoardGame)
|
||||
boardgame_expansions_from_db: list[boardgame_classes.BoardGameExpansion] = db_connection.get_all_boardgames(session, boardgame_classes.BoardGameExpansion)
|
||||
#boardgame_expansions_from_db: list[boardgame_classes.BoardGameExpansion] = db_connection.get_all_boardgames(session, boardgame_classes.BoardGameExpansion)
|
||||
|
||||
return boardgames_from_db + boardgame_expansions_from_db
|
||||
return boardgames_from_db # + boardgame_expansions_from_db
|
||||
|
||||
def get_user_owned_collection(session: Session, ) -> list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]]:
|
||||
def get_user_owned_collection(session: Session, ) -> list[boardgame_classes.BoardGame]:
|
||||
|
||||
owned_boardgames_from_db: list[boardgame_classes.OwnedBoardGame] = db_connection.get_all_boardgames(session, boardgame_classes.OwnedBoardGame)
|
||||
owned_boardgame_expanions_from_db: list[boardgame_classes.OwnedBoardGameExpansion] = db_connection.get_all_boardgames(session, boardgame_classes.OwnedBoardGameExpansion)
|
||||
owned_boardgames_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session, boardgame_classes.OwnedBoardGame)
|
||||
owned_boardgame_expanions_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session, boardgame_classes.BoardGame)
|
||||
|
||||
if len(owned_boardgames_from_db) == 0 and len(owned_boardgame_expanions_from_db) == 0:
|
||||
owned_boardgames = bgg_connection.get_user_owned_collection()
|
||||
db_connection.add_multiple_boardgames(session, owned_boardgames)
|
||||
|
||||
owned_boardgames_from_db: list[boardgame_classes.OwnedBoardGame] = db_connection.get_all_boardgames(session, boardgame_classes.OwnedBoardGame)
|
||||
owned_boardgame_expanions_from_db: list[boardgame_classes.OwnedBoardGameExpansion] = db_connection.get_all_boardgames(session, boardgame_classes.OwnedBoardGameExpansion)
|
||||
owned_boardgame_expanions_from_db: list[boardgame_classes.BoardGame] = db_connection.get_all_boardgames(session, boardgame_classes.BoardGame)
|
||||
|
||||
|
||||
return owned_boardgames_from_db + owned_boardgame_expanions_from_db
|
||||
|
||||
|
||||
|
||||
def get_user_wishlist_collection(session: Session, wishlist_priority: int = 0) -> Union[list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]:
|
||||
def get_user_wishlist_collection(session: Session, wishlist_priority: int = 0) -> list[boardgame_classes.BoardGame]:
|
||||
|
||||
wishlisted_boardgames_from_db = db_connection.get_all_boardgames(session, boardgame_classes.WishlistBoardGame)
|
||||
wishlisted_boardgame_expansions_from_db = db_connection.get_all_boardgames(session, boardgame_classes.WishlistBoardGameExpansion)
|
||||
wishlisted_boardgames_from_db = db_connection.get_all_boardgames(session, boardgame_classes.BoardGame)
|
||||
|
||||
if len(wishlisted_boardgames_from_db) == 0 and len(wishlisted_boardgame_expansions_from_db) == 0:
|
||||
if len(wishlisted_boardgames_from_db) == 0:
|
||||
wishlisted_boardgames = bgg_connection.get_user_wishlist_collection()
|
||||
db_connection.add_multiple_boardgames(session, wishlisted_boardgames)
|
||||
|
||||
wishlisted_boardgames_from_db = db_connection.get_all_boardgames(session, boardgame_classes.WishlistBoardGame)
|
||||
wishlisted_boardgame_expansions_from_db = db_connection.get_all_boardgames(session, boardgame_classes.WishlistBoardGameExpansion)
|
||||
wishlisted_boardgames_from_db = db_connection.get_all_boardgames(session, boardgame_classes.BoardGame)
|
||||
|
||||
to_return_boardgames = wishlisted_boardgames_from_db + wishlisted_boardgame_expansions_from_db
|
||||
to_return_boardgames = wishlisted_boardgames_from_db
|
||||
|
||||
if wishlist_priority != 0:
|
||||
to_return_boardgames = list(filter(lambda game: game.wishlist_priority == wishlist_priority, to_return_boardgames))
|
||||
|
|
|
|||
|
|
@ -16,17 +16,14 @@ engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
|
|||
def get_engine():
|
||||
return engine
|
||||
|
||||
def add_boardgame(session: Session, boardgame: Union[
|
||||
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
||||
boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion,
|
||||
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]):
|
||||
def add_boardgame(session: Session, boardgame: boardgame_classes.BoardGame):
|
||||
|
||||
with critical_function_lock:
|
||||
boardgame_designers = boardgame.designers
|
||||
for designer_index in range(len(boardgame_designers)):
|
||||
designer_in_db = get_designer(session, boardgame_designers[designer_index].id)
|
||||
if designer_in_db != None:
|
||||
boardgame.designers[designer_index] = designer_in_db
|
||||
# boardgame_designers = boardgame.designers
|
||||
# for designer_index in range(len(boardgame_designers)):
|
||||
# designer_in_db = get_designer(session, boardgame_designers[designer_index].id)
|
||||
# if designer_in_db != None:
|
||||
# boardgame.designers[designer_index] = designer_in_db
|
||||
|
||||
is_boardgame_present = len(session.exec(
|
||||
select(boardgame.__class__).where(boardgame.__class__.id == boardgame.id)
|
||||
|
|
@ -37,10 +34,7 @@ def add_boardgame(session: Session, boardgame: Union[
|
|||
session.commit()
|
||||
session.refresh(boardgame)
|
||||
|
||||
def add_multiple_boardgames(session: Session, boardgame_list: list[Union[
|
||||
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion,
|
||||
boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion,
|
||||
boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]]):
|
||||
def add_multiple_boardgames(session: Session, boardgame_list: list[boardgame_classes.BoardGame]):
|
||||
|
||||
with critical_function_lock:
|
||||
altered_boardgames = []
|
||||
|
|
@ -85,18 +79,17 @@ def get_multiple_designers(session: Session, designer_ids: list[int]) -> list[pe
|
|||
|
||||
return designers, missing_designer_ids
|
||||
|
||||
def get_boardgame(session: Session, boardgame_id: int) -> Union[
|
||||
boardgame_classes.BoardGame, boardgame_classes.BoardGameExpansion]:
|
||||
def get_boardgame(session: Session, boardgame_id: int) -> boardgame_classes.BoardGame:
|
||||
|
||||
statement = select(boardgame_classes.BoardGame).where(boardgame_classes.BoardGame.id == boardgame_id)
|
||||
|
||||
base_boardgames = session.exec(statement).all()
|
||||
|
||||
statement = select(boardgame_classes.BoardGameExpansion).where(boardgame_classes.BoardGameExpansion.id == boardgame_id)
|
||||
# statement = select(boardgame_classes.BoardGameExpansion).where(boardgame_classes.BoardGameExpansion.id == boardgame_id)
|
||||
|
||||
expansion_boardgames = session.exec(statement).all()
|
||||
# expansion_boardgames = session.exec(statement).all()
|
||||
|
||||
returned_boardgames = base_boardgames + expansion_boardgames
|
||||
returned_boardgames = base_boardgames # + expansion_boardgames
|
||||
|
||||
if len(returned_boardgames) == 0:
|
||||
boardgame = None
|
||||
|
|
@ -105,8 +98,7 @@ def get_boardgame(session: Session, boardgame_id: int) -> Union[
|
|||
|
||||
return boardgame
|
||||
|
||||
def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> tuple[Union[
|
||||
list[boardgame_classes.BoardGame], list[boardgame_classes.BoardGameExpansion]], list[int]]:
|
||||
def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> tuple[list[boardgame_classes.BoardGame], list[int]]:
|
||||
statement = select(boardgame_classes.BoardGame).where(boardgame_classes.BoardGame.id.in_(boardgame_ids))
|
||||
results = session.exec(statement)
|
||||
|
||||
|
|
@ -123,10 +115,7 @@ def get_multiple_boardgames(session: Session, boardgame_ids: list[int]) -> tuple
|
|||
|
||||
return boardgames, missing_boardgame_ids
|
||||
|
||||
def get_all_boardgames(session: Session, boardgame_type: SQLModel) -> Union[
|
||||
list[boardgame_classes.BoardGame], list[boardgame_classes.BoardGameExpansion],
|
||||
list[boardgame_classes.OwnedBoardGame], list[boardgame_classes.OwnedBoardGameExpansion],
|
||||
list[boardgame_classes.WishlistBoardGame], list[boardgame_classes.WishlistBoardGameExpansion]]:
|
||||
def get_all_boardgames(session: Session, boardgame_type: SQLModel) -> list[boardgame_classes.BoardGame]:
|
||||
|
||||
statement = select(boardgame_type)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue