diff --git a/app/routers_v3/transactions.py b/app/routers_v3/transactions.py index 79411d9..122a7ea 100644 --- a/app/routers_v3/transactions.py +++ b/app/routers_v3/transactions.py @@ -5,15 +5,17 @@ import logging import pydantic from ..db_engine import db, Transaction, Team, Player, model_to_dict, chunked, fn -from ..dependencies import oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA, handle_db_errors - -logger = logging.getLogger('discord_app') - -router = APIRouter( - prefix='/api/v3/transactions', - tags=['transactions'] +from ..dependencies import ( + oauth2_scheme, + valid_token, + PRIVATE_IN_SCHEMA, + handle_db_errors, ) +logger = logging.getLogger("discord_app") + +router = APIRouter(prefix="/api/v3/transactions", tags=["transactions"]) + class TransactionModel(pydantic.BaseModel): week: int @@ -31,13 +33,20 @@ class TransactionList(pydantic.BaseModel): moves: List[TransactionModel] -@router.get('') +@router.get("") @handle_db_errors async def get_transactions( - season, team_abbrev: list = Query(default=None), week_start: Optional[int] = 0, - week_end: Optional[int] = None, cancelled: Optional[bool] = None, frozen: Optional[bool] = None, - player_name: list = Query(default=None), player_id: list = Query(default=None), move_id: Optional[str] = None, - is_trade: Optional[bool] = None, short_output: Optional[bool] = False): + season, + team_abbrev: list = Query(default=None), + week_start: Optional[int] = 0, + week_end: Optional[int] = None, + cancelled: Optional[bool] = None, + frozen: Optional[bool] = None, + player_name: list = Query(default=None), + player_id: list = Query(default=None), + move_id: Optional[str] = None, + short_output: Optional[bool] = False, +): if season: transactions = Transaction.select_season(season) else: @@ -74,32 +83,35 @@ async def get_transactions( else: transactions = transactions.where(Transaction.frozen == 0) - if is_trade is not None: - raise HTTPException(status_code=501, detail='The is_trade parameter is not implemented, yet') - transactions = transactions.order_by(-Transaction.week, Transaction.moveid) return_trans = { - 'count': transactions.count(), - 'transactions': [model_to_dict(x, recurse=not short_output) for x in transactions] + "count": transactions.count(), + "transactions": [ + model_to_dict(x, recurse=not short_output) for x in transactions + ], } db.close() return return_trans -@router.patch('/{move_id}', include_in_schema=PRIVATE_IN_SCHEMA) +@router.patch("/{move_id}", include_in_schema=PRIVATE_IN_SCHEMA) @handle_db_errors async def patch_transactions( - move_id, token: str = Depends(oauth2_scheme), frozen: Optional[bool] = None, cancelled: Optional[bool] = None): + move_id, + token: str = Depends(oauth2_scheme), + frozen: Optional[bool] = None, + cancelled: Optional[bool] = None, +): if not valid_token(token): - logger.warning(f'patch_transactions - Bad Token: {token}') - raise HTTPException(status_code=401, detail='Unauthorized') + logger.warning(f"patch_transactions - Bad Token: {token}") + raise HTTPException(status_code=401, detail="Unauthorized") these_moves = Transaction.select().where(Transaction.moveid == move_id) if these_moves.count() == 0: db.close() - raise HTTPException(status_code=404, detail=f'Move ID {move_id} not found') + raise HTTPException(status_code=404, detail=f"Move ID {move_id} not found") if frozen is not None: for x in these_moves: @@ -111,25 +123,33 @@ async def patch_transactions( x.save() db.close() - return f'Updated {these_moves.count()} transactions' + return f"Updated {these_moves.count()} transactions" -@router.post('', include_in_schema=PRIVATE_IN_SCHEMA) +@router.post("", include_in_schema=PRIVATE_IN_SCHEMA) @handle_db_errors -async def post_transactions(moves: TransactionList, token: str = Depends(oauth2_scheme)): +async def post_transactions( + moves: TransactionList, token: str = Depends(oauth2_scheme) +): if not valid_token(token): - logger.warning(f'post_transactions - Bad Token: {token}') - raise HTTPException(status_code=401, detail='Unauthorized') + logger.warning(f"post_transactions - Bad Token: {token}") + raise HTTPException(status_code=401, detail="Unauthorized") all_moves = [] for x in moves.moves: if Team.get_or_none(Team.id == x.oldteam_id) is None: - raise HTTPException(status_code=404, detail=f'Team ID {x.oldteam_id} not found') + raise HTTPException( + status_code=404, detail=f"Team ID {x.oldteam_id} not found" + ) if Team.get_or_none(Team.id == x.newteam_id) is None: - raise HTTPException(status_code=404, detail=f'Team ID {x.newteam_id} not found') + raise HTTPException( + status_code=404, detail=f"Team ID {x.newteam_id} not found" + ) if Player.get_or_none(Player.id == x.player_id) is None: - raise HTTPException(status_code=404, detail=f'Player ID {x.player_id} not found') + raise HTTPException( + status_code=404, detail=f"Player ID {x.player_id} not found" + ) all_moves.append(x.dict()) @@ -138,22 +158,25 @@ async def post_transactions(moves: TransactionList, token: str = Depends(oauth2_ Transaction.insert_many(batch).on_conflict_ignore().execute() db.close() - return f'{len(all_moves)} transactions have been added' + return f"{len(all_moves)} transactions have been added" -@router.delete('/{move_id}', include_in_schema=PRIVATE_IN_SCHEMA) +@router.delete("/{move_id}", include_in_schema=PRIVATE_IN_SCHEMA) @handle_db_errors async def delete_transactions(move_id, token: str = Depends(oauth2_scheme)): if not valid_token(token): - logger.warning(f'delete_transactions - Bad Token: {token}') - raise HTTPException(status_code=401, detail='Unauthorized') + logger.warning(f"delete_transactions - Bad Token: {token}") + raise HTTPException(status_code=401, detail="Unauthorized") delete_query = Transaction.delete().where(Transaction.moveid == move_id) count = delete_query.execute() db.close() if count > 0: - return f'Removed {count} transactions' + return f"Removed {count} transactions" else: - raise HTTPException(status_code=418, detail=f'Well slap my ass and call me a teapot; ' - f'I did not delete any records') + raise HTTPException( + status_code=418, + detail=f"Well slap my ass and call me a teapot; " + f"I did not delete any records", + )