Fix relative package issues
This commit is contained in:
parent
54b0485599
commit
782804424d
@ -5,4 +5,4 @@ WORKDIR /usr/src/app
|
||||
COPY requirements.txt ./
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY ./app /app
|
||||
COPY ./app /app/app
|
||||
@ -1,8 +1,8 @@
|
||||
import copy
|
||||
import math
|
||||
from typing import Literal
|
||||
|
||||
from peewee import *
|
||||
|
||||
from playhouse.shortcuts import model_to_dict
|
||||
|
||||
db = SqliteDatabase(
|
||||
@ -24,6 +24,38 @@ Per season updates:
|
||||
"""
|
||||
|
||||
|
||||
WEEK_NUMS = {
|
||||
'regular': {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def per_season_weeks(season: int, s_type: Literal['regular', 'post', 'total']):
|
||||
if season == 1:
|
||||
if s_type == 'regular':
|
||||
return {'start': 1, 'end': 20}
|
||||
elif s_type == 'post':
|
||||
return {'start': 21, 'end': 22}
|
||||
else:
|
||||
return {'start': 1, 'end': 22}
|
||||
elif season in [3, 4, 5, 6, 7]:
|
||||
if s_type == 'regular':
|
||||
return {'start': 1, 'end': 22}
|
||||
elif s_type == 'post':
|
||||
return {'start': 23, 'end': 25}
|
||||
else:
|
||||
return {'start': 1, 'end': 25}
|
||||
# Season 2, 8, and beyond
|
||||
else:
|
||||
if s_type == 'regular':
|
||||
return {'start': 1, 'end': 18}
|
||||
elif s_type == 'post':
|
||||
return {'start': 19, 'end': 21}
|
||||
else:
|
||||
return {'start': 1, 'end': 21}
|
||||
|
||||
|
||||
def win_pct(this_team_stan):
|
||||
if this_team_stan.wins + this_team_stan.losses == 0:
|
||||
return 0
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from routers_v3 import current, players, results, schedules, standings, teams, transactions
|
||||
from .routers_v3 import current, players, results, schedules, standings, teams, transactions, battingstats
|
||||
|
||||
app = FastAPI(
|
||||
responses={404: {'description': 'Not found'}}
|
||||
@ -14,6 +14,7 @@ app.include_router(schedules.router)
|
||||
app.include_router(teams.router)
|
||||
app.include_router(transactions.router)
|
||||
app.include_router(standings.router)
|
||||
app.include_router(battingstats.router)
|
||||
|
||||
|
||||
# @app.get("/api")
|
||||
|
||||
264
app/routers_v3/battingstats.py
Normal file
264
app/routers_v3/battingstats.py
Normal file
@ -0,0 +1,264 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from typing import List, Optional, Literal
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from ..db_engine import db, BattingStat, Team, Player, Current, model_to_dict, chunked, fn, per_season_weeks
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/battingstats',
|
||||
tags=['battingstats']
|
||||
)
|
||||
|
||||
|
||||
class BatStatModel(pydantic.BaseModel):
|
||||
player_id: int
|
||||
team_id: int
|
||||
pos: str
|
||||
pa: int
|
||||
ab: int
|
||||
run: int
|
||||
hit: int
|
||||
rbi: int
|
||||
double: int
|
||||
triple: int
|
||||
hr: int
|
||||
bb: int
|
||||
so: int
|
||||
hbp: int
|
||||
sac: int
|
||||
ibb: int
|
||||
gidp: int
|
||||
sb: int
|
||||
cs: int
|
||||
bphr: int
|
||||
bpfo: int
|
||||
bp1b: int
|
||||
bplo: int
|
||||
xba: int
|
||||
xbt: int
|
||||
xch: int
|
||||
xhit: int
|
||||
error: int
|
||||
pb: int
|
||||
sbc: int
|
||||
csc: int
|
||||
roba: int
|
||||
robs: int
|
||||
raa: int
|
||||
rto: int
|
||||
week: int
|
||||
game: int
|
||||
season: int
|
||||
|
||||
|
||||
class BatStatList(pydantic.BaseModel):
|
||||
count: int
|
||||
stats: List[BatStatModel]
|
||||
|
||||
|
||||
@router.get('')
|
||||
async def get_batstats(
|
||||
season: int, s_type: Optional[str] = None, team_abbrev: list = Query(default=None),
|
||||
player_name: list = Query(default=None), player_id: list = Query(default=None),
|
||||
week_start: Optional[int] = None, week_end: Optional[int] = None, game_num: list = Query(default=None),
|
||||
position: list = Query(default=None), limit: Optional[int] = None, sort: Optional[str] = None,
|
||||
short_output: Optional[bool] = True):
|
||||
if 'post' in s_type.lower():
|
||||
all_stats = BattingStat.post_season(season)
|
||||
if all_stats.count() == 0:
|
||||
db.close()
|
||||
return {'count': 0, 'stats': []}
|
||||
elif s_type.lower() in ['combined', 'total', 'all']:
|
||||
all_stats = BattingStat.combined_season(season)
|
||||
if all_stats.count() == 0:
|
||||
db.close()
|
||||
return {'count': 0, 'stats': []}
|
||||
else:
|
||||
all_stats = BattingStat.regular_season(season)
|
||||
if all_stats.count() == 0:
|
||||
db.close()
|
||||
return {'count': 0, 'stats': []}
|
||||
if position is not None:
|
||||
all_stats = all_stats.where(BattingStat.pos << [x.upper() for x in position])
|
||||
if team_abbrev is not None:
|
||||
t_query = Team.select().where(Team.abbrev << [x.upper() for x in team_abbrev])
|
||||
all_stats = all_stats.where(BattingStat.team << t_query)
|
||||
if player_name is not None or player_id is not None:
|
||||
if player_id:
|
||||
all_stats = all_stats.where(BattingStat.player_id << player_id)
|
||||
else:
|
||||
p_query = Player.select_season(season).where(Player.name << player_name)
|
||||
all_stats = all_stats.where(BattingStat.player << p_query)
|
||||
if game_num:
|
||||
all_stats = all_stats.where(BattingStat.game == game_num)
|
||||
|
||||
start = 1
|
||||
end = Current.get(Current.season == season).week
|
||||
if week_start is not None:
|
||||
start = week_start
|
||||
if week_end is not None:
|
||||
end = min(week_end, end)
|
||||
if start > end:
|
||||
db.close()
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f'Start week {start} is after end week {end} - cannot pull stats'
|
||||
)
|
||||
all_stats = all_stats.where(
|
||||
(BattingStat.week >= start) & (BattingStat.week <= end)
|
||||
)
|
||||
|
||||
if limit:
|
||||
all_stats = all_stats.limit(limit)
|
||||
if sort:
|
||||
if sort == 'newest':
|
||||
all_stats = all_stats.order_by(-BattingStat.week, -BattingStat.game)
|
||||
|
||||
return_stats = {
|
||||
'count': all_stats.count(),
|
||||
'stats': [model_to_dict(x, recurse=not short_output) for x in all_stats]
|
||||
}
|
||||
|
||||
db.close()
|
||||
return return_stats
|
||||
|
||||
|
||||
@router.get('/season/{season}')
|
||||
async def get_seasonstats(
|
||||
season: int, s_type: Literal['regular', 'post', 'total'] = 'regular', team_abbrev: list = Query(default=None),
|
||||
team_id: list = Query(default=None), player_name: list = Query(default=None),
|
||||
player_id: list = Query(default=None), full_player: Optional[bool] = False):
|
||||
weeks = per_season_weeks(season, s_type)
|
||||
all_stats = (
|
||||
BattingStat
|
||||
.select(BattingStat.player, fn.SUM(BattingStat.pa).alias('sum_pa'), fn.SUM(BattingStat.ab).alias('sum_ab'),
|
||||
fn.SUM(BattingStat.run).alias('sum_run'), fn.SUM(BattingStat.hit).alias('sum_hit'),
|
||||
fn.SUM(BattingStat.rbi).alias('sum_rbi'), fn.SUM(BattingStat.double).alias('sum_double'),
|
||||
fn.SUM(BattingStat.triple).alias('sum_triple'), fn.SUM(BattingStat.hr).alias('sum_hr'),
|
||||
fn.SUM(BattingStat.bb).alias('sum_bb'), fn.SUM(BattingStat.so).alias('sum_so'),
|
||||
fn.SUM(BattingStat.hbp).alias('sum_hbp'), fn.SUM(BattingStat.sac).alias('sum_sac'),
|
||||
fn.SUM(BattingStat.ibb).alias('sum_ibb'), fn.SUM(BattingStat.gidp).alias('sum_gidp'),
|
||||
fn.SUM(BattingStat.sb).alias('sum_sb'), fn.SUM(BattingStat.cs).alias('sum_cs'),
|
||||
fn.SUM(BattingStat.bphr).alias('sum_bphr'), fn.SUM(BattingStat.bpfo).alias('sum_bpfo'),
|
||||
fn.SUM(BattingStat.bp1b).alias('sum_bp1b'), fn.SUM(BattingStat.bplo).alias('sum_bplo'),
|
||||
fn.SUM(BattingStat.xba).alias('sum_xba'), fn.SUM(BattingStat.xbt).alias('sum_xbt'),
|
||||
fn.SUM(BattingStat.xch).alias('sum_xch'), fn.SUM(BattingStat.xhit).alias('sum_xhit'),
|
||||
fn.SUM(BattingStat.error).alias('sum_error'), fn.SUM(BattingStat.pb).alias('sum_pb'),
|
||||
fn.SUM(BattingStat.sbc).alias('sum_sbc'), fn.SUM(BattingStat.csc).alias('sum_csc'),
|
||||
fn.SUM(BattingStat.roba).alias('sum_roba'), fn.SUM(BattingStat.robs).alias('sum_robs'),
|
||||
fn.SUM(BattingStat.raa).alias('sum_raa'), fn.SUM(BattingStat.rto).alias('sum_rto'))
|
||||
.where((BattingStat.week >= weeks['start']) & (BattingStat.week <= weeks['end']) &
|
||||
(BattingStat.season == season))
|
||||
.order_by(BattingStat.player)
|
||||
.group_by(BattingStat.player)
|
||||
)
|
||||
|
||||
if team_abbrev is None and team_id is None and player_name is None and player_id is None:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f'Must include team_id/team_abbrev and/or player_name/player_id'
|
||||
)
|
||||
|
||||
if team_id is not None:
|
||||
all_teams = Team.select().where(Team.id << team_id)
|
||||
all_stats = all_stats.where(BattingStat.team << all_teams)
|
||||
elif team_abbrev is not None:
|
||||
all_teams = Team.select().where(fn.Lower(Team.abbrev) << [x.lower() for x in team_abbrev])
|
||||
all_stats = all_stats.where(BattingStat.team << all_teams)
|
||||
|
||||
if player_name is not None:
|
||||
all_players = Player.select().where(fn.Lower(Player.name) << [x.lower() for x in player_name])
|
||||
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||
elif player_id is not None:
|
||||
all_players = Player.select().where(Player.id << player_id)
|
||||
all_stats = all_stats.where(BattingStat.player << all_players)
|
||||
|
||||
return_stats = {
|
||||
'count': all_stats.count(),
|
||||
'stats': [{
|
||||
'player': model_to_dict(x.player, recurse=False) if full_player else x.player_id,
|
||||
'pa': x.sum_pa,
|
||||
'ab': x.sum_ab,
|
||||
'run': x.sum_run,
|
||||
'hit': x.sum_hit,
|
||||
'rbi': x.sum_rbi,
|
||||
'double': x.sum_double,
|
||||
'triple': x.sum_triple,
|
||||
'hr': x.sum_hr,
|
||||
'bb': x.sum_bb,
|
||||
'so': x.sum_so,
|
||||
'hbp': x.sum_hbp,
|
||||
'sac': x.sum_sac,
|
||||
'ibb': x.sum_ibb,
|
||||
'gidp': x.sum_gidp,
|
||||
'sb': x.sum_sb,
|
||||
'cs': x.sum_cs,
|
||||
'bphr': x.sum_bphr,
|
||||
'bpfo': x.sum_bpfo,
|
||||
'bp1b': x.sum_bp1b,
|
||||
'bplo': x.sum_bplo,
|
||||
'xba': x.sum_xba,
|
||||
'xbt': x.sum_xbt,
|
||||
'xch': x.sum_xch,
|
||||
'xhit': x.sum_xhit,
|
||||
'error': x.sum_error,
|
||||
'pb': x.sum_pb,
|
||||
'sbc': x.sum_sbc,
|
||||
'csc': x.sum_csc,
|
||||
'roba': x.sum_roba,
|
||||
'robs': x.sum_robs,
|
||||
'raa': x.sum_raa,
|
||||
'rto': x.sum_rto,
|
||||
} for x in all_stats]
|
||||
}
|
||||
db.close()
|
||||
return return_stats
|
||||
|
||||
|
||||
@router.get('/career/{player_name}')
|
||||
async def get_careerstats(
|
||||
s_type: Literal['regular', 'post', 'total'] = 'regular', player_name: list = Query(default=None)):
|
||||
pass # Keep Career Stats table and recalculate after posting stats
|
||||
|
||||
|
||||
@router.patch('/{stat_id}')
|
||||
async def patch_batstats(stat_id: int, new_stats: BatStatModel, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_batstats - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
if BattingStat.get_or_none(BattingStat.id == stat_id) is None:
|
||||
raise HTTPException(status_code=404, detail=f'Stat ID {stat_id} not found')
|
||||
|
||||
BattingStat.update(**new_stats.dict()).where(BattingStat.id == stat_id).execute()
|
||||
r_stat = model_to_dict(BattingStat.get_by_id(stat_id))
|
||||
db.close()
|
||||
return r_stat
|
||||
|
||||
|
||||
@router.post('')
|
||||
async def post_batstats(s_list: BatStatList, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'post_batstats - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
all_stats = []
|
||||
|
||||
for x in s_list.stats:
|
||||
team = Team.get_or_none(Team.id == x.team_id)
|
||||
this_player = Player.get_or_none(Player.id == x.player_id)
|
||||
if team is None:
|
||||
raise HTTPException(status_code=404, detail=f'Team ID {x.team_id} not found')
|
||||
if this_player is None:
|
||||
raise HTTPException(status_code=404, detail=f'Player ID {x.player_id} not found')
|
||||
|
||||
all_stats.append(BattingStat(**x.dict()))
|
||||
|
||||
with db.atomic():
|
||||
BattingStat.bulk_create(all_stats, batch_size=15)
|
||||
|
||||
# Update career stats
|
||||
|
||||
return f'Added {len(all_stats)} batting lines'
|
||||
@ -3,8 +3,8 @@ from typing import Optional
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from db_engine import db, Current, model_to_dict
|
||||
from dependencies import oauth2_scheme, valid_token, logging
|
||||
from ..db_engine import db, Current, model_to_dict
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/current',
|
||||
@ -107,7 +107,7 @@ async def post_current(new_current: CurrentModel, token: str = Depends(oauth2_sc
|
||||
|
||||
|
||||
@router.delete('/{current_id}')
|
||||
def delete_current(current_id: int, token: str = Depends(oauth2_scheme)):
|
||||
async def delete_current(current_id: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_current - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
@ -3,8 +3,8 @@ from typing import List, Optional
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from db_engine import db, Player, model_to_dict, chunked
|
||||
from dependencies import oauth2_scheme, valid_token, logging
|
||||
from ..db_engine import db, Player, model_to_dict, chunked
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/players',
|
||||
@ -42,9 +42,9 @@ class PlayerList(pydantic.BaseModel):
|
||||
|
||||
|
||||
@router.get('')
|
||||
def get_players(
|
||||
async def get_players(
|
||||
season: Optional[int], team_id: list = Query(default=None), pos: list = Query(default=None),
|
||||
is_injured: Optional[bool] = None, short_output: Optional[bool] = False, sort: Optional[str] = None):
|
||||
is_injured: Optional[bool] = None, sort: Optional[str] = None):
|
||||
logging.info(f'team_id: {team_id}')
|
||||
|
||||
all_players = Player.select_season(season)
|
||||
@ -74,17 +74,17 @@ def get_players(
|
||||
|
||||
return_players = {
|
||||
'count': all_players.count(),
|
||||
'players': [model_to_dict(x, recurse=not short_output) for x in all_players]
|
||||
'players': [model_to_dict(x, recurse=False) for x in all_players]
|
||||
}
|
||||
db.close()
|
||||
return return_players
|
||||
|
||||
|
||||
@router.get('/{player_id}')
|
||||
def get_one_player(player_id: int, short_output: Optional[bool] = False):
|
||||
async def get_one_player(player_id: int, short_output: Optional[bool] = False):
|
||||
this_player = Player.get_or_none(Player.id == player_id)
|
||||
if this_player:
|
||||
r_player = model_to_dict(this_player, recurse=not short_output)
|
||||
r_player = model_to_dict(this_player)
|
||||
else:
|
||||
r_player = None
|
||||
db.close()
|
||||
@ -92,74 +92,23 @@ def get_one_player(player_id: int, short_output: Optional[bool] = False):
|
||||
|
||||
|
||||
@router.patch('/{player_id}')
|
||||
def patch_player(
|
||||
player_id: int, wara: Optional[int] = None, image: Optional[str] = None, image2: Optional[str] = None,
|
||||
team_id: Optional[int] = None, season: Optional[int] = None, last_game: Optional[str] = None,
|
||||
last_game2: Optional[str] = None, il_return: Optional[str] = None, demotion_week: Optional[int] = None,
|
||||
strat_code: Optional[str] = None, bbref_id: Optional[str] = None, injury_rating: Optional[str] = None,
|
||||
pos_1: Optional[str] = None, pos_2: Optional[str] = None, pos_3: Optional[str] = None,
|
||||
pos_4: Optional[str] = None, pos_5: Optional[str] = None, pos_6: Optional[str] = None,
|
||||
pos_7: Optional[str] = None, pos_8: Optional[str] = None, token: str = Depends(oauth2_scheme)):
|
||||
async def patch_player(
|
||||
player_id: int, new_player: PlayerModel, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_player - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
this_player = Player.get_or_none(Player.id == player_id)
|
||||
if not this_player:
|
||||
return None
|
||||
if Player.get_or_none(Player.id == player_id) is None:
|
||||
raise HTTPException(status_code=404, detail=f'Player ID {player_id} not found')
|
||||
|
||||
if wara is not None:
|
||||
this_player.wara = wara
|
||||
if image is not None:
|
||||
this_player.image = image
|
||||
if image2 is not None:
|
||||
this_player.image2 = image2
|
||||
if team_id is not None:
|
||||
this_player.team_id = team_id
|
||||
if season is not None:
|
||||
this_player.season = season
|
||||
if last_game is not None:
|
||||
this_player.last_game = last_game
|
||||
if last_game2 is not None:
|
||||
this_player.last_game2 = last_game2
|
||||
if il_return is not None:
|
||||
this_player.il_return = il_return
|
||||
if demotion_week is not None:
|
||||
this_player.demotion_week = demotion_week
|
||||
if strat_code is not None:
|
||||
this_player.strat_code = strat_code
|
||||
if bbref_id is not None:
|
||||
this_player.bbref_id = bbref_id
|
||||
if injury_rating is not None:
|
||||
this_player.injury_rating = injury_rating
|
||||
if pos_1 is not None:
|
||||
this_player.pos_1 = pos_1
|
||||
if pos_2 is not None:
|
||||
this_player.pos_2 = pos_2
|
||||
if pos_3 is not None:
|
||||
this_player.pos_3 = pos_3
|
||||
if pos_4 is not None:
|
||||
this_player.pos_4 = pos_4
|
||||
if pos_5 is not None:
|
||||
this_player.pos_5 = pos_5
|
||||
if pos_6 is not None:
|
||||
this_player.pos_6 = pos_6
|
||||
if pos_7 is not None:
|
||||
this_player.pos_7 = pos_7
|
||||
if pos_8 is not None:
|
||||
this_player.pos_8 = pos_8
|
||||
|
||||
if this_player.save() == 1:
|
||||
r_player = model_to_dict(this_player)
|
||||
db.close()
|
||||
return r_player
|
||||
else:
|
||||
db.close()
|
||||
raise HTTPException(status_code=500, detail=f'Unable to patch player {player_id}')
|
||||
Player.update(**new_player.dict()).where(Player.id == player_id).execute()
|
||||
r_player = model_to_dict(Player.get_by_id(player_id))
|
||||
db.close()
|
||||
return r_player
|
||||
|
||||
|
||||
@router.post('')
|
||||
def post_players(p_list: PlayerList, token: str = Depends(oauth2_scheme)):
|
||||
async def post_players(p_list: PlayerList, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'post_players - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
@ -185,7 +134,7 @@ def post_players(p_list: PlayerList, token: str = Depends(oauth2_scheme)):
|
||||
|
||||
|
||||
@router.delete('/{player_id}')
|
||||
def delete_player(player_id: int, token: str = Depends(oauth2_scheme)):
|
||||
async def delete_player(player_id: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'delete_player - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
@ -3,8 +3,8 @@ from typing import List, Optional
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from db_engine import db, Result, Team, model_to_dict, DatabaseError, chunked
|
||||
from dependencies import oauth2_scheme, valid_token, logging
|
||||
from ..db_engine import db, Result, Team, model_to_dict, chunked
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/results',
|
||||
@ -28,11 +28,10 @@ class ResultList(pydantic.BaseModel):
|
||||
|
||||
|
||||
@router.get('')
|
||||
def get_results(
|
||||
async def get_results(
|
||||
season: int, team_abbrev: list = Query(default=None), week_start: list = Query(default=None),
|
||||
week_end: list = Query(default=None), game_num: list = Query(default=None),
|
||||
away_abbrev: list = Query(default=None), home_abbrev: list = Query(default=None),
|
||||
short_output: Optional[bool] = False):
|
||||
away_abbrev: list = Query(default=None), home_abbrev: list = Query(default=None)):
|
||||
all_results = Result.select_season(season)
|
||||
|
||||
if team_abbrev is not None:
|
||||
@ -66,14 +65,14 @@ def get_results(
|
||||
|
||||
return_results = {
|
||||
'count': all_results.count(),
|
||||
'results': [model_to_dict(x, recurse=not short_output) for x in all_results]
|
||||
'results': [model_to_dict(x, recurse=False) for x in all_results]
|
||||
}
|
||||
db.close()
|
||||
return return_results
|
||||
|
||||
|
||||
@router.get('/{result_id}')
|
||||
def get_one_result(result_id: int, short_output: Optional[bool] = False):
|
||||
async def get_one_result(result_id: int, short_output: Optional[bool] = False):
|
||||
this_result = Result.get_or_none(Result.id == result_id)
|
||||
if this_result is not None:
|
||||
r_result = model_to_dict(this_result, recurse=not short_output)
|
||||
@ -84,7 +83,7 @@ def get_one_result(result_id: int, short_output: Optional[bool] = False):
|
||||
|
||||
|
||||
@router.patch('/{result_id}')
|
||||
def patch_result(
|
||||
async def patch_result(
|
||||
result_id: int, week_num: Optional[int] = None, game_num: Optional[int] = None,
|
||||
away_team_id: Optional[int] = None, home_team_id: Optional[int] = None, away_score: Optional[int] = None,
|
||||
home_score: Optional[int] = None, season: Optional[int] = None, scorecard_url: Optional[str] = None,
|
||||
@ -131,7 +130,7 @@ def patch_result(
|
||||
|
||||
|
||||
@router.post('')
|
||||
def post_results(result_list: ResultList, token: str = Depends(oauth2_scheme)):
|
||||
async def post_results(result_list: ResultList, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_player - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
@ -154,7 +153,7 @@ def post_results(result_list: ResultList, token: str = Depends(oauth2_scheme)):
|
||||
|
||||
|
||||
@router.delete('/{result_id}')
|
||||
def delete_result(result_id: int, token: str = Depends(oauth2_scheme)):
|
||||
async def delete_result(result_id: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'delete_result - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
@ -3,8 +3,8 @@ from typing import List, Optional
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from db_engine import db, Schedule, Team, model_to_dict, DatabaseError, chunked
|
||||
from dependencies import oauth2_scheme, valid_token, logging
|
||||
from ..db_engine import db, Schedule, Team, model_to_dict, chunked
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/schedules',
|
||||
@ -25,10 +25,10 @@ class ScheduleList(pydantic.BaseModel):
|
||||
|
||||
|
||||
@router.get('')
|
||||
def get_schedules(
|
||||
async def get_schedules(
|
||||
season: int, team_abbrev: list = Query(default=None), away_abbrev: list = Query(default=None),
|
||||
home_abbrev: list = Query(default=None), week_start: Optional[int] = None, week_end: Optional[int] = None,
|
||||
short_output: Optional[bool] = False):
|
||||
short_output: Optional[bool] = True):
|
||||
all_sched = Schedule.select_season(season)
|
||||
|
||||
if team_abbrev is not None:
|
||||
@ -68,10 +68,10 @@ def get_schedules(
|
||||
|
||||
|
||||
@router.get('/{schedule_id}')
|
||||
def get_one_schedule(schedule_id: int, short_output: Optional[bool] = False):
|
||||
async def get_one_schedule(schedule_id: int):
|
||||
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
|
||||
if this_sched is not None:
|
||||
r_sched = model_to_dict(this_sched, recurse=not short_output)
|
||||
r_sched = model_to_dict(this_sched)
|
||||
else:
|
||||
r_sched = None
|
||||
db.close()
|
||||
@ -79,9 +79,14 @@ def get_one_schedule(schedule_id: int, short_output: Optional[bool] = False):
|
||||
|
||||
|
||||
@router.patch('/{schedule_id}')
|
||||
def patch_schedule(
|
||||
async def patch_schedule(
|
||||
schedule_id: int, week: list = Query(default=None), awayteam_id: Optional[int] = None,
|
||||
hometeam_id: Optional[int] = None, gamecount: Optional[int] = None, season: Optional[int] = None):
|
||||
hometeam_id: Optional[int] = None, gamecount: Optional[int] = None, season: Optional[int] = None,
|
||||
token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_schedule - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
|
||||
if this_sched is None:
|
||||
raise HTTPException(status_code=404, detail=f'Schedule ID {schedule_id} not found')
|
||||
@ -111,7 +116,11 @@ def patch_schedule(
|
||||
|
||||
|
||||
@router.post('')
|
||||
def post_schedules(sched_list: ScheduleList):
|
||||
async def post_schedules(sched_list: ScheduleList, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'post_schedules - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
new_sched = []
|
||||
for x in sched_list.schedules:
|
||||
if Team.get_or_none(Team.id == x.awayteam_id) is None:
|
||||
@ -130,7 +139,11 @@ def post_schedules(sched_list: ScheduleList):
|
||||
|
||||
|
||||
@router.delete('/{schedule_id}')
|
||||
def delete_schedule(schedule_id: int):
|
||||
async def delete_schedule(schedule_id: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'delete_schedule - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
|
||||
if this_sched is None:
|
||||
raise HTTPException(status_code=404, detail=f'Schedule ID {schedule_id} not found')
|
||||
|
||||
@ -3,8 +3,8 @@ from typing import List, Optional
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from db_engine import db, Standings, Team, Division, model_to_dict, chunked, fn
|
||||
from dependencies import oauth2_scheme, valid_token, logging
|
||||
from ..db_engine import db, Standings, Team, Division, model_to_dict, chunked, fn
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/standings',
|
||||
@ -13,8 +13,8 @@ router = APIRouter(
|
||||
|
||||
|
||||
@router.get('')
|
||||
async def v1_standings(
|
||||
season: int, team_abbrev: Optional[str] = None, league_abbrev: Optional[str] = None,
|
||||
async def get_standings(
|
||||
season: int, team_abbrev: list = Query(default=None), league_abbrev: Optional[str] = None,
|
||||
division_abbrev: Optional[str] = None, short_output: Optional[bool] = False):
|
||||
standings = Standings.select_season(season)
|
||||
|
||||
@ -22,29 +22,17 @@ async def v1_standings(
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'No output for season {season}')
|
||||
|
||||
if team_abbrev:
|
||||
this_team = Team.get_season(team_abbrev, season)
|
||||
if not this_team:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'Team {team_abbrev} not found')
|
||||
|
||||
standings = standings.where(Standings.team == this_team)
|
||||
if team_abbrev is not None:
|
||||
t_query = Team.select().where(fn.Lower(Team.abbrev) << [x.lower() for x in team_abbrev])
|
||||
standings = standings.where(Standings.team << t_query)
|
||||
|
||||
if league_abbrev:
|
||||
these_divisions = Division.select().where(fn.Lower(Division.league_abbrev) == league_abbrev.lower())
|
||||
if these_divisions.count() == 0:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'No output for league {league_abbrev}')
|
||||
|
||||
standings = standings.where(Standings.team.division << these_divisions)
|
||||
l_query = Division.select().where(fn.Lower(Division.league_abbrev) << league_abbrev.lower())
|
||||
standings = standings.where(Standings.team.division << l_query)
|
||||
|
||||
if division_abbrev:
|
||||
this_division = Division.select().where(fn.Lower(Division.division_abbrev) == division_abbrev.lower())
|
||||
if not this_division:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'No output for division {division_abbrev}')
|
||||
|
||||
standings = standings.where(Standings.team.division << this_division)
|
||||
d_query = Division.select().where(fn.Lower(Division.division_abbrev) << division_abbrev.lower())
|
||||
standings = standings.where(Standings.team.division << d_query)
|
||||
|
||||
def win_pct(this_team_stan):
|
||||
if this_team_stan.wins + this_team_stan.losses == 0:
|
||||
@ -65,11 +53,11 @@ async def v1_standings(
|
||||
return return_standings
|
||||
|
||||
|
||||
@router.patch('/api/v1/standings/{stan_id}')
|
||||
@router.patch('/{stan_id}')
|
||||
async def patch_standings(
|
||||
stan_id, wins: Optional[int] = None, losses: Optional[int] = None, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_player - Bad Token: {token}')
|
||||
logging.warning(f'patch_standings - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
try:
|
||||
@ -89,7 +77,7 @@ async def patch_standings(
|
||||
return model_to_dict(this_stan)
|
||||
|
||||
|
||||
@router.post('/api/v1/standings/s{season}/recalculate')
|
||||
@router.post('/s{season}/recalculate')
|
||||
async def recalculate_standings(season: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'recalculate_standings - Bad Token: {token}')
|
||||
|
||||
@ -3,8 +3,8 @@ from typing import List, Optional
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from db_engine import db, Team, Manager, Division, model_to_dict, chunked
|
||||
from dependencies import oauth2_scheme, valid_token, logging
|
||||
from ..db_engine import db, Team, Manager, Division, model_to_dict, chunked
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/teams',
|
||||
@ -33,7 +33,7 @@ class TeamList(pydantic.BaseModel):
|
||||
|
||||
|
||||
@router.get('')
|
||||
def get_teams(
|
||||
async def get_teams(
|
||||
season: Optional[int] = None, owner_id: Optional[int] = None, manager_id: Optional[int] = None,
|
||||
abbrev: Optional[str] = None, active_only: Optional[bool] = False, short_output: Optional[bool] = False):
|
||||
if season is not None:
|
||||
@ -66,10 +66,10 @@ def get_teams(
|
||||
|
||||
|
||||
@router.get('/{team_id}')
|
||||
def get_one_team(team_id: int, short_output: Optional[bool] = False):
|
||||
async def get_one_team(team_id: int):
|
||||
this_team = Team.get_or_none(Team.id == team_id)
|
||||
if this_team:
|
||||
r_team = model_to_dict(this_team, recurse=not short_output)
|
||||
r_team = model_to_dict(this_team)
|
||||
else:
|
||||
r_team = None
|
||||
db.close()
|
||||
@ -77,12 +77,16 @@ def get_one_team(team_id: int, short_output: Optional[bool] = False):
|
||||
|
||||
|
||||
@router.get('/{team_id}')
|
||||
def patch_team(
|
||||
async def patch_team(
|
||||
team_id: int, manager1_id: Optional[int] = None, manager2_id: Optional[int] = None, gmid: Optional[int] = None,
|
||||
gmid2: Optional[int] = None, mascot: Optional[str] = None, stadium: Optional[str] = None,
|
||||
thumbnail: Optional[str] = None, color: Optional[str] = None, abbrev: Optional[str] = None,
|
||||
sname: Optional[str] = None, lname: Optional[str] = None, dice_color: Optional[str] = None,
|
||||
division_id: Optional[int] = None):
|
||||
division_id: Optional[int] = None, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_team - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
this_team = Team.get_or_none(Team.id == team_id)
|
||||
if not this_team:
|
||||
return None
|
||||
@ -151,7 +155,11 @@ def patch_team(
|
||||
|
||||
|
||||
@router.get('')
|
||||
def post_team(team_list: TeamList):
|
||||
async def post_team(team_list: TeamList, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'post_team - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
new_teams = []
|
||||
for team in team_list.teams:
|
||||
dupe_team = Team.get_or_none(Team.season == team.season, Team.abbrev == team.abbrev)
|
||||
@ -184,7 +192,11 @@ def post_team(team_list: TeamList):
|
||||
|
||||
|
||||
@router.get('/{team_id}')
|
||||
def delete_team(team_id: int):
|
||||
async def delete_team(team_id: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'delete_team - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
|
||||
this_team = Team.get_or_none(Team.id == team_id)
|
||||
if not this_team:
|
||||
db.close()
|
||||
|
||||
@ -4,8 +4,8 @@ from pandas import DataFrame
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from db_engine import db, Transaction, Team, Player, model_to_dict, chunked, fn
|
||||
from dependencies import oauth2_scheme, valid_token, logging
|
||||
from ..db_engine import db, Transaction, Team, Player, model_to_dict, chunked, fn
|
||||
from ..dependencies import oauth2_scheme, valid_token
|
||||
|
||||
router = APIRouter(
|
||||
prefix='/api/v3/transactions',
|
||||
@ -40,10 +40,6 @@ async def get_transactions(
|
||||
else:
|
||||
transactions = Transaction.select()
|
||||
|
||||
# if transactions.count() == 0:
|
||||
# db.close()
|
||||
# raise HTTPException(status_code=404, detail=f'Season {season} not found')
|
||||
|
||||
if team_abbrev:
|
||||
these_teams = Team.select().where(
|
||||
(Team.abbrev == team_abbrev.upper()) |
|
||||
|
||||
Loading…
Reference in New Issue
Block a user