Cards and ratings updates
This commit is contained in:
parent
51a5251c92
commit
6183a125bc
@ -129,7 +129,7 @@ async def get_one_rating(ratings_id: int, token: str = Depends(oauth2_scheme)):
|
||||
return r_data
|
||||
|
||||
|
||||
@router.get('/{player_id}')
|
||||
@router.get('/player/{player_id}')
|
||||
async def get_player_ratings(player_id: int, variant: list = Query(default=None), short_output: bool = False):
|
||||
all_cards = BattingCard.select().where(BattingCard.player_id == player_id).order_by(BattingCard.variant)
|
||||
if variant is not None:
|
||||
@ -154,17 +154,29 @@ async def put_ratings(ratings: RatingsList, token: str = Depends(oauth2_scheme))
|
||||
detail='You are not authorized to post card ratings.'
|
||||
)
|
||||
|
||||
new_ratings = [x.dict() for x in ratings.ratings]
|
||||
new_ratings = []
|
||||
updates = 0
|
||||
for x in ratings.ratings:
|
||||
try:
|
||||
BattingCardRatings.get(
|
||||
(BattingCardRatings.battingcard_id == x.battingcard_id) & (BattingCardRatings.vs_hand == x.vs_hand)
|
||||
)
|
||||
updates += BattingCardRatings.update(x.dict()).where(
|
||||
(BattingCardRatings.battingcard_id == x.battingcard_id) & (BattingCardRatings.vs_hand == x.vs_hand)
|
||||
).execute()
|
||||
except BattingCardRatings.DoesNotExist:
|
||||
new_ratings.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
for batch in chunked(new_ratings, 30):
|
||||
BattingCardRatings.insert_many(batch).on_conflict_replace().execute()
|
||||
|
||||
db.close()
|
||||
return f'Inserted {len(new_ratings)} batting ratings'
|
||||
return f'Updated ratings: {updates}; new ratings: {len(new_ratings)}'
|
||||
|
||||
|
||||
@router.delete('/{ratings_id}')
|
||||
async def put_one_rating(
|
||||
async def delete_rating(
|
||||
ratings_id: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'Bad Token: {token}')
|
||||
|
||||
@ -88,13 +88,26 @@ async def put_cards(cards: BattingCardList, token: str = Depends(oauth2_scheme))
|
||||
detail='You are not authorized to post batting cards. This event has been logged.'
|
||||
)
|
||||
|
||||
new_cards = [x.dict() for x in cards.cards]
|
||||
new_cards = []
|
||||
updates = 0
|
||||
|
||||
for x in cards.cards:
|
||||
try:
|
||||
BattingCard.get(
|
||||
(BattingCard.player_id == x.player_id) & (BattingCard.variant == x.variant)
|
||||
)
|
||||
updates += BattingCard.update(x.dict()).where(
|
||||
(BattingCard.player_id == x.player_id) & (BattingCard.variant == x.variant)
|
||||
).execute()
|
||||
except BattingCard.DoesNotExist:
|
||||
new_cards.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
for batch in chunked(new_cards, 30):
|
||||
BattingCard.insert_many(batch).on_conflict_replace().execute()
|
||||
|
||||
db.close()
|
||||
return f'Inserted {len(new_cards)} batting cards'
|
||||
return f'Updated cards: {updates}; new cards: {len(new_cards)}'
|
||||
|
||||
|
||||
@router.patch('/{card_id}')
|
||||
|
||||
@ -134,13 +134,13 @@ async def get_one_rating(ratings_id: int, token: str = Depends(oauth2_scheme)):
|
||||
return r_data
|
||||
|
||||
|
||||
@router.get('/{player_id}')
|
||||
@router.get('/player/{player_id}')
|
||||
async def get_player_ratings(player_id: int, variant: list = Query(default=None), short_output: bool = False):
|
||||
all_cards = PitchingCard.select().where(PitchingCard.player_id == player_id).order_by(PitchingCard.variant)
|
||||
if variant is not None:
|
||||
all_cards = all_cards.where(PitchingCard.variant << variant)
|
||||
|
||||
all_ratings = PitchingCardRatings.select().where(PitchingCardRatings.PitchingCard << all_cards)
|
||||
all_ratings = PitchingCardRatings.select().where(PitchingCardRatings.pitchingcard << all_cards)
|
||||
|
||||
return_val = {'count': all_ratings.count(), 'ratings': [
|
||||
model_to_dict(x, recurse=not short_output) for x in all_ratings
|
||||
@ -159,17 +159,29 @@ async def put_ratings(ratings: RatingsList, token: str = Depends(oauth2_scheme))
|
||||
detail='You are not authorized to post card ratings.'
|
||||
)
|
||||
|
||||
new_ratings = [x.dict() for x in ratings.ratings]
|
||||
new_ratings = []
|
||||
updates = 0
|
||||
for x in ratings.ratings:
|
||||
try:
|
||||
PitchingCardRatings.get(
|
||||
(PitchingCardRatings.pitchingcard_id == x.pitchingcard_id) & (PitchingCardRatings.vs_hand == x.vs_hand)
|
||||
)
|
||||
updates += PitchingCardRatings.update(x.dict()).where(
|
||||
(PitchingCardRatings.pitchingcard_id == x.pitchingcard_id) & (PitchingCardRatings.vs_hand == x.vs_hand)
|
||||
).execute()
|
||||
except PitchingCardRatings.DoesNotExist:
|
||||
new_ratings.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
for batch in chunked(new_ratings, 30):
|
||||
PitchingCardRatings.insert_many(batch).on_conflict_replace().execute() # TODO: replace gives new ID which breaks links
|
||||
PitchingCardRatings.insert_many(batch).on_conflict_replace().execute()
|
||||
|
||||
db.close()
|
||||
return f'Inserted {len(new_ratings)} batting ratings'
|
||||
return f'Updated ratings: {updates}; new ratings: {len(new_ratings)}'
|
||||
|
||||
|
||||
@router.delete('/{ratings_id}')
|
||||
async def put_one_rating(
|
||||
async def delete_rating(
|
||||
ratings_id: int, token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'Bad Token: {token}')
|
||||
|
||||
@ -88,13 +88,26 @@ async def put_cards(cards: PitchingCardList, token: str = Depends(oauth2_scheme)
|
||||
detail='You are not authorized to post batting cards. This event has been logged.'
|
||||
)
|
||||
|
||||
new_cards = [x.dict() for x in cards.cards]
|
||||
new_cards = []
|
||||
updates = 0
|
||||
|
||||
for x in cards.cards:
|
||||
try:
|
||||
PitchingCard.get(
|
||||
(PitchingCard.player_id == x.player_id) & (PitchingCard.variant == x.variant)
|
||||
)
|
||||
updates += PitchingCard.update(x.dict()).where(
|
||||
(PitchingCard.player_id == x.player_id) & (PitchingCard.variant == x.variant)
|
||||
).execute()
|
||||
except PitchingCard.DoesNotExist:
|
||||
new_cards.append(x.dict())
|
||||
|
||||
with db.atomic():
|
||||
for batch in chunked(new_cards, 30):
|
||||
PitchingCard.insert_many(batch).on_conflict_replace().execute()
|
||||
|
||||
db.close()
|
||||
return f'Inserted {len(new_cards)} batting cards'
|
||||
return f'Updated cards: {updates}; new cards: {len(new_cards)}'
|
||||
|
||||
|
||||
@router.patch('/{card_id}')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user