paper-dynasty-database/app/routers_v2/current.py
Cal Corum 0c042165b7 fix: replace broad except Exception blocks with DoesNotExist (#15)
Replace 71 broad `except Exception` blocks in 19 router files with the
specific `peewee.DoesNotExist` exception. GET endpoints that call
`Model.get_by_id()` now only catch the expected DoesNotExist error,
allowing real DB failures (connection errors, etc.) to propagate as
500s rather than being masked as 404s.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-05 15:32:53 -06:00

148 lines
4.9 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Response
from pandas import DataFrame
from typing import Optional
import logging
import pydantic
from ..db_engine import Current, model_to_dict, DoesNotExist
from ..dependencies import oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA
router = APIRouter(
prefix='/api/v2/current',
tags=['current']
)
class CurrentModel(pydantic.BaseModel):
season: int
week: int
gsheet_template: str
gsheet_version: str
@router.get('')
async def get_current(season: Optional[int] = None, csv: Optional[bool] = False):
if season:
current = Current.get_or_none(season=season)
else:
current = Current.latest()
if csv:
current_list = [
['id', 'season', 'week'],
[current.id, current.season, current.week]
]
return_val = DataFrame(current_list).to_csv(header=False, index=False)
return Response(content=return_val, media_type='text/csv')
else:
return_val = model_to_dict(current)
return return_val
@router.get('/{current_id}', include_in_schema=PRIVATE_IN_SCHEMA)
async def get_one_current(current_id, csv: Optional[bool] = False):
try:
current = Current.get_by_id(current_id)
except DoesNotExist:
raise HTTPException(status_code=404, detail=f'No current found with id {current_id}')
if csv:
current_list = [
['id', 'season', 'week'],
[current.id, current.season, current.week]
]
return_val = DataFrame(current_list).to_csv(header=False, index=False)
return Response(content=return_val, media_type='text/csv')
else:
return_val = model_to_dict(current)
return return_val
@router.post('', include_in_schema=PRIVATE_IN_SCHEMA)
async def post_current(current: CurrentModel, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning('Bad Token: [REDACTED]')
raise HTTPException(
status_code=401,
detail='You are not authorized to post current. This event has been logged.'
)
dupe_curr = Current.get_or_none(Current.season == current.season)
if dupe_curr:
raise HTTPException(status_code=400, detail=f'There is already a current for season {current.season}')
this_curr = Current(
season=current.season,
week=current.week,
gsheet_template=current.gsheet_template,
gsheet_version=current.gsheet_version
)
saved = this_curr.save()
if saved == 1:
return_val = model_to_dict(this_curr)
return return_val
else:
raise HTTPException(status_code=418, detail='Well slap my ass and call me a teapot; I could not save that team')
@router.patch('/{current_id}', include_in_schema=PRIVATE_IN_SCHEMA)
async def patch_current(
current_id: int, season: Optional[int] = None, week: Optional[int] = None,
gsheet_template: Optional[str] = None, gsheet_version: Optional[str] = None,
live_scoreboard: Optional[int] = None, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning('Bad Token: [REDACTED]')
raise HTTPException(
status_code=401,
detail='You are not authorized to patch current. This event has been logged.'
)
try:
current = Current.get_by_id(current_id)
except DoesNotExist:
raise HTTPException(status_code=404, detail=f'No current found with id {current_id}')
if season is not None:
current.season = season
if week is not None:
current.week = week
if gsheet_template is not None:
current.gsheet_template = gsheet_template
if gsheet_version is not None:
current.gsheet_version = gsheet_version
if live_scoreboard is not None:
current.live_scoreboard = live_scoreboard
if current.save() == 1:
return_val = model_to_dict(current)
return return_val
else:
raise HTTPException(
status_code=418,
detail='Well slap my ass and call me a teapot; I could not save that current'
)
@router.delete('/{current_id}', include_in_schema=PRIVATE_IN_SCHEMA)
async def delete_current(current_id, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning('Bad Token: [REDACTED]')
raise HTTPException(
status_code=401,
detail='You are not authorized to delete current. This event has been logged.'
)
try:
this_curr = Current.get_by_id(current_id)
except DoesNotExist:
raise HTTPException(status_code=404, detail=f'No current found with id {current_id}')
count = this_curr.delete_instance()
if count == 1:
raise HTTPException(status_code=200, detail=f'Current {current_id} has been deleted')
else:
raise HTTPException(status_code=500, detail=f'Current {current_id} was not deleted')