Fixed wishlist endpoint. Application no longer assumes wishlist board games are owned

This commit is contained in:
Yarne Coppens 2024-08-02 10:50:18 +02:00
parent 5b87e45523
commit 1036205acc
4 changed files with 68 additions and 19 deletions

View file

@ -5,8 +5,8 @@ from enum import Enum
class BoardgameType(Enum):
BOARDGAME = 'boardgame'
BOARDGAMEEXPANSION = 'boardgameexpansion'
COLLECTIONBOARDGAME = 'collectionboardgame'
COLLECTIONBOARDGAMEEXPANSION = 'collectionboardgameexpansion'
OWNEDBOARDGAME = 'ownedboardgame'
OWNEDBOARDGAMEEXPANSION = 'ownedboardgameexpansion'
WISHLISTBOARDGAME = 'wishlistboardgame'
WISHLISTBOARDGAMEEXPANSION = 'wishlistboardgameexpansion'
@ -33,10 +33,10 @@ class OwnedBoardGame(BoardGame):
price_paid: float
acquisition_date: date
acquired_from: str
type: BoardgameType = BoardgameType.COLLECTIONBOARDGAME
type: BoardgameType = BoardgameType.OWNEDBOARDGAME
class OwnedBoardGameExpansion(OwnedBoardGame):
type: BoardgameType = BoardgameType.COLLECTIONBOARDGAMEEXPANSION
type: BoardgameType = BoardgameType.OWNEDBOARDGAMEEXPANSION
class WishlistBoardGame(BoardGame):
wishlist_priority: int

View file

@ -16,9 +16,9 @@ def get_boardgame_by_id(boardgame_id: int):
requested_boardgame: boardgame_classes.BoardGame = bgg_connection.get_boardgame(boardgame_id)
return requested_boardgame
@app.get("/collection", response_model=list[boardgame_classes.CollectionBoardGame])
@app.get("/collection", response_model=list[boardgame_classes.OwnedBoardGame])
def get_owned_collection():
requested_collection: list[boardgame_classes.CollectionBoardGame] = bgg_connection.get_user_owned_collection()
requested_collection: list[boardgame_classes.OwnedBoardGame] = bgg_connection.get_user_owned_collection()
return requested_collection

View file

