Merge pull request 'fix: case-insensitive team_abbrev filter in transactions endpoint' (#60) from fix/transaction-team-filter-case-insensitive into main
All checks were successful
Build Docker Image / build (push) Successful in 54s

Reviewed-on: #60
This commit is contained in:
cal 2026-03-08 22:37:45 +00:00
commit bc36970aeb

View File

@ -5,15 +5,17 @@ import logging
import pydantic import pydantic
from ..db_engine import db, Transaction, Team, Player, model_to_dict, chunked, fn 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 from ..dependencies import (
oauth2_scheme,
logger = logging.getLogger('discord_app') valid_token,
PRIVATE_IN_SCHEMA,
router = APIRouter( handle_db_errors,
prefix='/api/v3/transactions',
tags=['transactions']
) )
logger = logging.getLogger("discord_app")
router = APIRouter(prefix="/api/v3/transactions", tags=["transactions"])
class TransactionModel(pydantic.BaseModel): class TransactionModel(pydantic.BaseModel):
week: int week: int
@ -31,13 +33,21 @@ class TransactionList(pydantic.BaseModel):
moves: List[TransactionModel] moves: List[TransactionModel]
@router.get('') @router.get("")
@handle_db_errors @handle_db_errors
async def get_transactions( async def get_transactions(
season, team_abbrev: list = Query(default=None), week_start: Optional[int] = 0, season,
week_end: Optional[int] = None, cancelled: Optional[bool] = None, frozen: Optional[bool] = None, team_abbrev: list = Query(default=None),
player_name: list = Query(default=None), player_id: list = Query(default=None), move_id: Optional[str] = None, week_start: Optional[int] = 0,
is_trade: Optional[bool] = None, short_output: Optional[bool] = False): 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,
):
if season: if season:
transactions = Transaction.select_season(season) transactions = Transaction.select_season(season)
else: else:
@ -45,7 +55,7 @@ async def get_transactions(
if team_abbrev is not None: if team_abbrev is not None:
t_list = [x.upper() for x in team_abbrev] t_list = [x.upper() for x in team_abbrev]
these_teams = Team.select().where((Team.abbrev << t_list)) these_teams = Team.select().where(fn.UPPER(Team.abbrev) << t_list)
transactions = transactions.where( transactions = transactions.where(
(Transaction.newteam << these_teams) | (Transaction.oldteam << these_teams) (Transaction.newteam << these_teams) | (Transaction.oldteam << these_teams)
) )
@ -75,31 +85,39 @@ async def get_transactions(
transactions = transactions.where(Transaction.frozen == 0) transactions = transactions.where(Transaction.frozen == 0)
if is_trade is not None: if is_trade is not None:
raise HTTPException(status_code=501, detail='The is_trade parameter is not implemented, yet') raise HTTPException(
status_code=501, detail="The is_trade parameter is not implemented, yet"
)
transactions = transactions.order_by(-Transaction.week, Transaction.moveid) transactions = transactions.order_by(-Transaction.week, Transaction.moveid)
return_trans = { return_trans = {
'count': transactions.count(), "count": transactions.count(),
'transactions': [model_to_dict(x, recurse=not short_output) for x in transactions] "transactions": [
model_to_dict(x, recurse=not short_output) for x in transactions
],
} }
db.close() db.close()
return return_trans 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 @handle_db_errors
async def patch_transactions( 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): if not valid_token(token):
logger.warning(f'patch_transactions - Bad Token: {token}') logger.warning(f"patch_transactions - Bad Token: {token}")
raise HTTPException(status_code=401, detail='Unauthorized') raise HTTPException(status_code=401, detail="Unauthorized")
these_moves = Transaction.select().where(Transaction.moveid == move_id) these_moves = Transaction.select().where(Transaction.moveid == move_id)
if these_moves.count() == 0: if these_moves.count() == 0:
db.close() 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: if frozen is not None:
for x in these_moves: for x in these_moves:
@ -111,25 +129,33 @@ async def patch_transactions(
x.save() x.save()
db.close() 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 @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): if not valid_token(token):
logger.warning(f'post_transactions - Bad Token: {token}') logger.warning(f"post_transactions - Bad Token: {token}")
raise HTTPException(status_code=401, detail='Unauthorized') raise HTTPException(status_code=401, detail="Unauthorized")
all_moves = [] all_moves = []
for x in moves.moves: for x in moves.moves:
if Team.get_or_none(Team.id == x.oldteam_id) is None: 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: 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: 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()) all_moves.append(x.dict())
@ -138,22 +164,25 @@ async def post_transactions(moves: TransactionList, token: str = Depends(oauth2_
Transaction.insert_many(batch).on_conflict_ignore().execute() Transaction.insert_many(batch).on_conflict_ignore().execute()
db.close() 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 @handle_db_errors
async def delete_transactions(move_id, token: str = Depends(oauth2_scheme)): async def delete_transactions(move_id, token: str = Depends(oauth2_scheme)):
if not valid_token(token): if not valid_token(token):
logger.warning(f'delete_transactions - Bad Token: {token}') logger.warning(f"delete_transactions - Bad Token: {token}")
raise HTTPException(status_code=401, detail='Unauthorized') raise HTTPException(status_code=401, detail="Unauthorized")
delete_query = Transaction.delete().where(Transaction.moveid == move_id) delete_query = Transaction.delete().where(Transaction.moveid == move_id)
count = delete_query.execute() count = delete_query.execute()
db.close() db.close()
if count > 0: if count > 0:
return f'Removed {count} transactions' return f"Removed {count} transactions"
else: else:
raise HTTPException(status_code=418, detail=f'Well slap my ass and call me a teapot; ' raise HTTPException(
f'I did not delete any records') status_code=418,
detail=f"Well slap my ass and call me a teapot; "
f"I did not delete any records",
)