Update players.py

Adding headers and moving files to sub batting and pitching directories
This commit is contained in:
Cal Corum 2023-10-26 21:45:02 -05:00
parent 4e54e36bc9
commit c62d0fc60e

View File

@ -352,6 +352,8 @@ async def get_one_player(player_id, csv: Optional[bool] = False):
@router.get('/{player_id}/{card_type}card')
@router.get('/{player_id}/{card_type}card/{d}')
@router.get('/{player_id}/{card_type}card/{d}/{variant}')
async def get_batter_card(
request: Request, player_id: int, card_type: Literal['batting', 'pitching'], variant: int = 0, d: str = None,
html: Optional[bool] = False):
@ -361,11 +363,14 @@ async def get_batter_card(
db.close()
raise HTTPException(status_code=404, detail=f'No player found with id {player_id}')
if os.path.isfile(f'storage/cards/cardset-{this_player.cardset.id}/{player_id}-{d}-v{variant}.png') and html is False:
headers = {'Cache-Control': 'public, max-age=86400'}
filename = f'{this_player.description} {this_player.p_name} {card_type} {d}-v{variant}'
if os.path.isfile(f'storage/cards/cardset-{this_player.cardset.id}/{card_type}/{player_id}-{d}-v{variant}.png') and html is False:
db.close()
return FileResponse(
path=f'storage/cards/cardset-{this_player.cardset.id}/{player_id}-{d}-v{variant}.png',
media_type='image/png'
path=f'storage/cards/cardset-{this_player.cardset.id}/{card_type}/{player_id}-{d}-v{variant}.png',
media_type='image/png',
headers=headers
)
all_pos = CardPosition.select().where(CardPosition.player == this_player).order_by(CardPosition.innings.desc())
@ -436,7 +441,7 @@ async def get_batter_card(
hti = Html2Image(
browser='chromium',
size=(1200, 600),
output_path=f'storage/cards/cardset-{this_player.cardset.id}/',
output_path=f'storage/cards/cardset-{this_player.cardset.id}/{card_type}/',
custom_flags=['--no-sandbox', '--disable-remote-debugging', '--headless', '--disable-gpu',
'--disable-software-rasterizer', '--disable-dev-shm-usage']
)
@ -448,7 +453,7 @@ async def get_batter_card(
)
db.close()
return FileResponse(path=x[0], media_type='image/png')
return FileResponse(path=x[0], media_type='image/png', headers=headers)
# @router.get('/{player_id}/pitchingcard')