Added preordered games endpoint
This commit is contained in:
parent
18fb6a38b3
commit
fb5995c88f
5 changed files with 103 additions and 0 deletions
|
|
@ -12,6 +12,8 @@ class BoardgameType(Enum):
|
||||||
OWNEDBOARDGAMEEXPANSION = 'ownedboardgameexpansion'
|
OWNEDBOARDGAMEEXPANSION = 'ownedboardgameexpansion'
|
||||||
WISHLISTBOARDGAME = 'wishlistboardgame'
|
WISHLISTBOARDGAME = 'wishlistboardgame'
|
||||||
WISHLISTBOARDGAMEEXPANSION = 'wishlistboardgameexpansion'
|
WISHLISTBOARDGAMEEXPANSION = 'wishlistboardgameexpansion'
|
||||||
|
PREORDEREDBOARDGAME = 'preorderedboargame'
|
||||||
|
PREORDEREDBOARDGAMEEXPANSION = 'preorderedboargameexpansion'
|
||||||
|
|
||||||
|
|
||||||
class BoardGameBase(SQLModel):
|
class BoardGameBase(SQLModel):
|
||||||
|
|
@ -43,6 +45,8 @@ class BoardGame(BoardGameBase, table=True):
|
||||||
|
|
||||||
wishlist_info: Optional["WishlistInfo"] = Relationship(back_populates="boardgame")
|
wishlist_info: Optional["WishlistInfo"] = Relationship(back_populates="boardgame")
|
||||||
|
|
||||||
|
preordered_info: Optional["PreorderedInfo"] = Relationship(back_populates="boardgame")
|
||||||
|
|
||||||
plays: list["Play"] = Relationship(back_populates='boardgame')
|
plays: list["Play"] = Relationship(back_populates='boardgame')
|
||||||
|
|
||||||
model_config = {
|
model_config = {
|
||||||
|
|
@ -56,6 +60,7 @@ 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"]
|
||||||
|
preordered_info: Optional["PreorderedInfoPublicNoGame"]
|
||||||
plays: list["PlayPublicNoGame"]
|
plays: list["PlayPublicNoGame"]
|
||||||
|
|
||||||
class BoardGamePublicNoPlays(BoardGameBase):
|
class BoardGamePublicNoPlays(BoardGameBase):
|
||||||
|
|
@ -65,6 +70,7 @@ class BoardGamePublicNoPlays(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"]
|
||||||
|
preordered_info: Optional["PreorderedInfoPublicNoGame"]
|
||||||
|
|
||||||
|
|
||||||
class ExpansionInfo(SQLModel, table=True):
|
class ExpansionInfo(SQLModel, table=True):
|
||||||
|
|
@ -129,6 +135,24 @@ class WishlistInfoPublicNoGame(SQLModel):
|
||||||
wishlist_priority: int
|
wishlist_priority: int
|
||||||
|
|
||||||
|
|
||||||
|
class PreorderedInfo(SQLModel, table=True):
|
||||||
|
id: Optional[int] = Field(default=None, primary_key=True)
|
||||||
|
arrival_date: date
|
||||||
|
|
||||||
|
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="preordered_info"
|
||||||
|
)
|
||||||
|
|
||||||
|
class PreorderedInfoPublic(SQLModel):
|
||||||
|
arrival_date: date
|
||||||
|
boardgame: BoardGame
|
||||||
|
|
||||||
|
class PreorderedInfoPublicNoGame(SQLModel):
|
||||||
|
arrival_date: date
|
||||||
|
|
||||||
from src.classes.play_classes import Play, PlayPublicNoGame
|
from src.classes.play_classes import Play, PlayPublicNoGame
|
||||||
from src.classes.people_classes import Designer, DesignerPublicNoGames, Artist, ArtistPublicNoGames
|
from src.classes.people_classes import Designer, DesignerPublicNoGames, Artist, ArtistPublicNoGames
|
||||||
BoardGame.model_rebuild()
|
BoardGame.model_rebuild()
|
||||||
12
src/main.py
12
src/main.py
|
|
@ -29,6 +29,7 @@ def refresh_data():
|
||||||
data_connection.get_user_collection(session)
|
data_connection.get_user_collection(session)
|
||||||
data_connection.get_user_owned_collection(session)
|
data_connection.get_user_owned_collection(session)
|
||||||
data_connection.get_user_wishlist_collection(session)
|
data_connection.get_user_wishlist_collection(session)
|
||||||
|
data_connection.get_user_preordered_collection(session)
|
||||||
data_connection.get_plays(session)
|
data_connection.get_plays(session)
|
||||||
|
|
||||||
print('DONE')
|
print('DONE')
|
||||||
|
|
@ -142,6 +143,17 @@ def get_wishlist_collection(priority: int = 0, query: BoardgameFilterParams = De
|
||||||
|
|
||||||
return to_return_boardgames
|
return to_return_boardgames
|
||||||
|
|
||||||
|
@app.get("/preordered", response_model=list[boardgame_classes.BoardGamePublicNoPlays])
|
||||||
|
def get_preordered_collection(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||||
|
|
||||||
|
to_return_boardgames = data_connection.get_user_preordered_collection(session)
|
||||||
|
|
||||||
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
|
|
||||||
|
to_return_boardgames.sort(key=lambda x: x.name)
|
||||||
|
|
||||||
|
return to_return_boardgames
|
||||||
|
|
||||||
|
|
||||||
@app.get("/plays", response_model=list[play_classes.PlayPublic])
|
@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)):
|
||||||
|
|
|
||||||
|
|
@ -196,6 +196,35 @@ def convert_collection_xml_to_wishlist_boardgame(boardgame_extra_info: boardgame
|
||||||
|
|
||||||
return boardgame
|
return boardgame
|
||||||
|
|
||||||
|
def convert_collection_xml_to_preordered_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.BoardGame:
|
||||||
|
|
||||||
|
date_string = collection_boardgame_xml.find('privateinfo').get('acquisitiondate')
|
||||||
|
if date_string == '':
|
||||||
|
date_string = '2020-01-01'
|
||||||
|
|
||||||
|
acquisition_date = datetime.strptime(date_string, '%Y-%m-%d').date()
|
||||||
|
|
||||||
|
preordered_boardgame_dict = {
|
||||||
|
"arrival_date" : acquisition_date
|
||||||
|
}
|
||||||
|
|
||||||
|
preordered_info = boardgame_classes.PreorderedInfo.model_validate(preordered_boardgame_dict)
|
||||||
|
|
||||||
|
boardgame_extra_info.preordered_info = preordered_info
|
||||||
|
|
||||||
|
boardgame_version_info = collection_boardgame_xml.find('version')
|
||||||
|
|
||||||
|
if boardgame_version_info != None:
|
||||||
|
boardgame_extra_info_item = boardgame_version_info.find('item')
|
||||||
|
boardgame_extra_info.name = collection_boardgame_xml.find('name').text if boardgame_extra_info_item.find('name') != None else boardgame_extra_info.name
|
||||||
|
boardgame_extra_info.image_url = boardgame_extra_info_item.find('image').text if boardgame_extra_info_item.find('image') != None else boardgame_extra_info.image_url
|
||||||
|
boardgame_extra_info.thumbnail_url = boardgame_extra_info_item.find('thumbnail').text if boardgame_extra_info_item.find('thumbnail') != None else boardgame_extra_info.thumbnail_url
|
||||||
|
boardgame_extra_info.year_published = boardgame_extra_info_item.find('yearpublished').get('value') if boardgame_extra_info_item.find('yearpublished') != None else boardgame_extra_info.year_published
|
||||||
|
|
||||||
|
boardgame = boardgame_extra_info
|
||||||
|
|
||||||
|
return boardgame
|
||||||
|
|
||||||
def convert_playplayer_xml_to_playplayer(playplayer_xml: ET.Element) -> play_classes.PlayPlayer:
|
def convert_playplayer_xml_to_playplayer(playplayer_xml: ET.Element) -> play_classes.PlayPlayer:
|
||||||
|
|
||||||
score = playplayer_xml.get('score')
|
score = playplayer_xml.get('score')
|
||||||
|
|
@ -280,6 +309,10 @@ def get_boardgames_from_collection_url(collection_url: str, boardgame_type: boar
|
||||||
boardgame = convert_collection_xml_to_wishlist_boardgame(boardgame_extra, boardgame_item)
|
boardgame = convert_collection_xml_to_wishlist_boardgame(boardgame_extra, boardgame_item)
|
||||||
case boardgame_classes.BoardgameType.WISHLISTBOARDGAMEEXPANSION:
|
case boardgame_classes.BoardgameType.WISHLISTBOARDGAMEEXPANSION:
|
||||||
boardgame = convert_collection_xml_to_wishlist_boardgame(boardgame_extra, boardgame_item)
|
boardgame = convert_collection_xml_to_wishlist_boardgame(boardgame_extra, boardgame_item)
|
||||||
|
case boardgame_classes.BoardgameType.PREORDEREDBOARDGAME:
|
||||||
|
boardgame = convert_collection_xml_to_preordered_boardgame(boardgame_extra, boardgame_item)
|
||||||
|
case boardgame_classes.BoardgameType.PREORDEREDBOARDGAMEEXPANSION:
|
||||||
|
boardgame = convert_collection_xml_to_preordered_boardgame(boardgame_extra, boardgame_item)
|
||||||
boardgame.type = boardgame_type
|
boardgame.type = boardgame_type
|
||||||
collection_list.append(boardgame)
|
collection_list.append(boardgame)
|
||||||
current_index += 1
|
current_index += 1
|
||||||
|
|
@ -320,6 +353,18 @@ def get_user_wishlist_collection() -> list[boardgame_classes.BoardGame]:
|
||||||
|
|
||||||
return wishlisted_boardgames
|
return wishlisted_boardgames
|
||||||
|
|
||||||
|
def get_user_preordered_collection() -> list[boardgame_classes.BoardGame]:
|
||||||
|
url_no_expanions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&preordered=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||||
|
url_only_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&preordered=1&stats=1&subtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
|
||||||
|
|
||||||
|
preordered_boardgames = get_boardgames_from_collection_url(url_no_expanions, boardgame_classes.BoardgameType.PREORDEREDBOARDGAME)
|
||||||
|
preordered_boardgame_expansions = get_boardgames_from_collection_url(url_only_expansions, boardgame_classes.BoardgameType.PREORDEREDBOARDGAMEEXPANSION)
|
||||||
|
|
||||||
|
preordered_boardgames += preordered_boardgame_expansions
|
||||||
|
|
||||||
|
|
||||||
|
return preordered_boardgames
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_plays() -> list[play_classes.Play]:
|
def get_plays() -> list[play_classes.Play]:
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,19 @@ def get_user_wishlist_collection(session: Session, wishlist_priority: int = 0) -
|
||||||
wishlisted_boardgames_from_db = list(filter(lambda game: game.wishlist_info.wishlist_priority == wishlist_priority, wishlisted_boardgames_from_db))
|
wishlisted_boardgames_from_db = list(filter(lambda game: game.wishlist_info.wishlist_priority == wishlist_priority, wishlisted_boardgames_from_db))
|
||||||
|
|
||||||
return wishlisted_boardgames_from_db
|
return wishlisted_boardgames_from_db
|
||||||
|
|
||||||
|
def get_user_preordered_collection(session: Session) -> list[boardgame_classes.BoardGame]:
|
||||||
|
with critical_function_lock:
|
||||||
|
preordered_boardgames_from_db = db_connection.get_preordered_boardgames(session)
|
||||||
|
|
||||||
|
if len(preordered_boardgames_from_db) == 0:
|
||||||
|
preordered_boardgames = bgg_connection.get_user_preordered_collection()
|
||||||
|
db_connection.upsert_multiple_boardgames(session, preordered_boardgames)
|
||||||
|
|
||||||
|
preordered_boardgames_from_db = db_connection.get_preordered_boardgames(session)
|
||||||
|
|
||||||
|
|
||||||
|
return preordered_boardgames_from_db
|
||||||
|
|
||||||
|
|
||||||
def get_plays(session: Session) -> list[play_classes.Play]:
|
def get_plays(session: Session) -> list[play_classes.Play]:
|
||||||
|
|
|
||||||
|
|
@ -218,6 +218,15 @@ def get_wishlist_boardgames(session: Session) -> list[boardgame_classes.BoardGam
|
||||||
return wishlisted_boardgames
|
return wishlisted_boardgames
|
||||||
|
|
||||||
|
|
||||||
|
def get_preordered_boardgames(session: Session) -> list[boardgame_classes.BoardGame]:
|
||||||
|
statement = select(boardgame_classes.PreorderedInfo)
|
||||||
|
results = session.exec(statement)
|
||||||
|
|
||||||
|
preordered_boardgames = [preordered_info.boardgame for preordered_info in results.all()]
|
||||||
|
|
||||||
|
return preordered_boardgames
|
||||||
|
|
||||||
|
|
||||||
def add_play(session: Session, play: play_classes.Play):
|
def add_play(session: Session, play: play_classes.Play):
|
||||||
|
|
||||||
with critical_function_lock:
|
with critical_function_lock:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue