Added cardpositions
This commit is contained in:
parent
6183a125bc
commit
995735d878
@ -590,7 +590,9 @@ class BattingCardRatings(BaseModel):
|
||||
slg = FloatField(null=True)
|
||||
|
||||
|
||||
bcr_index = ModelIndex(BattingCardRatings, (BattingCardRatings.battingcard, BattingCardRatings.vs_hand), unique=True)
|
||||
bcr_index = ModelIndex(
|
||||
BattingCardRatings, (BattingCardRatings.battingcard, BattingCardRatings.vs_hand), unique=True
|
||||
)
|
||||
BattingCardRatings.add_index(bcr_index)
|
||||
|
||||
|
||||
@ -646,14 +648,15 @@ class PitchingCardRatings(BaseModel):
|
||||
slg = FloatField(null=True)
|
||||
|
||||
|
||||
pcr_index = ModelIndex(PitchingCardRatings, (PitchingCardRatings.pitchingcard, PitchingCardRatings.vs_hand), unique=True)
|
||||
pcr_index = ModelIndex(
|
||||
PitchingCardRatings, (PitchingCardRatings.pitchingcard, PitchingCardRatings.vs_hand), unique=True
|
||||
)
|
||||
PitchingCardRatings.add_index(pcr_index)
|
||||
|
||||
|
||||
class CardPosition(BaseModel):
|
||||
player = ForeignKeyField(Player)
|
||||
batting = ForeignKeyField(BattingCard, null=True)
|
||||
pitching = ForeignKeyField(PitchingCard, null=True)
|
||||
variant = IntegerField()
|
||||
position = CharField()
|
||||
innings = IntegerField()
|
||||
range = IntegerField()
|
||||
@ -663,6 +666,12 @@ class CardPosition(BaseModel):
|
||||
overthrow = IntegerField(null=True)
|
||||
|
||||
|
||||
pos_index = ModelIndex(
|
||||
CardPosition, (CardPosition.player, CardPosition.variant, CardPosition.position), unique=True
|
||||
)
|
||||
PitchingCardRatings.add_index(pos_index)
|
||||
|
||||
|
||||
db.create_tables([BattingCard, BattingCardRatings, PitchingCard, PitchingCardRatings, CardPosition])
|
||||
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@ from fastapi import FastAPI
|
||||
from.routers_v2 import (
|
||||
current, teams, rarity, cardsets, players, packtypes, packs, cards, events, results, rewards,
|
||||
batstats, pitstats, notifications, paperdex, gamerewards, gauntletrewards, gauntletruns, battingcards,
|
||||
battingcardratings, pitchingcards, pitchingcardratings)
|
||||
battingcardratings, pitchingcards, pitchingcardratings, cardpositions)
|
||||
|
||||
app = FastAPI(
|
||||
responses={404: {'description': 'Not found'}}
|
||||
@ -31,3 +31,4 @@ app.include_router(battingcards.router)
|
||||
app.include_router(battingcardratings.router)
|
||||
app.include_router(pitchingcards.router)
|
||||
app.include_router(pitchingcardratings.router)
|
||||
app.include_router(cardpositions.router)
|
||||
|
||||
148
app/routers_v2/cardpositions.py
Normal file
148
app/routers_v2/cardpositions.py
Normal file
@ -0,0 +1,148 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from typing import Literal, Optional, List
|
||||
import logging
|
||||
import pydantic
|
||||
from pydantic import root_validator
|
||||
|
||||
from ..db_engine import db, CardPosition, model_to_dict, chunked, Player
|
||||
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/cardpositions',
|
||||
tags=['cardpositions']
|
||||
)
|
||||
|
||||
|
||||
class CardPositionModel(pydantic.BaseModel):
|
||||
player_id: int
|
||||
variant: int = 0
|
||||
position: Literal['P', 'C', '1B', '2B', '3B', 'SS', 'LF', 'CF', 'RF']
|
||||
innings: int = 1
|
||||
range: int = 5
|
||||
error: int = 0
|
||||
arm: Optional[int] = None
|
||||
pb: Optional[int] = None
|
||||
overthrow: Optional[int] = None
|
||||
|
||||
@root_validator
|
||||
def position_validator(cls, values):
|
||||
if values['position'] in ['C', 'LF', 'CF', 'RF'] and values['arm'] is None:
|
||||
raise ValueError(f'{values["position"]} must have an arm rating')
|
||||
if values['position'] == 'C' and (values['pb'] is None or values['overthrow'] is None):
|
||||
raise ValueError('Catchers must have a pb and overthrow rating')
|
||||
return values
|
||||
|
||||
|
||||
class PositionList(pydantic.BaseModel):
|
||||
positions: List[CardPositionModel]
|
||||
|
||||
|
||||
@router.get('')
|
||||
async def get_card_positions(
|
||||
player_id: Optional[int] = None, position: list = Query(default=None), min_innings: Optional[int] = 1,
|
||||
r: list = Query(default=None), e: list = Query(default=None), arm: list = Query(default=None),
|
||||
pb: list = Query(default=None), overthrow: list = Query(default=None), cardset_id: list = Query(default=None),
|
||||
short_output: Optional[bool] = False):
|
||||
all_pos = CardPosition.select().where(CardPosition.innings >= min_innings).order_by(
|
||||
CardPosition.player, CardPosition.position, CardPosition.variant
|
||||
)
|
||||
|
||||
if player_id is not None:
|
||||
all_pos = all_pos.where(CardPosition.player_id << player_id)
|
||||
if position is not None:
|
||||
all_pos = all_pos.where(CardPosition.position << position)
|
||||
if r is not None:
|
||||
all_pos = all_pos.where(CardPosition.range << r)
|
||||
if e is not None:
|
||||
all_pos = all_pos.where(CardPosition.error << e)
|
||||
if arm is not None:
|
||||
all_pos = all_pos.where(CardPosition.arm << arm)
|
||||
if pb is not None:
|
||||
all_pos = all_pos.where(CardPosition.pb << pb)
|
||||
if overthrow is not None:
|
||||
all_pos = all_pos.where(CardPosition.overthrow << overthrow)
|
||||
if position is not None:
|
||||
all_players = Player.select().where(Player.cardset_id << cardset_id)
|
||||
all_pos = all_pos.where(CardPosition.player << all_players)
|
||||
|
||||
return_val = {'count': all_pos.count(), 'positions': [
|
||||
model_to_dict(x, recurse=not short_output) for x in all_pos
|
||||
]}
|
||||
db.close()
|
||||
return return_val
|
||||
|
||||
|
||||
@router.get('/{position_id}')
|
||||
async def get_one_position(position_id: int):
|
||||
this_pos = CardPosition.get_or_none(CardPosition.id == position_id)
|
||||
if this_pos is None:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'CardPosition id {position_id} not found')
|
||||
|
||||
r_data = model_to_dict(this_pos)
|
||||
db.close()
|
||||
return r_data
|
||||
|
||||
|
||||
@router.put('')
|
||||
async def put_positions(positions: PositionList, 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 card positions. This event has been logged.'
|
||||
)
|
||||
|
||||
new_cards = []
|
||||
updates = 0
|
||||
|
||||
for x in positions.positions:
|
||||
try:
|
||||
CardPosition.get(
|
||||
(CardPosition.player_id == x.player_id) & (CardPosition.variant == x.variant) &
|
||||
(CardPosition.position == x.position)
|
||||
)
|
||||
updates += CardPosition.update(x.dict()).where(
|
||||
(CardPosition.player_id == x.player_id) & (CardPosition.variant == x.variant) &
|
||||
(CardPosition.position == x.position)
|
||||
).execute()
|
||||
except CardPosition.DoesNotExist:
|
||||
new_cards.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
for batch in chunked(new_cards, 30):
|
||||
CardPosition.insert_many(batch).on_conflict_replace().execute()
|
||||
|
||||
db.close()
|
||||
return f'Updated cards: {updates}; new cards: {len(new_cards)}'
|
||||
|
||||
|
||||
@router.delete('/{position_id}')
|
||||
async def delete_position(position_id: int, 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 card positions. This event has been logged.'
|
||||
)
|
||||
|
||||
this_pos = CardPosition.get_or_none(CardPosition.id == position_id)
|
||||
if this_pos is None:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail=f'CardPosition id {position_id} not found')
|
||||
|
||||
count = this_pos.delete_instance()
|
||||
db.close()
|
||||
|
||||
if count == 1:
|
||||
return f'Card Position {this_pos} has been deleted'
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail=f'Card Position {this_pos} could not be deleted')
|
||||
@ -162,7 +162,7 @@ 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 patch batting cards. This event has been logged.'
|
||||
detail='You are not authorized to delete batting cards. This event has been logged.'
|
||||
)
|
||||
|
||||
this_card = PitchingCard.get_or_none(PitchingCard.id == card_id)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user