diff --git a/app/routers_v2/stratgame.py b/app/routers_v2/stratgame.py index 1a3fa7b..73f44cf 100644 --- a/app/routers_v2/stratgame.py +++ b/app/routers_v2/stratgame.py @@ -8,10 +8,7 @@ from ..db_engine import StratGame, model_to_dict, fn from ..dependencies import oauth2_scheme, valid_token -router = APIRouter( - prefix='/api/v2/games', - tags=['games'] -) +router = APIRouter(prefix="/api/v2/games", tags=["games"]) class GameModel(pydantic.BaseModel): @@ -35,13 +32,22 @@ class GameList(pydantic.BaseModel): games: List[GameModel] -@router.get('') +@router.get("") async def get_games( - season: list = Query(default=None), forfeit: Optional[bool] = None, away_team_id: list = Query(default=None), - home_team_id: list = Query(default=None), team1_id: list = Query(default=None), - team2_id: list = Query(default=None), game_type: list = Query(default=None), ranked: Optional[bool] = None, - short_game: Optional[bool] = None, csv: Optional[bool] = False, short_output: bool = False, - gauntlet_id: Optional[int] = None): + season: list = Query(default=None), + forfeit: Optional[bool] = None, + away_team_id: list = Query(default=None), + home_team_id: list = Query(default=None), + team1_id: list = Query(default=None), + team2_id: list = Query(default=None), + game_type: list = Query(default=None), + ranked: Optional[bool] = None, + short_game: Optional[bool] = None, + csv: Optional[bool] = False, + short_output: bool = False, + gauntlet_id: Optional[int] = None, + limit: int = 100, +): all_games = StratGame.select().order_by(StratGame.id) if season is not None: @@ -68,49 +74,70 @@ async def get_games( if short_game is not None: all_games = all_games.where(StratGame.short_game == short_game) if gauntlet_id is not None: - all_games = all_games.where(StratGame.game_type.contains(f'gauntlet-{gauntlet_id}')) + all_games = all_games.where( + StratGame.game_type.contains(f"gauntlet-{gauntlet_id}") + ) + + all_games = all_games.limit(max(0, min(limit, 500))) if csv: return_vals = [model_to_dict(x) for x in all_games] for x in return_vals: - x['away_abbrev'] = x['away_team']['abbrev'] - x['home_abbrev'] = x['home_team']['abbrev'] - del x['away_team'], x['home_team'] + x["away_abbrev"] = x["away_team"]["abbrev"] + x["home_abbrev"] = x["home_team"]["abbrev"] + del x["away_team"], x["home_team"] - output = pd.DataFrame(return_vals)[[ - 'id', 'away_abbrev', 'home_abbrev', 'away_score', 'home_score', 'away_team_value', 'home_team_value', - 'game_type', 'season', 'week', 'short_game', 'ranked' - ]] + output = pd.DataFrame(return_vals)[ + [ + "id", + "away_abbrev", + "home_abbrev", + "away_score", + "home_score", + "away_team_value", + "home_team_value", + "game_type", + "season", + "week", + "short_game", + "ranked", + ] + ] - return Response(content=output.to_csv(index=False), media_type='text/csv') + return Response(content=output.to_csv(index=False), media_type="text/csv") - return_val = {'count': all_games.count(), 'games': [ - model_to_dict(x, recurse=not short_output) for x in all_games - ]} + return_val = { + "count": all_games.count(), + "games": [model_to_dict(x, recurse=not short_output) for x in all_games], + } return return_val -@router.get('/{game_id}') +@router.get("/{game_id}") async def get_one_game(game_id: int): this_game = StratGame.get_or_none(StratGame.id == game_id) if not this_game: - raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found') + raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found") g_result = model_to_dict(this_game) return g_result -@router.patch('/{game_id}') +@router.patch("/{game_id}") async def patch_game( - game_id: int, game_type: Optional[str] = None, away_score: Optional[int] = None, - home_score: Optional[int] = None, token: str = Depends(oauth2_scheme)): + game_id: int, + game_type: Optional[str] = None, + away_score: Optional[int] = None, + home_score: Optional[int] = None, + token: str = Depends(oauth2_scheme), +): if not valid_token(token): - logging.warning('patch_game - Bad Token: [REDACTED]') - raise HTTPException(status_code=401, detail='Unauthorized') + logging.warning("patch_game - Bad Token: [REDACTED]") + raise HTTPException(status_code=401, detail="Unauthorized") this_game = StratGame.get_or_none(StratGame.id == game_id) if not this_game: - raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found') + raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found") if away_score is not None: this_game.away_score = away_score @@ -123,14 +150,14 @@ async def patch_game( g_result = model_to_dict(this_game) return g_result else: - raise HTTPException(status_code=500, detail=f'Unable to patch game {game_id}') + raise HTTPException(status_code=500, detail=f"Unable to patch game {game_id}") -@router.post('') +@router.post("") async def post_game(this_game: GameModel, token: str = Depends(oauth2_scheme)): if not valid_token(token): - logging.warning('post_games - Bad Token: [REDACTED]') - raise HTTPException(status_code=401, detail='Unauthorized') + logging.warning("post_games - Bad Token: [REDACTED]") + raise HTTPException(status_code=401, detail="Unauthorized") this_game = StratGame(**this_game.dict()) @@ -141,25 +168,25 @@ async def post_game(this_game: GameModel, token: str = Depends(oauth2_scheme)): else: raise HTTPException( status_code=418, - detail='Well slap my ass and call me a teapot; I could not save that game' + detail="Well slap my ass and call me a teapot; I could not save that game", ) -@router.delete('/{game_id}') +@router.delete("/{game_id}") async def delete_game(game_id: int, token: str = Depends(oauth2_scheme)): if not valid_token(token): - logging.warning('delete_game - Bad Token: [REDACTED]') - raise HTTPException(status_code=401, detail='Unauthorized') + logging.warning("delete_game - Bad Token: [REDACTED]") + raise HTTPException(status_code=401, detail="Unauthorized") this_game = StratGame.get_or_none(StratGame.id == game_id) if not this_game: - raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found') + raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found") count = this_game.delete_instance() if count == 1: - return f'StratGame {game_id} has been deleted' + return f"StratGame {game_id} has been deleted" else: - raise HTTPException(status_code=500, detail=f'StratGame {game_id} could not be deleted') - - + raise HTTPException( + status_code=500, detail=f"StratGame {game_id} could not be deleted" + )