167 lines
5.1 KiB
Python
167 lines
5.1 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 db, Current, model_to_dict
|
|
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
|
|
|
logging.basicConfig(
|
|
filename=LOG_DATA['filename'],
|
|
format=LOG_DATA['format'],
|
|
level=LOG_DATA['log_level']
|
|
)
|
|
|
|
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)
|
|
|
|
db.close()
|
|
return Response(content=return_val, media_type='text/csv')
|
|
else:
|
|
return_val = model_to_dict(current)
|
|
db.close()
|
|
return return_val
|
|
|
|
|
|
@router.get('/{current_id}')
|
|
async def get_one_current(current_id, csv: Optional[bool] = False):
|
|
try:
|
|
current = Current.get_by_id(current_id)
|
|
except Exception:
|
|
db.close()
|
|
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)
|
|
|
|
db.close()
|
|
return Response(content=return_val, media_type='text/csv')
|
|
else:
|
|
return_val = model_to_dict(current)
|
|
db.close()
|
|
return return_val
|
|
|
|
|
|
@router.post('')
|
|
async def post_current(current: CurrentModel, token: str = Depends(oauth2_scheme)):
|
|
if not valid_token(token):
|
|
logging.warning(f'Bad Token: {token}')
|
|
db.close()
|
|
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:
|
|
db.close()
|
|
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)
|
|
db.close()
|
|
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}')
|
|
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(f'Bad Token: {token}')
|
|
db.close()
|
|
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 Exception:
|
|
db.close()
|
|
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)
|
|
db.close()
|
|
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}')
|
|
async def delete_current(current_id, token: str = Depends(oauth2_scheme)):
|
|
if not valid_token(token):
|
|
logging.warning(f'Bad Token: {token}')
|
|
db.close()
|
|
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 Exception:
|
|
db.close()
|
|
raise HTTPException(status_code=404, detail=f'No current found with id {current_id}')
|
|
|
|
count = this_curr.delete_instance()
|
|
db.close()
|
|
|
|
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')
|