@ -93,7 +93,7 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> boardgame_classes.Boa
return boardgame
def convert_collection_xml_to_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.BoardGame:
def convert_collection_xml_to_owned_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.OwnedBoardGame:
boardgame_type = collection_boardgame_xml.get('subtype')
@ -113,7 +113,7 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: boardgame_classes.
acquired_from = collection_boardgame_xml.find('privateinfo').get('acquiredfrom')
collection_boardgame_dict = {
owned_boardgame_dict = {
"price_paid" : price_paid,
"acquisition_date" : acquisition_date,
"acquired_from" : acquired_from
@ -132,21 +132,57 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: boardgame_classes.
"max_playing_time" : boardgame_extra_info.max_playing_time,
"min_age" : boardgame_extra_info.min_age,
"all_expansion_ids" : boardgame_extra_info.all_expansion_ids,
**collection_boardgame_dict
**owned_boardgame_dict
}
match boardgame_type:
case "boardgame":
boardgame = boardgame_classes.CollectionBoardGame(**boardgame_dict)
boardgame = boardgame_classes.OwnedBoardGame(**boardgame_dict)
case "boardgameexpansion":
boardgame = boardgame_classes.CollectionBoardGameExpansion(**boardgame_dict)
boardgame = boardgame_classes.OwnedBoardGameExpansion(**boardgame_dict)
return boardgame
def convert_collection_xml_to_wishlist_boardgame(boardgame_extra_info: boardgame_classes.BoardGame, collection_boardgame_xml: ET.Element) -> boardgame_classes.WishlistBoardGame:
boardgame_type = collection_boardgame_xml.get('subtype')
wishlist_priority = collection_boardgame_xml.find('status').get('wishlistpriority')
wishlist_boardgame_dict = {
"wishlist_priority" : wishlist_priority,
}
boardgame_dict = {
"id" : boardgame_extra_info.id,
"name" : boardgame_extra_info.name,
"description" : boardgame_extra_info.description,
"image_url" : boardgame_extra_info.image_url,
"thumbnail_url" : boardgame_extra_info.thumbnail_url,
"year_published" : boardgame_extra_info.year_published,
"min_players" : boardgame_extra_info.min_players,
"max_players" : boardgame_extra_info.max_players,
"min_playing_time" : boardgame_extra_info.min_playing_time,
"max_playing_time" : boardgame_extra_info.max_playing_time,
"min_age" : boardgame_extra_info.min_age,
"all_expansion_ids" : boardgame_extra_info.all_expansion_ids,
**wishlist_boardgame_dict
}
match boardgame_type:
case "boardgame":
boardgame = boardgame_classes.WishlistBoardGame(**boardgame_dict)
case "boardgameexpansion":
boardgame = boardgame_classes.WishlistBoardGameExpansion(**boardgame_dict)
return boardgame
#Creates list of board games from a collection '/collection' URL
def get_boardgames_from_collection_url(collection_url: str) -> list[boardgame_classes.BoardGame]:
def get_boardgames_from_collection_url(collection_url: str, boardgame_type: boardgame_classes.BoardgameType) -> list[boardgame_classes.BoardGame]:
collection_xml = url_to_xml_object(collection_url)
collection_list: list[boardgame_classes.BoardGame] = []
@ -166,7 +202,15 @@ def get_boardgames_from_collection_url(collection_url: str) -> list[boardgame_cl
for boardgame_item in collection_xml:
boardgame_extra = boardgame_extras[current_index]
boardgame = convert_collection_xml_to_boardgame(boardgame_extra, boardgame_item)
match boardgame_type:
case boardgame_classes.BoardgameType.OWNEDBOARDGAME:
boardgame = convert_collection_xml_to_owned_boardgame(boardgame_extra, boardgame_item)
case boardgame_classes.BoardgameType.OWNEDBOARDGAMEEXPANSION:
boardgame = convert_collection_xml_to_owned_boardgame(boardgame_extra, boardgame_item)
case boardgame_classes.BoardgameType.WISHLISTBOARDGAME:
boardgame = convert_collection_xml_to_wishlist_boardgame(boardgame_extra, boardgame_item)
case boardgame_classes.BoardgameType.WISHLISTBOARDGAMEEXPANSION:
boardgame = convert_collection_xml_to_wishlist_boardgame(boardgame_extra, boardgame_item)
collection_list.append(boardgame)
current_index += 1
@ -174,18 +218,23 @@ def get_boardgames_from_collection_url(collection_url: str) -> list[boardgame_cl
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)
owned_boardgames = get_boardgames_from_collection_url(url_no_expansions)
url_only_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&subtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
owned_boardgame_expansions = get_boardgames_from_collection_url(url_only_expansions)
owned_boardgames = get_boardgames_from_collection_url(url_no_expansions, boardgame_classes.BoardgameType.OWNEDBOARDGAME)
owned_boardgame_expansions = get_boardgames_from_collection_url(url_only_expansions, boardgame_classes.BoardgameType.OWNEDBOARDGAMEEXPANSION)
owned_boardgames += owned_boardgame_expansions
return owned_boardgames
def get_user_wishlist_collection() -> list[boardgame_classes.BoardGame]:
url = 'https://boardgamegeek.com/xmlapi2/collection?username={}&wishlist=1&stats=1&showprivate=1&version=1'.format(auth_manager.username)
wishlisted_boardgames = get_boardgames_from_collection_url(url)
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)
wishlisted_boardgames = get_boardgames_from_collection_url(url_no_expanions, boardgame_classes.BoardgameType.WISHLISTBOARDGAME)
wishlisted_boardgame_expansions = get_boardgames_from_collection_url(url_only_expansions, boardgame_classes.BoardgameType.WISHLISTBOARDGAMEEXPANSION)
wishlisted_boardgames += wishlisted_boardgame_expansions
return wishlisted_boardgames

View file

@ -25,7 +25,7 @@ def test_retrieve_collection():
response = client.get("/collection")
assert response.status_code == 200
returned_boardgame = boardgame_classes.CollectionBoardGame(**response.json()[0])
returned_boardgame = boardgame_classes.OwnedBoardGame(**response.json()[0])
assert type(returned_boardgame.id) == int
assert type(returned_boardgame.name) == str