Made generic params for boardgame and play filtering
This commit is contained in:
parent
92849b898b
commit
6992b1a6b3
1 changed files with 33 additions and 47 deletions
80
src/main.py
80
src/main.py
|
|
@ -49,18 +49,32 @@ app.add_middleware(
|
||||||
)
|
)
|
||||||
|
|
||||||
#expansion filtering parameters
|
#expansion filtering parameters
|
||||||
class ExpansionFilteringParams(BaseModel):
|
class BoardgameFilterParams(BaseModel):
|
||||||
filter_expansions_out: bool = False
|
filter_expansions_out: bool = False
|
||||||
only_expansions: bool = False
|
only_expansions: bool = False
|
||||||
|
|
||||||
def do_filtering(self,boardgame_list):
|
def do_filtering(self,boardgame_list):
|
||||||
if self.filter_expansions_out:
|
if self.filter_expansions_out:
|
||||||
to_return_boardgames = boardgame_filters.filter_expansions_out(boardgame_list)
|
boardgame_list = boardgame_filters.filter_expansions_out(boardgame_list)
|
||||||
|
|
||||||
if self.only_expansions:
|
if self.only_expansions:
|
||||||
to_return_boardgames = boardgame_filters.filter_non_expansions_out(boardgame_list)
|
boardgame_list = boardgame_filters.filter_non_expansions_out(boardgame_list)
|
||||||
|
|
||||||
|
return boardgame_list
|
||||||
|
|
||||||
|
class PlayFilterParams(BaseModel):
|
||||||
|
filter_expansions_out: bool = False
|
||||||
|
only_expansions: bool = False
|
||||||
|
|
||||||
|
def do_filtering(self, play_list):
|
||||||
|
if self.filter_expansions_out:
|
||||||
|
play_list = play_filters.filter_expansions_out(play_list)
|
||||||
|
|
||||||
|
if self.only_expansions:
|
||||||
|
play_list = play_filters.filter_non_expansions_out(play_list)
|
||||||
|
|
||||||
|
return play_list
|
||||||
|
|
||||||
return to_return_boardgames
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def read_root():
|
def read_root():
|
||||||
|
|
@ -72,42 +86,30 @@ def get_boardgame_by_id(id: int, session: Session = Depends(get_session)):
|
||||||
return requested_boardgame
|
return requested_boardgame
|
||||||
|
|
||||||
@app.get("/owned", response_model=list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]])
|
@app.get("/owned", response_model=list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]])
|
||||||
def get_owned_collection(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_session)):
|
def get_owned_collection(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||||
to_return_boardgames = data_connection.get_user_owned_collection(session)
|
to_return_boardgames = data_connection.get_user_owned_collection(session)
|
||||||
|
|
||||||
if query.filter_expansions_out:
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
to_return_boardgames = boardgame_filters.filter_expansions_out(to_return_boardgames)
|
|
||||||
|
|
||||||
if query.only_expansions:
|
|
||||||
to_return_boardgames = boardgame_filters.filter_non_expansions_out(to_return_boardgames)
|
|
||||||
|
|
||||||
return to_return_boardgames
|
return to_return_boardgames
|
||||||
|
|
||||||
|
|
||||||
@app.get("/wishlist", response_model=list[Union[boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]])
|
@app.get("/wishlist", response_model=list[Union[boardgame_classes.WishlistBoardGame, boardgame_classes.WishlistBoardGameExpansion]])
|
||||||
def get_wishlist_collection(priority: int = 0, query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_session)):
|
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)
|
to_return_boardgames = data_connection.get_user_wishlist_collection(session, priority)
|
||||||
|
|
||||||
if query.filter_expansions_out:
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
to_return_boardgames = boardgame_filters.filter_expansions_out(to_return_boardgames)
|
|
||||||
|
|
||||||
if query.only_expansions:
|
|
||||||
to_return_boardgames = boardgame_filters.filter_non_expansions_out(to_return_boardgames)
|
|
||||||
|
|
||||||
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.PlayPublicWithPlayers])
|
||||||
def get_plays(query: ExpansionFilteringParams = 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)
|
||||||
|
|
||||||
if query.filter_expansions_out:
|
requested_plays = query.do_filtering(requested_plays)
|
||||||
requested_plays = play_filters.filter_expansions_out(requested_plays)
|
|
||||||
|
|
||||||
if query.only_expansions:
|
|
||||||
requested_plays = play_filters.filter_non_expansions_out(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)
|
||||||
|
|
@ -122,14 +124,11 @@ def get_players_from_play(play_id: int, session: Session = Depends(get_session))
|
||||||
return requested_players
|
return requested_players
|
||||||
|
|
||||||
@app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic)
|
@app.get('/statistics/amount_of_games', response_model=statistic_classes.NumberStatistic)
|
||||||
def get_amount_of_games(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_session)):
|
def get_amount_of_games(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||||
|
|
||||||
owned_collection = data_connection.get_user_owned_collection(session)
|
owned_collection = data_connection.get_user_owned_collection(session)
|
||||||
|
|
||||||
if query.filter_expansions_out:
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
owned_collection = boardgame_filters.filter_expansions_out(owned_collection)
|
|
||||||
if query.only_expansions:
|
|
||||||
owned_collection = boardgame_filters.filter_non_expansions_out(owned_collection)
|
|
||||||
|
|
||||||
statistic_dict = {
|
statistic_dict = {
|
||||||
"name":"Amount of games in owned collection",
|
"name":"Amount of games in owned collection",
|
||||||
|
|
@ -141,7 +140,7 @@ def get_amount_of_games(query: ExpansionFilteringParams = Depends(), session: Se
|
||||||
return statistic_to_return
|
return statistic_to_return
|
||||||
|
|
||||||
@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic)
|
@app.get('/statistics/amount_of_games_over_time', response_model=statistic_classes.TimeLineStatistic)
|
||||||
def get_amount_of_games_over_time(day_step: int = 1, filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_session)):
|
def get_amount_of_games_over_time(query: BoardgameFilterParams = Depends(), day_step: int = 1, filter_expansions_out: bool = False, only_expansions: bool = False, session: Session = Depends(get_session)):
|
||||||
|
|
||||||
def daterange(start_date: date, end_date: date, day_step):
|
def daterange(start_date: date, end_date: date, day_step):
|
||||||
days = int((end_date - start_date).days)
|
days = int((end_date - start_date).days)
|
||||||
|
|
@ -153,11 +152,7 @@ def get_amount_of_games_over_time(day_step: int = 1, filter_expansions_out: bool
|
||||||
|
|
||||||
start_date = games_in_owned_collection[0].acquisition_date
|
start_date = games_in_owned_collection[0].acquisition_date
|
||||||
|
|
||||||
if filter_expansions_out:
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
games_in_owned_collection = boardgame_filters.filter_expansions_out(games_in_owned_collection)
|
|
||||||
|
|
||||||
if only_expansions:
|
|
||||||
games_in_owned_collection = boardgame_filters.filter_non_expansions_out(games_in_owned_collection)
|
|
||||||
|
|
||||||
timeline_dict = {}
|
timeline_dict = {}
|
||||||
|
|
||||||
|
|
@ -175,17 +170,12 @@ def get_amount_of_games_over_time(day_step: int = 1, filter_expansions_out: bool
|
||||||
return statistic_to_return
|
return statistic_to_return
|
||||||
|
|
||||||
@app.get('/statistics/games_played_per_year', response_model=statistic_classes.TimeLineStatistic)
|
@app.get('/statistics/games_played_per_year', response_model=statistic_classes.TimeLineStatistic)
|
||||||
def get_amount_of_games_played_per_year(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_session)):
|
def get_amount_of_games_played_per_year(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||||
all_plays = data_connection.get_plays(session)
|
all_plays = data_connection.get_plays(session)
|
||||||
|
|
||||||
all_plays.sort(key= lambda x: x.play_date)
|
all_plays.sort(key= lambda x: x.play_date)
|
||||||
|
|
||||||
if query.filter_expansions_out:
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
all_plays = play_filters.filter_expansions_out(all_plays)
|
|
||||||
|
|
||||||
if query.only_expansions:
|
|
||||||
all_plays = play_filters.filter_non_expansions_out(all_plays)
|
|
||||||
|
|
||||||
|
|
||||||
all_played_boardgame_ids = []
|
all_played_boardgame_ids = []
|
||||||
|
|
||||||
|
|
@ -215,15 +205,11 @@ def get_amount_of_games_played_per_year(query: ExpansionFilteringParams = Depend
|
||||||
|
|
||||||
|
|
||||||
@app.get('/statistics/most_expensive_games', response_model=statistic_classes.GamesStatistic)
|
@app.get('/statistics/most_expensive_games', response_model=statistic_classes.GamesStatistic)
|
||||||
def get_most_expensive_game(query: ExpansionFilteringParams = Depends(), top_amount: int = 10, session: Session = Depends(get_session)):
|
def get_most_expensive_game(query: BoardgameFilterParams = Depends(), top_amount: int = 10, session: Session = Depends(get_session)):
|
||||||
|
|
||||||
most_expensive_games: list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]] = data_connection.get_user_owned_collection(session)
|
most_expensive_games: list[Union[boardgame_classes.OwnedBoardGame, boardgame_classes.OwnedBoardGameExpansion]] = data_connection.get_user_owned_collection(session)
|
||||||
|
|
||||||
if query.filter_expansions_out:
|
to_return_boardgames = query.do_filtering(to_return_boardgames)
|
||||||
most_expensive_games = boardgame_filters.filter_expansions_out(most_expensive_games)
|
|
||||||
|
|
||||||
if query.only_expansions:
|
|
||||||
most_expensive_games = boardgame_filters.filter_non_expansions_out(most_expensive_games)
|
|
||||||
|
|
||||||
most_expensive_games.sort(key=lambda x: x.price_paid, reverse=True)
|
most_expensive_games.sort(key=lambda x: x.price_paid, reverse=True)
|
||||||
|
|
||||||
|
|
@ -239,5 +225,5 @@ def get_most_expensive_game(query: ExpansionFilteringParams = Depends(), top_amo
|
||||||
return statistic_to_return
|
return statistic_to_return
|
||||||
|
|
||||||
@app.get('/statistics/shelf_of_shame', response_model=statistic_classes.GamesStatistic)
|
@app.get('/statistics/shelf_of_shame', response_model=statistic_classes.GamesStatistic)
|
||||||
def get_shelf_of_shame(query: ExpansionFilteringParams = Depends(), session: Session = Depends(get_session)):
|
def get_shelf_of_shame(query: BoardgameFilterParams = Depends(), session: Session = Depends(get_session)):
|
||||||
pass
|
pass
|
||||||
Loading…
Reference in a new issue