Pitching card updates for card gen
This commit is contained in:
parent
d4f26a6a2e
commit
561c7e99d4
@ -123,7 +123,7 @@ async def put_cards(cards: BattingCardList, token: str = Depends(oauth2_scheme))
|
||||
else:
|
||||
logging.info(f'randomly setting offense_col for {this_player.p_name}')
|
||||
x.offense_col = random.randint(1, 3)
|
||||
logging.info(f'x.dict(): {x.dict()}')
|
||||
logging.debug(f'x.dict(): {x.dict()}')
|
||||
new_cards.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from typing import Literal, Optional, List
|
||||
import logging
|
||||
import pydantic
|
||||
from pydantic import validator, root_validator
|
||||
|
||||
from ..db_engine import db, PitchingCardRatings, model_to_dict, chunked, PitchingCard
|
||||
from ..db_engine import db, PitchingCardRatings, model_to_dict, chunked, PitchingCard, Player, query_to_csv
|
||||
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
||||
|
||||
logging.basicConfig(
|
||||
@ -91,7 +91,8 @@ class RatingsList(pydantic.BaseModel):
|
||||
@router.get('')
|
||||
async def get_card_ratings(
|
||||
pitchingcard_id: list = Query(default=None), vs_hand: Literal['R', 'L', 'vR', 'vL'] = None,
|
||||
short_output: bool = False, token: str = Depends(oauth2_scheme)):
|
||||
short_output: bool = False, csv: bool = False, cardset_id: list = Query(default=None),
|
||||
token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'Bad Token: {token}')
|
||||
db.close()
|
||||
@ -106,12 +107,22 @@ async def get_card_ratings(
|
||||
all_ratings = all_ratings.where(PitchingCardRatings.pitchingcard_id << pitchingcard_id)
|
||||
if vs_hand is not None:
|
||||
all_ratings = all_ratings.where(PitchingCardRatings.vs_hand << vs_hand[-1])
|
||||
if cardset_id is not None:
|
||||
set_players = Player.select(Player.player_id).where(Player.cardset_id << cardset_id)
|
||||
set_cards = PitchingCard.select(PitchingCard.id).where(PitchingCard.player << set_players)
|
||||
all_ratings = all_ratings.where(PitchingCardRatings.battingcard << set_cards)
|
||||
|
||||
return_val = {'count': all_ratings.count(), 'ratings': [
|
||||
model_to_dict(x, recurse=not short_output) for x in all_ratings
|
||||
]}
|
||||
db.close()
|
||||
return return_val
|
||||
if csv:
|
||||
return_val = query_to_csv(all_ratings)
|
||||
db.close()
|
||||
return Response(content=return_val, media_type='text/csv')
|
||||
|
||||
else:
|
||||
return_val = {'count': all_ratings.count(), 'ratings': [
|
||||
model_to_dict(x, recurse=not short_output) for x in all_ratings
|
||||
]}
|
||||
db.close()
|
||||
return return_val
|
||||
|
||||
|
||||
@router.get('/{ratings_id}')
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
import random
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from typing import Literal, Optional, List
|
||||
import logging
|
||||
import pydantic
|
||||
|
||||
from ..db_engine import db, PitchingCard, model_to_dict, chunked, Player
|
||||
from ..db_engine import db, PitchingCard, model_to_dict, chunked, Player, fn, MlbPlayer
|
||||
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
||||
|
||||
logging.basicConfig(
|
||||
@ -28,7 +30,7 @@ class PitchingCardModel(pydantic.BaseModel):
|
||||
relief_rating: int = 0
|
||||
closer_rating: int = None
|
||||
batting: str = "#1WR-C"
|
||||
offense_col: int
|
||||
offense_col: int = None
|
||||
hand: Literal['R', 'L', 'S'] = 'R'
|
||||
|
||||
|
||||
@ -38,13 +40,21 @@ class PitchingCardList(pydantic.BaseModel):
|
||||
|
||||
@router.get('')
|
||||
async def get_pitching_cards(
|
||||
player_id: list = Query(default=None), cardset_id: list = Query(default=None), short_output: bool = False):
|
||||
player_id: list = Query(default=None), player_name: list = Query(default=None),
|
||||
cardset_id: list = Query(default=None), short_output: bool = False, limit: Optional[int] = None):
|
||||
all_cards = PitchingCard.select()
|
||||
if player_id is not None:
|
||||
all_cards = all_cards.where(PitchingCard.player_id << player_id)
|
||||
if cardset_id is not None:
|
||||
all_players = Player.select().where(Player.cardset_id << cardset_id)
|
||||
all_cards = all_cards.where(PitchingCard.player << all_players)
|
||||
if player_name is not None:
|
||||
name_list = [x.lower() for x in player_name]
|
||||
all_players = Player.select().where(fn.lower(Player.p_name) << name_list)
|
||||
all_cards = all_cards.where(PitchingCard.player << all_players)
|
||||
|
||||
if limit is not None:
|
||||
all_cards = all_cards.limit(limit)
|
||||
|
||||
return_val = {'count': all_cards.count(), 'cards': [
|
||||
model_to_dict(x, recurse=not short_output) for x in all_cards
|
||||
@ -85,7 +95,7 @@ async def put_cards(cards: PitchingCardList, token: str = Depends(oauth2_scheme)
|
||||
db.close()
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail='You are not authorized to post batting cards. This event has been logged.'
|
||||
detail='You are not authorized to post pitching cards. This event has been logged.'
|
||||
)
|
||||
|
||||
new_cards = []
|
||||
@ -93,13 +103,26 @@ async def put_cards(cards: PitchingCardList, token: str = Depends(oauth2_scheme)
|
||||
|
||||
for x in cards.cards:
|
||||
try:
|
||||
PitchingCard.get(
|
||||
old = PitchingCard.get(
|
||||
(PitchingCard.player_id == x.player_id) & (PitchingCard.variant == x.variant)
|
||||
)
|
||||
|
||||
if x.offense_col is None:
|
||||
x.offense_col = old.offense_col
|
||||
updates += PitchingCard.update(x.dict()).where(
|
||||
(PitchingCard.player_id == x.player_id) & (PitchingCard.variant == x.variant)
|
||||
).execute()
|
||||
except PitchingCard.DoesNotExist:
|
||||
if x.offense_col is None:
|
||||
this_player = Player.get_or_none(Player.player_id == x.player_id)
|
||||
mlb_player = MlbPlayer.get_or_none(MlbPlayer.key_bbref == this_player.bbref_id)
|
||||
if mlb_player is not None:
|
||||
logging.info(f'setting offense_col to {mlb_player.offense_col} for {this_player.p_name}')
|
||||
x.offense_col = mlb_player.offense_col
|
||||
else:
|
||||
logging.info(f'randomly setting offense_col for {this_player.p_name}')
|
||||
x.offense_col = random.randint(1, 3)
|
||||
logging.debug(f'x.dict(): {x.dict()}')
|
||||
new_cards.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
@ -162,13 +185,13 @@ async def delete_card(card_id: int, token: str = Depends(oauth2_scheme)):
|
||||
db.close()
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail='You are not authorized to delete batting cards. This event has been logged.'
|
||||
detail='You are not authorized to delete pitching cards. This event has been logged.'
|
||||
)
|
||||
|
||||
this_card = PitchingCard.get_or_none(PitchingCard.id == card_id)
|
||||
if this_card is None:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'BattingCard id {card_id} not found')
|
||||
raise HTTPException(status_code=404, detail=f'Pitching id {card_id} not found')
|
||||
|
||||
count = this_card.delete_instance()
|
||||
db.close()
|
||||
@ -177,3 +200,19 @@ async def delete_card(card_id: int, token: str = Depends(oauth2_scheme)):
|
||||
return f'Card {this_card} has been deleted'
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail=f'Card {this_card} could not be deleted')
|
||||
|
||||
|
||||
@router.delete('')
|
||||
async def delete_all_cards(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 pitching cards. This event has been logged.'
|
||||
)
|
||||
|
||||
d_query = PitchingCard.delete()
|
||||
d_query.execute()
|
||||
|
||||
return f'Deleted {d_query.count()} pitching cards'
|
||||
|
||||
Loading…
Reference in New Issue
Block a user