Merge branch 'main' into issue/132-feat-add-limit-pagination-to-awards-endpoint

This commit is contained in:
cal 2026-03-24 12:06:35 +00:00
commit fe4d22f28a

View File

@ -8,10 +8,7 @@ from ..db_engine import GameRewards, model_to_dict, DoesNotExist
from ..dependencies import oauth2_scheme, valid_token from ..dependencies import oauth2_scheme, valid_token
router = APIRouter( router = APIRouter(prefix="/api/v2/gamerewards", tags=["gamerewards"])
prefix='/api/v2/gamerewards',
tags=['gamerewards']
)
class GameRewardModel(pydantic.BaseModel): class GameRewardModel(pydantic.BaseModel):
@ -21,10 +18,15 @@ class GameRewardModel(pydantic.BaseModel):
money: Optional[int] = None money: Optional[int] = None
@router.get('') @router.get("")
async def v1_gamerewards_get( async def v1_gamerewards_get(
name: Optional[str] = None, pack_type_id: Optional[int] = None, player_id: Optional[int] = None, name: Optional[str] = None,
money: Optional[int] = None, csv: Optional[bool] = None): pack_type_id: Optional[int] = None,
player_id: Optional[int] = None,
money: Optional[int] = None,
csv: Optional[bool] = None,
limit: int = 100,
):
all_rewards = GameRewards.select().order_by(GameRewards.id) all_rewards = GameRewards.select().order_by(GameRewards.id)
# if all_rewards.count() == 0: # if all_rewards.count() == 0:
@ -40,61 +42,76 @@ async def v1_gamerewards_get(
if money is not None: if money is not None:
all_rewards = all_rewards.where(GameRewards.money == money) all_rewards = all_rewards.where(GameRewards.money == money)
limit = max(0, min(limit, 500))
all_rewards = all_rewards.limit(limit)
if csv: if csv:
data_list = [['id', 'pack_type_id', 'player_id', 'money']] data_list = [["id", "pack_type_id", "player_id", "money"]]
for line in all_rewards: for line in all_rewards:
data_list.append([ data_list.append(
line.id, line.pack_type_id if line.pack_type else None, line.player_id if line.player else None, [
line.money line.id,
]) line.pack_type_id if line.pack_type else None,
line.player_id if line.player else None,
line.money,
]
)
return_val = DataFrame(data_list).to_csv(header=False, index=False) return_val = DataFrame(data_list).to_csv(header=False, index=False)
return Response(content=return_val, media_type='text/csv') return Response(content=return_val, media_type="text/csv")
else: else:
return_val = {'count': all_rewards.count(), 'gamerewards': []} return_val = {"count": all_rewards.count(), "gamerewards": []}
for x in all_rewards: for x in all_rewards:
return_val['gamerewards'].append(model_to_dict(x)) return_val["gamerewards"].append(model_to_dict(x))
return return_val return return_val
@router.get('/{gameaward_id}') @router.get("/{gameaward_id}")
async def v1_gamerewards_get_one(gamereward_id, csv: Optional[bool] = None): async def v1_gamerewards_get_one(gamereward_id, csv: Optional[bool] = None):
try: try:
this_game_reward = GameRewards.get_by_id(gamereward_id) this_game_reward = GameRewards.get_by_id(gamereward_id)
except DoesNotExist: except DoesNotExist:
raise HTTPException(status_code=404, detail=f'No game reward found with id {gamereward_id}') raise HTTPException(
status_code=404, detail=f"No game reward found with id {gamereward_id}"
)
if csv: if csv:
data_list = [ data_list = [
['id', 'pack_type_id', 'player_id', 'money'], ["id", "pack_type_id", "player_id", "money"],
[this_game_reward.id, this_game_reward.pack_type_id if this_game_reward.pack_type else None, [
this_game_reward.player_id if this_game_reward.player else None, this_game_reward.money] this_game_reward.id,
this_game_reward.pack_type_id if this_game_reward.pack_type else None,
this_game_reward.player_id if this_game_reward.player else None,
this_game_reward.money,
],
] ]
return_val = DataFrame(data_list).to_csv(header=False, index=False) return_val = DataFrame(data_list).to_csv(header=False, index=False)
return Response(content=return_val, media_type='text/csv') return Response(content=return_val, media_type="text/csv")
else: else:
return_val = model_to_dict(this_game_reward) return_val = model_to_dict(this_game_reward)
return return_val return return_val
@router.post('') @router.post("")
async def v1_gamerewards_post(game_reward: GameRewardModel, token: str = Depends(oauth2_scheme)): async def v1_gamerewards_post(
game_reward: GameRewardModel, token: str = Depends(oauth2_scheme)
):
if not valid_token(token): if not valid_token(token):
logging.warning('Bad Token: [REDACTED]') logging.warning("Bad Token: [REDACTED]")
raise HTTPException( raise HTTPException(
status_code=401, status_code=401,
detail='You are not authorized to post game rewards. This event has been logged.' detail="You are not authorized to post game rewards. This event has been logged.",
) )
this_award = GameRewards( this_award = GameRewards(
name=game_reward.name, name=game_reward.name,
pack_type_id=game_reward.pack_type_id, pack_type_id=game_reward.pack_type_id,
player_id=game_reward.player_id, player_id=game_reward.player_id,
money=game_reward.money money=game_reward.money,
) )
saved = this_award.save() saved = this_award.save()
@ -104,24 +121,31 @@ async def v1_gamerewards_post(game_reward: GameRewardModel, token: str = Depends
else: else:
raise HTTPException( raise HTTPException(
status_code=418, status_code=418,
detail='Well slap my ass and call me a teapot; I could not save that roster' detail="Well slap my ass and call me a teapot; I could not save that roster",
) )
@router.patch('/{game_reward_id}') @router.patch("/{game_reward_id}")
async def v1_gamerewards_patch( async def v1_gamerewards_patch(
game_reward_id: int, name: Optional[str] = None, pack_type_id: Optional[int] = None, game_reward_id: int,
player_id: Optional[int] = None, money: Optional[int] = None, token: str = Depends(oauth2_scheme)): name: Optional[str] = None,
pack_type_id: Optional[int] = None,
player_id: Optional[int] = None,
money: Optional[int] = None,
token: str = Depends(oauth2_scheme),
):
if not valid_token(token): if not valid_token(token):
logging.warning('Bad Token: [REDACTED]') logging.warning("Bad Token: [REDACTED]")
raise HTTPException( raise HTTPException(
status_code=401, status_code=401,
detail='You are not authorized to patch gamerewards. This event has been logged.' detail="You are not authorized to patch gamerewards. This event has been logged.",
) )
try: try:
this_game_reward = GameRewards.get_by_id(game_reward_id) this_game_reward = GameRewards.get_by_id(game_reward_id)
except DoesNotExist: except DoesNotExist:
raise HTTPException(status_code=404, detail=f'No game reward found with id {game_reward_id}') raise HTTPException(
status_code=404, detail=f"No game reward found with id {game_reward_id}"
)
if name is not None: if name is not None:
this_game_reward.name = name this_game_reward.name = name
@ -147,27 +171,32 @@ async def v1_gamerewards_patch(
else: else:
raise HTTPException( raise HTTPException(
status_code=418, status_code=418,
detail='Well slap my ass and call me a teapot; I could not save that rarity' detail="Well slap my ass and call me a teapot; I could not save that rarity",
) )
@router.delete('/{gamereward_id}') @router.delete("/{gamereward_id}")
async def v1_gamerewards_delete(gamereward_id, token: str = Depends(oauth2_scheme)): async def v1_gamerewards_delete(gamereward_id, token: str = Depends(oauth2_scheme)):
if not valid_token(token): if not valid_token(token):
logging.warning('Bad Token: [REDACTED]') logging.warning("Bad Token: [REDACTED]")
raise HTTPException( raise HTTPException(
status_code=401, status_code=401,
detail='You are not authorized to delete awards. This event has been logged.' detail="You are not authorized to delete awards. This event has been logged.",
) )
try: try:
this_award = GameRewards.get_by_id(gamereward_id) this_award = GameRewards.get_by_id(gamereward_id)
except DoesNotExist: except DoesNotExist:
raise HTTPException(status_code=404, detail=f'No award found with id {gamereward_id}') raise HTTPException(
status_code=404, detail=f"No award found with id {gamereward_id}"
)
count = this_award.delete_instance() count = this_award.delete_instance()
if count == 1: if count == 1:
raise HTTPException(status_code=200, detail=f'Game Reward {gamereward_id} has been deleted') raise HTTPException(
status_code=200, detail=f"Game Reward {gamereward_id} has been deleted"
)
else: else:
raise HTTPException(status_code=500, detail=f'Game Reward {gamereward_id} was not deleted') raise HTTPException(
status_code=500, detail=f"Game Reward {gamereward_id} was not deleted"
)