fix: replace manual db.close() calls with middleware-based connection management (#71)
All checks were successful
Build Docker Image / build (pull_request) Successful in 2m28s
All checks were successful
Build Docker Image / build (pull_request) Successful in 2m28s
Closes #71 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
da679b6d1a
commit
ab90dfc437
13
app/main.py
13
app/main.py
@ -70,6 +70,19 @@ app = FastAPI(
|
|||||||
logger.info(f"Starting up now...")
|
logger.info(f"Starting up now...")
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def db_connection_middleware(request: Request, call_next):
|
||||||
|
from .db_engine import db
|
||||||
|
|
||||||
|
db.connect(reuse_if_open=True)
|
||||||
|
try:
|
||||||
|
response = await call_next(request)
|
||||||
|
return response
|
||||||
|
finally:
|
||||||
|
if not db.is_closed():
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
app.include_router(current.router)
|
app.include_router(current.router)
|
||||||
app.include_router(players.router)
|
app.include_router(players.router)
|
||||||
app.include_router(results.router)
|
app.include_router(results.router)
|
||||||
|
|||||||
@ -71,7 +71,6 @@ async def get_awards(
|
|||||||
"count": all_awards.count(),
|
"count": all_awards.count(),
|
||||||
"awards": [model_to_dict(x, recurse=not short_output) for x in all_awards],
|
"awards": [model_to_dict(x, recurse=not short_output) for x in all_awards],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_awards
|
return return_awards
|
||||||
|
|
||||||
|
|
||||||
@ -80,10 +79,8 @@ async def get_awards(
|
|||||||
async def get_one_award(award_id: int, short_output: Optional[bool] = False):
|
async def get_one_award(award_id: int, short_output: Optional[bool] = False):
|
||||||
this_award = Award.get_or_none(Award.id == award_id)
|
this_award = Award.get_or_none(Award.id == award_id)
|
||||||
if this_award is None:
|
if this_award is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
||||||
|
|
||||||
db.close()
|
|
||||||
return model_to_dict(this_award, recurse=not short_output)
|
return model_to_dict(this_award, recurse=not short_output)
|
||||||
|
|
||||||
|
|
||||||
@ -107,7 +104,6 @@ async def patch_award(
|
|||||||
|
|
||||||
this_award = Award.get_or_none(Award.id == award_id)
|
this_award = Award.get_or_none(Award.id == award_id)
|
||||||
if this_award is None:
|
if this_award is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
||||||
|
|
||||||
if name is not None:
|
if name is not None:
|
||||||
@ -129,10 +125,8 @@ async def patch_award(
|
|||||||
|
|
||||||
if this_award.save() == 1:
|
if this_award.save() == 1:
|
||||||
r_award = model_to_dict(this_award)
|
r_award = model_to_dict(this_award)
|
||||||
db.close()
|
|
||||||
return r_award
|
return r_award
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to patch award {award_id}")
|
raise HTTPException(status_code=500, detail=f"Unable to patch award {award_id}")
|
||||||
|
|
||||||
|
|
||||||
@ -176,7 +170,6 @@ async def post_award(award_list: AwardList, token: str = Depends(oauth2_scheme))
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_awards, 15):
|
for batch in chunked(new_awards, 15):
|
||||||
Award.insert_many(batch).on_conflict_ignore().execute()
|
Award.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_awards)} awards"
|
return f"Inserted {len(new_awards)} awards"
|
||||||
|
|
||||||
@ -190,11 +183,9 @@ async def delete_award(award_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_award = Award.get_or_none(Award.id == award_id)
|
this_award = Award.get_or_none(Award.id == award_id)
|
||||||
if this_award is None:
|
if this_award is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
raise HTTPException(status_code=404, detail=f"Award ID {award_id} not found")
|
||||||
|
|
||||||
count = this_award.delete_instance()
|
count = this_award.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Award {award_id} has been deleted"
|
return f"Award {award_id} has been deleted"
|
||||||
|
|||||||
@ -91,17 +91,14 @@ async def get_batstats(
|
|||||||
if "post" in s_type.lower():
|
if "post" in s_type.lower():
|
||||||
all_stats = BattingStat.post_season(season)
|
all_stats = BattingStat.post_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
elif s_type.lower() in ["combined", "total", "all"]:
|
elif s_type.lower() in ["combined", "total", "all"]:
|
||||||
all_stats = BattingStat.combined_season(season)
|
all_stats = BattingStat.combined_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
else:
|
else:
|
||||||
all_stats = BattingStat.regular_season(season)
|
all_stats = BattingStat.regular_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
|
|
||||||
if position is not None:
|
if position is not None:
|
||||||
@ -127,7 +124,6 @@ async def get_batstats(
|
|||||||
if week_end is not None:
|
if week_end is not None:
|
||||||
end = min(week_end, end)
|
end = min(week_end, end)
|
||||||
if start > end:
|
if start > end:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404,
|
status_code=404,
|
||||||
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
||||||
@ -146,7 +142,6 @@ async def get_batstats(
|
|||||||
# 'stats': [{'id': x.id} for x in all_stats]
|
# 'stats': [{'id': x.id} for x in all_stats]
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -344,7 +339,6 @@ async def get_totalstats(
|
|||||||
"bplo": x.sum_bplo,
|
"bplo": x.sum_bplo,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -368,7 +362,6 @@ async def patch_batstats(
|
|||||||
|
|
||||||
BattingStat.update(**new_stats.dict()).where(BattingStat.id == stat_id).execute()
|
BattingStat.update(**new_stats.dict()).where(BattingStat.id == stat_id).execute()
|
||||||
r_stat = model_to_dict(BattingStat.get_by_id(stat_id))
|
r_stat = model_to_dict(BattingStat.get_by_id(stat_id))
|
||||||
db.close()
|
|
||||||
return r_stat
|
return r_stat
|
||||||
|
|
||||||
|
|
||||||
@ -412,5 +405,4 @@ async def post_batstats(s_list: BatStatList, token: str = Depends(oauth2_scheme)
|
|||||||
|
|
||||||
# Update career stats
|
# Update career stats
|
||||||
|
|
||||||
db.close()
|
|
||||||
return f"Added {len(all_stats)} batting lines"
|
return f"Added {len(all_stats)} batting lines"
|
||||||
|
|||||||
@ -41,7 +41,6 @@ async def get_current(season: Optional[int] = None):
|
|||||||
|
|
||||||
if current is not None:
|
if current is not None:
|
||||||
r_curr = model_to_dict(current)
|
r_curr = model_to_dict(current)
|
||||||
db.close()
|
|
||||||
return r_curr
|
return r_curr
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
@ -100,10 +99,8 @@ async def patch_current(
|
|||||||
|
|
||||||
if current.save():
|
if current.save():
|
||||||
r_curr = model_to_dict(current)
|
r_curr = model_to_dict(current)
|
||||||
db.close()
|
|
||||||
return r_curr
|
return r_curr
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch current {current_id}"
|
status_code=500, detail=f"Unable to patch current {current_id}"
|
||||||
)
|
)
|
||||||
@ -120,10 +117,8 @@ async def post_current(new_current: CurrentModel, token: str = Depends(oauth2_sc
|
|||||||
|
|
||||||
if this_current.save():
|
if this_current.save():
|
||||||
r_curr = model_to_dict(this_current)
|
r_curr = model_to_dict(this_current)
|
||||||
db.close()
|
|
||||||
return r_curr
|
return r_curr
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
detail=f"Unable to post season {new_current.season} current",
|
detail=f"Unable to post season {new_current.season} current",
|
||||||
|
|||||||
@ -364,7 +364,6 @@ async def get_custom_commands(
|
|||||||
logger.error(f"Error getting custom commands: {e}")
|
logger.error(f"Error getting custom commands: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
# Move this route to after the specific string routes
|
# Move this route to after the specific string routes
|
||||||
@ -430,7 +429,6 @@ async def create_custom_command_endpoint(
|
|||||||
logger.error(f"Error creating custom command: {e}")
|
logger.error(f"Error creating custom command: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -491,7 +489,6 @@ async def update_custom_command_endpoint(
|
|||||||
logger.error(f"Error updating custom command {command_id}: {e}")
|
logger.error(f"Error updating custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -576,7 +573,6 @@ async def patch_custom_command(
|
|||||||
logger.error(f"Error patching custom command {command_id}: {e}")
|
logger.error(f"Error patching custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -613,7 +609,6 @@ async def delete_custom_command_endpoint(
|
|||||||
logger.error(f"Error deleting custom command {command_id}: {e}")
|
logger.error(f"Error deleting custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
# Creator endpoints
|
# Creator endpoints
|
||||||
@ -684,7 +679,6 @@ async def get_creators(
|
|||||||
logger.error(f"Error getting creators: {e}")
|
logger.error(f"Error getting creators: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/creators", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post("/creators", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -729,7 +723,6 @@ async def create_creator_endpoint(
|
|||||||
logger.error(f"Error creating creator: {e}")
|
logger.error(f"Error creating creator: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats")
|
@router.get("/stats")
|
||||||
@ -855,7 +848,6 @@ async def get_custom_command_stats():
|
|||||||
logger.error(f"Error getting custom command stats: {e}")
|
logger.error(f"Error getting custom command stats: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
# Special endpoints for Discord bot integration
|
# Special endpoints for Discord bot integration
|
||||||
@ -922,7 +914,6 @@ async def get_custom_command_by_name_endpoint(command_name: str):
|
|||||||
logger.error(f"Error getting custom command by name '{command_name}': {e}")
|
logger.error(f"Error getting custom command by name '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/by_name/{command_name}/execute", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/by_name/{command_name}/execute", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -991,7 +982,6 @@ async def execute_custom_command(
|
|||||||
logger.error(f"Error executing custom command '{command_name}': {e}")
|
logger.error(f"Error executing custom command '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/autocomplete")
|
@router.get("/autocomplete")
|
||||||
@ -1028,7 +1018,6 @@ async def get_command_names_for_autocomplete(
|
|||||||
logger.error(f"Error getting command names for autocomplete: {e}")
|
logger.error(f"Error getting command names for autocomplete: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{command_id}")
|
@router.get("/{command_id}")
|
||||||
@ -1078,4 +1067,3 @@ async def get_custom_command(command_id: int):
|
|||||||
logger.error(f"Error getting custom command {command_id}: {e}")
|
logger.error(f"Error getting custom command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|||||||
@ -144,7 +144,6 @@ async def get_decisions(
|
|||||||
"count": all_dec.count(),
|
"count": all_dec.count(),
|
||||||
"decisions": [model_to_dict(x, recurse=not short_output) for x in all_dec],
|
"decisions": [model_to_dict(x, recurse=not short_output) for x in all_dec],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_dec
|
return return_dec
|
||||||
|
|
||||||
|
|
||||||
@ -169,7 +168,6 @@ async def patch_decision(
|
|||||||
|
|
||||||
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
||||||
if this_dec is None:
|
if this_dec is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Decision ID {decision_id} not found"
|
status_code=404, detail=f"Decision ID {decision_id} not found"
|
||||||
)
|
)
|
||||||
@ -195,10 +193,8 @@ async def patch_decision(
|
|||||||
|
|
||||||
if this_dec.save() == 1:
|
if this_dec.save() == 1:
|
||||||
d_result = model_to_dict(this_dec)
|
d_result = model_to_dict(this_dec)
|
||||||
db.close()
|
|
||||||
return d_result
|
return d_result
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch decision {decision_id}"
|
status_code=500, detail=f"Unable to patch decision {decision_id}"
|
||||||
)
|
)
|
||||||
@ -227,7 +223,6 @@ async def post_decisions(dec_list: DecisionList, token: str = Depends(oauth2_sch
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_dec, 10):
|
for batch in chunked(new_dec, 10):
|
||||||
Decision.insert_many(batch).on_conflict_ignore().execute()
|
Decision.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_dec)} decisions"
|
return f"Inserted {len(new_dec)} decisions"
|
||||||
|
|
||||||
@ -241,13 +236,11 @@ async def delete_decision(decision_id: int, token: str = Depends(oauth2_scheme))
|
|||||||
|
|
||||||
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
this_dec = Decision.get_or_none(Decision.id == decision_id)
|
||||||
if this_dec is None:
|
if this_dec is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Decision ID {decision_id} not found"
|
status_code=404, detail=f"Decision ID {decision_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_dec.delete_instance()
|
count = this_dec.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Decision {decision_id} has been deleted"
|
return f"Decision {decision_id} has been deleted"
|
||||||
@ -266,11 +259,9 @@ async def delete_decisions_game(game_id: int, token: str = Depends(oauth2_scheme
|
|||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
||||||
|
|
||||||
count = Decision.delete().where(Decision.game == this_game).execute()
|
count = Decision.delete().where(Decision.game == this_game).execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count > 0:
|
if count > 0:
|
||||||
return f"Deleted {count} decisions matching Game ID {game_id}"
|
return f"Deleted {count} decisions matching Game ID {game_id}"
|
||||||
|
|||||||
@ -48,7 +48,6 @@ async def get_divisions(
|
|||||||
"count": all_divisions.count(),
|
"count": all_divisions.count(),
|
||||||
"divisions": [model_to_dict(x) for x in all_divisions],
|
"divisions": [model_to_dict(x) for x in all_divisions],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_div
|
return return_div
|
||||||
|
|
||||||
|
|
||||||
@ -57,13 +56,11 @@ async def get_divisions(
|
|||||||
async def get_one_division(division_id: int):
|
async def get_one_division(division_id: int):
|
||||||
this_div = Division.get_or_none(Division.id == division_id)
|
this_div = Division.get_or_none(Division.id == division_id)
|
||||||
if this_div is None:
|
if this_div is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Division ID {division_id} not found"
|
status_code=404, detail=f"Division ID {division_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
r_div = model_to_dict(this_div)
|
r_div = model_to_dict(this_div)
|
||||||
db.close()
|
|
||||||
return r_div
|
return r_div
|
||||||
|
|
||||||
|
|
||||||
@ -83,7 +80,6 @@ async def patch_division(
|
|||||||
|
|
||||||
this_div = Division.get_or_none(Division.id == division_id)
|
this_div = Division.get_or_none(Division.id == division_id)
|
||||||
if this_div is None:
|
if this_div is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Division ID {division_id} not found"
|
status_code=404, detail=f"Division ID {division_id} not found"
|
||||||
)
|
)
|
||||||
@ -99,10 +95,8 @@ async def patch_division(
|
|||||||
|
|
||||||
if this_div.save() == 1:
|
if this_div.save() == 1:
|
||||||
r_division = model_to_dict(this_div)
|
r_division = model_to_dict(this_div)
|
||||||
db.close()
|
|
||||||
return r_division
|
return r_division
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch division {division_id}"
|
status_code=500, detail=f"Unable to patch division {division_id}"
|
||||||
)
|
)
|
||||||
@ -121,10 +115,8 @@ async def post_division(
|
|||||||
|
|
||||||
if this_division.save() == 1:
|
if this_division.save() == 1:
|
||||||
r_division = model_to_dict(this_division)
|
r_division = model_to_dict(this_division)
|
||||||
db.close()
|
|
||||||
return r_division
|
return r_division
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to post division")
|
raise HTTPException(status_code=500, detail=f"Unable to post division")
|
||||||
|
|
||||||
|
|
||||||
@ -137,13 +129,11 @@ async def delete_division(division_id: int, token: str = Depends(oauth2_scheme))
|
|||||||
|
|
||||||
this_div = Division.get_or_none(Division.id == division_id)
|
this_div = Division.get_or_none(Division.id == division_id)
|
||||||
if this_div is None:
|
if this_div is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Division ID {division_id} not found"
|
status_code=404, detail=f"Division ID {division_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_div.delete_instance()
|
count = this_div.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Division {division_id} has been deleted"
|
return f"Division {division_id} has been deleted"
|
||||||
|
|||||||
@ -32,7 +32,6 @@ async def get_draftdata():
|
|||||||
|
|
||||||
if draft_data is not None:
|
if draft_data is not None:
|
||||||
r_data = model_to_dict(draft_data)
|
r_data = model_to_dict(draft_data)
|
||||||
db.close()
|
|
||||||
return r_data
|
return r_data
|
||||||
|
|
||||||
raise HTTPException(status_code=404, detail=f'No draft data found')
|
raise HTTPException(status_code=404, detail=f'No draft data found')
|
||||||
@ -50,7 +49,6 @@ async def patch_draftdata(
|
|||||||
|
|
||||||
draft_data = DraftData.get_or_none(DraftData.id == data_id)
|
draft_data = DraftData.get_or_none(DraftData.id == data_id)
|
||||||
if draft_data is None:
|
if draft_data is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f'No draft data found')
|
raise HTTPException(status_code=404, detail=f'No draft data found')
|
||||||
|
|
||||||
if currentpick is not None:
|
if currentpick is not None:
|
||||||
@ -68,7 +66,6 @@ async def patch_draftdata(
|
|||||||
|
|
||||||
saved = draft_data.save()
|
saved = draft_data.save()
|
||||||
r_data = model_to_dict(draft_data)
|
r_data = model_to_dict(draft_data)
|
||||||
db.close()
|
|
||||||
|
|
||||||
if saved == 1:
|
if saved == 1:
|
||||||
return r_data
|
return r_data
|
||||||
|
|||||||
@ -48,7 +48,6 @@ async def get_draftlist(
|
|||||||
|
|
||||||
r_list = {"count": all_list.count(), "picks": [model_to_dict(x) for x in all_list]}
|
r_list = {"count": all_list.count(), "picks": [model_to_dict(x) for x in all_list]}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return r_list
|
return r_list
|
||||||
|
|
||||||
|
|
||||||
@ -69,7 +68,6 @@ async def get_team_draftlist(team_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
"picks": [model_to_dict(x) for x in this_list],
|
"picks": [model_to_dict(x) for x in this_list],
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return r_list
|
return r_list
|
||||||
|
|
||||||
|
|
||||||
@ -99,7 +97,6 @@ async def post_draftlist(
|
|||||||
for batch in chunked(new_list, 15):
|
for batch in chunked(new_list, 15):
|
||||||
DraftList.insert_many(batch).on_conflict_ignore().execute()
|
DraftList.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
|
||||||
db.close()
|
|
||||||
return f"Inserted {len(new_list)} list values"
|
return f"Inserted {len(new_list)} list values"
|
||||||
|
|
||||||
|
|
||||||
@ -111,5 +108,4 @@ async def delete_draftlist(team_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
count = DraftList.delete().where(DraftList.team_id == team_id).execute()
|
count = DraftList.delete().where(DraftList.team_id == team_id).execute()
|
||||||
db.close()
|
|
||||||
return f"Deleted {count} list values"
|
return f"Deleted {count} list values"
|
||||||
|
|||||||
@ -123,7 +123,6 @@ async def get_picks(
|
|||||||
for line in all_picks:
|
for line in all_picks:
|
||||||
return_picks["picks"].append(model_to_dict(line, recurse=not short_output))
|
return_picks["picks"].append(model_to_dict(line, recurse=not short_output))
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_picks
|
return return_picks
|
||||||
|
|
||||||
|
|
||||||
@ -135,7 +134,6 @@ async def get_one_pick(pick_id: int, short_output: Optional[bool] = False):
|
|||||||
r_pick = model_to_dict(this_pick, recurse=not short_output)
|
r_pick = model_to_dict(this_pick, recurse=not short_output)
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
||||||
db.close()
|
|
||||||
return r_pick
|
return r_pick
|
||||||
|
|
||||||
|
|
||||||
@ -153,7 +151,6 @@ async def patch_pick(
|
|||||||
|
|
||||||
DraftPick.update(**new_pick.dict()).where(DraftPick.id == pick_id).execute()
|
DraftPick.update(**new_pick.dict()).where(DraftPick.id == pick_id).execute()
|
||||||
r_pick = model_to_dict(DraftPick.get_by_id(pick_id))
|
r_pick = model_to_dict(DraftPick.get_by_id(pick_id))
|
||||||
db.close()
|
|
||||||
return r_pick
|
return r_pick
|
||||||
|
|
||||||
|
|
||||||
@ -170,7 +167,6 @@ async def post_picks(p_list: DraftPickList, token: str = Depends(oauth2_scheme))
|
|||||||
DraftPick.season == pick.season, DraftPick.overall == pick.overall
|
DraftPick.season == pick.season, DraftPick.overall == pick.overall
|
||||||
)
|
)
|
||||||
if dupe:
|
if dupe:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
detail=f"Pick # {pick.overall} already exists for season {pick.season}",
|
detail=f"Pick # {pick.overall} already exists for season {pick.season}",
|
||||||
@ -181,7 +177,6 @@ async def post_picks(p_list: DraftPickList, token: str = Depends(oauth2_scheme))
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_picks, 15):
|
for batch in chunked(new_picks, 15):
|
||||||
DraftPick.insert_many(batch).on_conflict_ignore().execute()
|
DraftPick.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_picks)} picks"
|
return f"Inserted {len(new_picks)} picks"
|
||||||
|
|
||||||
@ -198,7 +193,6 @@ async def delete_pick(pick_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
raise HTTPException(status_code=404, detail=f"Pick ID {pick_id} not found")
|
||||||
|
|
||||||
count = this_pick.delete_instance()
|
count = this_pick.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Draft pick {pick_id} has been deleted"
|
return f"Draft pick {pick_id} has been deleted"
|
||||||
|
|||||||
@ -25,17 +25,14 @@ async def get_fieldingstats(
|
|||||||
if 'post' in s_type.lower():
|
if 'post' in s_type.lower():
|
||||||
all_stats = BattingStat.post_season(season)
|
all_stats = BattingStat.post_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {'count': 0, 'stats': []}
|
return {'count': 0, 'stats': []}
|
||||||
elif s_type.lower() in ['combined', 'total', 'all']:
|
elif s_type.lower() in ['combined', 'total', 'all']:
|
||||||
all_stats = BattingStat.combined_season(season)
|
all_stats = BattingStat.combined_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {'count': 0, 'stats': []}
|
return {'count': 0, 'stats': []}
|
||||||
else:
|
else:
|
||||||
all_stats = BattingStat.regular_season(season)
|
all_stats = BattingStat.regular_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {'count': 0, 'stats': []}
|
return {'count': 0, 'stats': []}
|
||||||
|
|
||||||
all_stats = all_stats.where(
|
all_stats = all_stats.where(
|
||||||
@ -63,7 +60,6 @@ async def get_fieldingstats(
|
|||||||
if week_end is not None:
|
if week_end is not None:
|
||||||
end = min(week_end, end)
|
end = min(week_end, end)
|
||||||
if start > end:
|
if start > end:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404,
|
status_code=404,
|
||||||
detail=f'Start week {start} is after end week {end} - cannot pull stats'
|
detail=f'Start week {start} is after end week {end} - cannot pull stats'
|
||||||
@ -96,7 +92,6 @@ async def get_fieldingstats(
|
|||||||
} for x in all_stats]
|
} for x in all_stats]
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -219,5 +214,4 @@ async def get_totalstats(
|
|||||||
})
|
})
|
||||||
|
|
||||||
return_stats['count'] = len(return_stats['stats'])
|
return_stats['count'] = len(return_stats['stats'])
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
@ -139,7 +139,6 @@ async def get_help_commands(
|
|||||||
logger.error(f"Error getting help commands: {e}")
|
logger.error(f"Error getting help commands: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.post("/", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -188,7 +187,6 @@ async def create_help_command_endpoint(
|
|||||||
logger.error(f"Error creating help command: {e}")
|
logger.error(f"Error creating help command: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.put("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -239,7 +237,6 @@ async def update_help_command_endpoint(
|
|||||||
logger.error(f"Error updating help command {command_id}: {e}")
|
logger.error(f"Error updating help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{command_id}/restore", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/{command_id}/restore", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -278,7 +275,6 @@ async def restore_help_command_endpoint(
|
|||||||
logger.error(f"Error restoring help command {command_id}: {e}")
|
logger.error(f"Error restoring help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.delete("/{command_id}", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -310,7 +306,6 @@ async def delete_help_command_endpoint(
|
|||||||
logger.error(f"Error deleting help command {command_id}: {e}")
|
logger.error(f"Error deleting help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats")
|
@router.get("/stats")
|
||||||
@ -369,7 +364,6 @@ async def get_help_command_stats():
|
|||||||
logger.error(f"Error getting help command stats: {e}")
|
logger.error(f"Error getting help command stats: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
# Special endpoints for Discord bot integration
|
# Special endpoints for Discord bot integration
|
||||||
@ -403,7 +397,6 @@ async def get_help_command_by_name_endpoint(
|
|||||||
logger.error(f"Error getting help command by name '{command_name}': {e}")
|
logger.error(f"Error getting help command by name '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/by_name/{command_name}/view", include_in_schema=PRIVATE_IN_SCHEMA)
|
@router.patch("/by_name/{command_name}/view", include_in_schema=PRIVATE_IN_SCHEMA)
|
||||||
@ -440,7 +433,6 @@ async def increment_view_count(command_name: str, token: str = Depends(oauth2_sc
|
|||||||
logger.error(f"Error incrementing view count for '{command_name}': {e}")
|
logger.error(f"Error incrementing view count for '{command_name}': {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/autocomplete")
|
@router.get("/autocomplete")
|
||||||
@ -471,7 +463,6 @@ async def get_help_names_for_autocomplete(
|
|||||||
logger.error(f"Error getting help names for autocomplete: {e}")
|
logger.error(f"Error getting help names for autocomplete: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{command_id}")
|
@router.get("/{command_id}")
|
||||||
@ -500,4 +491,3 @@ async def get_help_command(command_id: int):
|
|||||||
logger.error(f"Error getting help command {command_id}: {e}")
|
logger.error(f"Error getting help command {command_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
|
||||||
|
|||||||
@ -68,7 +68,6 @@ async def get_injuries(
|
|||||||
"count": all_injuries.count(),
|
"count": all_injuries.count(),
|
||||||
"injuries": [model_to_dict(x, recurse=not short_output) for x in all_injuries],
|
"injuries": [model_to_dict(x, recurse=not short_output) for x in all_injuries],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_injuries
|
return return_injuries
|
||||||
|
|
||||||
|
|
||||||
@ -85,7 +84,6 @@ async def patch_injury(
|
|||||||
|
|
||||||
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
||||||
if this_injury is None:
|
if this_injury is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
||||||
|
|
||||||
if is_active is not None:
|
if is_active is not None:
|
||||||
@ -93,10 +91,8 @@ async def patch_injury(
|
|||||||
|
|
||||||
if this_injury.save() == 1:
|
if this_injury.save() == 1:
|
||||||
r_injury = model_to_dict(this_injury)
|
r_injury = model_to_dict(this_injury)
|
||||||
db.close()
|
|
||||||
return r_injury
|
return r_injury
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch injury {injury_id}"
|
status_code=500, detail=f"Unable to patch injury {injury_id}"
|
||||||
)
|
)
|
||||||
@ -113,10 +109,8 @@ async def post_injury(new_injury: InjuryModel, token: str = Depends(oauth2_schem
|
|||||||
|
|
||||||
if this_injury.save():
|
if this_injury.save():
|
||||||
r_injury = model_to_dict(this_injury)
|
r_injury = model_to_dict(this_injury)
|
||||||
db.close()
|
|
||||||
return r_injury
|
return r_injury
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to post injury")
|
raise HTTPException(status_code=500, detail=f"Unable to post injury")
|
||||||
|
|
||||||
|
|
||||||
@ -129,11 +123,9 @@ async def delete_injury(injury_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
this_injury = Injury.get_or_none(Injury.id == injury_id)
|
||||||
if this_injury is None:
|
if this_injury is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
raise HTTPException(status_code=404, detail=f"Injury ID {injury_id} not found")
|
||||||
|
|
||||||
count = this_injury.delete_instance()
|
count = this_injury.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Injury {injury_id} has been deleted"
|
return f"Injury {injury_id} has been deleted"
|
||||||
|
|||||||
@ -48,7 +48,6 @@ async def get_keepers(
|
|||||||
"count": all_keepers.count(),
|
"count": all_keepers.count(),
|
||||||
"keepers": [model_to_dict(x, recurse=not short_output) for x in all_keepers],
|
"keepers": [model_to_dict(x, recurse=not short_output) for x in all_keepers],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_keepers
|
return return_keepers
|
||||||
|
|
||||||
|
|
||||||
@ -78,10 +77,8 @@ async def patch_keeper(
|
|||||||
|
|
||||||
if this_keeper.save():
|
if this_keeper.save():
|
||||||
r_keeper = model_to_dict(this_keeper)
|
r_keeper = model_to_dict(this_keeper)
|
||||||
db.close()
|
|
||||||
return r_keeper
|
return r_keeper
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch keeper {keeper_id}"
|
status_code=500, detail=f"Unable to patch keeper {keeper_id}"
|
||||||
)
|
)
|
||||||
@ -101,7 +98,6 @@ async def post_keepers(k_list: KeeperList, token: str = Depends(oauth2_scheme)):
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_keepers, 14):
|
for batch in chunked(new_keepers, 14):
|
||||||
Keeper.insert_many(batch).on_conflict_ignore().execute()
|
Keeper.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_keepers)} keepers"
|
return f"Inserted {len(new_keepers)} keepers"
|
||||||
|
|
||||||
@ -118,7 +114,6 @@ async def delete_keeper(keeper_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
raise HTTPException(status_code=404, detail=f"Keeper ID {keeper_id} not found")
|
raise HTTPException(status_code=404, detail=f"Keeper ID {keeper_id} not found")
|
||||||
|
|
||||||
count = this_keeper.delete_instance()
|
count = this_keeper.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Keeper ID {keeper_id} has been deleted"
|
return f"Keeper ID {keeper_id} has been deleted"
|
||||||
|
|||||||
@ -76,7 +76,6 @@ async def get_managers(
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_managers
|
return return_managers
|
||||||
|
|
||||||
|
|
||||||
@ -86,7 +85,6 @@ async def get_one_manager(manager_id: int, short_output: Optional[bool] = False)
|
|||||||
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
||||||
if this_manager is not None:
|
if this_manager is not None:
|
||||||
r_manager = model_to_dict(this_manager, recurse=not short_output)
|
r_manager = model_to_dict(this_manager, recurse=not short_output)
|
||||||
db.close()
|
|
||||||
return r_manager
|
return r_manager
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=404, detail=f"Manager {manager_id} not found")
|
raise HTTPException(status_code=404, detail=f"Manager {manager_id} not found")
|
||||||
@ -108,7 +106,6 @@ async def patch_manager(
|
|||||||
|
|
||||||
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
||||||
if this_manager is None:
|
if this_manager is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Manager ID {manager_id} not found"
|
status_code=404, detail=f"Manager ID {manager_id} not found"
|
||||||
)
|
)
|
||||||
@ -124,10 +121,8 @@ async def patch_manager(
|
|||||||
|
|
||||||
if this_manager.save() == 1:
|
if this_manager.save() == 1:
|
||||||
r_manager = model_to_dict(this_manager)
|
r_manager = model_to_dict(this_manager)
|
||||||
db.close()
|
|
||||||
return r_manager
|
return r_manager
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch manager {this_manager}"
|
status_code=500, detail=f"Unable to patch manager {this_manager}"
|
||||||
)
|
)
|
||||||
@ -144,10 +139,8 @@ async def post_manager(new_manager: ManagerModel, token: str = Depends(oauth2_sc
|
|||||||
|
|
||||||
if this_manager.save():
|
if this_manager.save():
|
||||||
r_manager = model_to_dict(this_manager)
|
r_manager = model_to_dict(this_manager)
|
||||||
db.close()
|
|
||||||
return r_manager
|
return r_manager
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to post manager {this_manager.name}"
|
status_code=500, detail=f"Unable to post manager {this_manager.name}"
|
||||||
)
|
)
|
||||||
@ -162,13 +155,11 @@ async def delete_manager(manager_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
this_manager = Manager.get_or_none(Manager.id == manager_id)
|
||||||
if this_manager is None:
|
if this_manager is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"Manager ID {manager_id} not found"
|
status_code=404, detail=f"Manager ID {manager_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_manager.delete_instance()
|
count = this_manager.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Manager {manager_id} has been deleted"
|
return f"Manager {manager_id} has been deleted"
|
||||||
|
|||||||
@ -76,17 +76,14 @@ async def get_pitstats(
|
|||||||
if "post" in s_type.lower():
|
if "post" in s_type.lower():
|
||||||
all_stats = PitchingStat.post_season(season)
|
all_stats = PitchingStat.post_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
elif s_type.lower() in ["combined", "total", "all"]:
|
elif s_type.lower() in ["combined", "total", "all"]:
|
||||||
all_stats = PitchingStat.combined_season(season)
|
all_stats = PitchingStat.combined_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
else:
|
else:
|
||||||
all_stats = PitchingStat.regular_season(season)
|
all_stats = PitchingStat.regular_season(season)
|
||||||
if all_stats.count() == 0:
|
if all_stats.count() == 0:
|
||||||
db.close()
|
|
||||||
return {"count": 0, "stats": []}
|
return {"count": 0, "stats": []}
|
||||||
|
|
||||||
if team_abbrev is not None:
|
if team_abbrev is not None:
|
||||||
@ -112,7 +109,6 @@ async def get_pitstats(
|
|||||||
if week_end is not None:
|
if week_end is not None:
|
||||||
end = min(week_end, end)
|
end = min(week_end, end)
|
||||||
if start > end:
|
if start > end:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404,
|
status_code=404,
|
||||||
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
detail=f"Start week {start} is after end week {end} - cannot pull stats",
|
||||||
@ -132,7 +128,6 @@ async def get_pitstats(
|
|||||||
"stats": [model_to_dict(x, recurse=not short_output) for x in all_stats],
|
"stats": [model_to_dict(x, recurse=not short_output) for x in all_stats],
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -301,7 +296,6 @@ async def get_totalstats(
|
|||||||
"bsv": x.sum_bsv,
|
"bsv": x.sum_bsv,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|
||||||
|
|
||||||
@ -319,7 +313,6 @@ async def patch_pitstats(
|
|||||||
|
|
||||||
PitchingStat.update(**new_stats.dict()).where(PitchingStat.id == stat_id).execute()
|
PitchingStat.update(**new_stats.dict()).where(PitchingStat.id == stat_id).execute()
|
||||||
r_stat = model_to_dict(PitchingStat.get_by_id(stat_id))
|
r_stat = model_to_dict(PitchingStat.get_by_id(stat_id))
|
||||||
db.close()
|
|
||||||
return r_stat
|
return r_stat
|
||||||
|
|
||||||
|
|
||||||
@ -350,5 +343,4 @@ async def post_pitstats(s_list: PitStatList, token: str = Depends(oauth2_scheme)
|
|||||||
for batch in chunked(all_stats, 15):
|
for batch in chunked(all_stats, 15):
|
||||||
PitchingStat.insert_many(batch).on_conflict_ignore().execute()
|
PitchingStat.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
|
||||||
db.close()
|
|
||||||
return f"Added {len(all_stats)} batting lines"
|
return f"Added {len(all_stats)} batting lines"
|
||||||
|
|||||||
@ -78,7 +78,6 @@ async def get_results(
|
|||||||
"count": all_results.count(),
|
"count": all_results.count(),
|
||||||
"results": [model_to_dict(x, recurse=not short_output) for x in all_results],
|
"results": [model_to_dict(x, recurse=not short_output) for x in all_results],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_results
|
return return_results
|
||||||
|
|
||||||
|
|
||||||
@ -90,7 +89,6 @@ async def get_one_result(result_id: int, short_output: Optional[bool] = False):
|
|||||||
r_result = model_to_dict(this_result, recurse=not short_output)
|
r_result = model_to_dict(this_result, recurse=not short_output)
|
||||||
else:
|
else:
|
||||||
r_result = None
|
r_result = None
|
||||||
db.close()
|
|
||||||
return r_result
|
return r_result
|
||||||
|
|
||||||
|
|
||||||
@ -142,10 +140,8 @@ async def patch_result(
|
|||||||
|
|
||||||
if this_result.save() == 1:
|
if this_result.save() == 1:
|
||||||
r_result = model_to_dict(this_result)
|
r_result = model_to_dict(this_result)
|
||||||
db.close()
|
|
||||||
return r_result
|
return r_result
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch result {result_id}"
|
status_code=500, detail=f"Unable to patch result {result_id}"
|
||||||
)
|
)
|
||||||
@ -185,7 +181,6 @@ async def post_results(result_list: ResultList, token: str = Depends(oauth2_sche
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_results, 15):
|
for batch in chunked(new_results, 15):
|
||||||
Result.insert_many(batch).on_conflict_ignore().execute()
|
Result.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_results)} results"
|
return f"Inserted {len(new_results)} results"
|
||||||
|
|
||||||
@ -199,11 +194,9 @@ async def delete_result(result_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_result = Result.get_or_none(Result.id == result_id)
|
this_result = Result.get_or_none(Result.id == result_id)
|
||||||
if not this_result:
|
if not this_result:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Result ID {result_id} not found")
|
raise HTTPException(status_code=404, detail=f"Result ID {result_id} not found")
|
||||||
|
|
||||||
count = this_result.delete_instance()
|
count = this_result.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Result {result_id} has been deleted"
|
return f"Result {result_id} has been deleted"
|
||||||
|
|||||||
@ -98,14 +98,12 @@ async def get_players(
|
|||||||
|
|
||||||
if csv:
|
if csv:
|
||||||
return_val = query_to_csv(all_players)
|
return_val = query_to_csv(all_players)
|
||||||
db.close()
|
|
||||||
return Response(content=return_val, media_type="text/csv")
|
return Response(content=return_val, media_type="text/csv")
|
||||||
|
|
||||||
return_val = {
|
return_val = {
|
||||||
"count": all_players.count(),
|
"count": all_players.count(),
|
||||||
"players": [model_to_dict(x) for x in all_players],
|
"players": [model_to_dict(x) for x in all_players],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_val
|
return return_val
|
||||||
|
|
||||||
|
|
||||||
@ -114,13 +112,11 @@ async def get_players(
|
|||||||
async def get_one_player(player_id: int):
|
async def get_one_player(player_id: int):
|
||||||
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
||||||
if this_player is None:
|
if this_player is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
r_data = model_to_dict(this_player)
|
r_data = model_to_dict(this_player)
|
||||||
db.close()
|
|
||||||
return r_data
|
return r_data
|
||||||
|
|
||||||
|
|
||||||
@ -138,7 +134,6 @@ async def patch_player(
|
|||||||
):
|
):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logging.warning(f"Bad Token: {token}")
|
logging.warning(f"Bad Token: {token}")
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to patch mlb players. This event has been logged.",
|
detail="You are not authorized to patch mlb players. This event has been logged.",
|
||||||
@ -146,7 +141,6 @@ async def patch_player(
|
|||||||
|
|
||||||
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
||||||
if this_player is None:
|
if this_player is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
||||||
)
|
)
|
||||||
@ -166,10 +160,8 @@ async def patch_player(
|
|||||||
|
|
||||||
if this_player.save() == 1:
|
if this_player.save() == 1:
|
||||||
return_val = model_to_dict(this_player)
|
return_val = model_to_dict(this_player)
|
||||||
db.close()
|
|
||||||
return return_val
|
return return_val
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=418,
|
status_code=418,
|
||||||
detail="Well slap my ass and call me a teapot; I could not save that player",
|
detail="Well slap my ass and call me a teapot; I could not save that player",
|
||||||
@ -181,7 +173,6 @@ async def patch_player(
|
|||||||
async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme)):
|
async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logging.warning(f"Bad Token: {token}")
|
logging.warning(f"Bad Token: {token}")
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to post mlb players. This event has been logged.",
|
detail="You are not authorized to post mlb players. This event has been logged.",
|
||||||
@ -200,7 +191,6 @@ async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme))
|
|||||||
)
|
)
|
||||||
if dupes.count() > 0:
|
if dupes.count() > 0:
|
||||||
logger.error(f"Found a dupe for {x}")
|
logger.error(f"Found a dupe for {x}")
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=f"{x.first_name} {x.last_name} has a key already in the database",
|
detail=f"{x.first_name} {x.last_name} has a key already in the database",
|
||||||
@ -211,7 +201,6 @@ async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme))
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_players, 15):
|
for batch in chunked(new_players, 15):
|
||||||
SbaPlayer.insert_many(batch).on_conflict_ignore().execute()
|
SbaPlayer.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_players)} new MLB players"
|
return f"Inserted {len(new_players)} new MLB players"
|
||||||
|
|
||||||
@ -221,7 +210,6 @@ async def post_players(players: PlayerList, token: str = Depends(oauth2_scheme))
|
|||||||
async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_scheme)):
|
async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_scheme)):
|
||||||
if not valid_token(token):
|
if not valid_token(token):
|
||||||
logging.warning(f"Bad Token: {token}")
|
logging.warning(f"Bad Token: {token}")
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to post mlb players. This event has been logged.",
|
detail="You are not authorized to post mlb players. This event has been logged.",
|
||||||
@ -236,7 +224,6 @@ async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_sc
|
|||||||
logging.info(f"POST /SbaPlayers/one - dupes found:")
|
logging.info(f"POST /SbaPlayers/one - dupes found:")
|
||||||
for x in dupes:
|
for x in dupes:
|
||||||
logging.info(f"{x}")
|
logging.info(f"{x}")
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=f"{player.first_name} {player.last_name} has a key already in the database",
|
detail=f"{player.first_name} {player.last_name} has a key already in the database",
|
||||||
@ -246,10 +233,8 @@ async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_sc
|
|||||||
saved = new_player.save()
|
saved = new_player.save()
|
||||||
if saved == 1:
|
if saved == 1:
|
||||||
return_val = model_to_dict(new_player)
|
return_val = model_to_dict(new_player)
|
||||||
db.close()
|
|
||||||
return return_val
|
return return_val
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=418,
|
status_code=418,
|
||||||
detail="Well slap my ass and call me a teapot; I could not save that player",
|
detail="Well slap my ass and call me a teapot; I could not save that player",
|
||||||
@ -261,7 +246,6 @@ async def post_one_player(player: SbaPlayerModel, token: str = Depends(oauth2_sc
|
|||||||
async 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):
|
if not valid_token(token):
|
||||||
logging.warning(f"Bad Token: {token}")
|
logging.warning(f"Bad Token: {token}")
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=401,
|
status_code=401,
|
||||||
detail="You are not authorized to delete mlb players. This event has been logged.",
|
detail="You are not authorized to delete mlb players. This event has been logged.",
|
||||||
@ -269,13 +253,11 @@ async def delete_player(player_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
this_player = SbaPlayer.get_or_none(SbaPlayer.id == player_id)
|
||||||
if this_player is None:
|
if this_player is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
status_code=404, detail=f"SbaPlayer id {player_id} not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
count = this_player.delete_instance()
|
count = this_player.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Player {player_id} has been deleted"
|
return f"Player {player_id} has been deleted"
|
||||||
|
|||||||
@ -73,7 +73,6 @@ async def get_schedules(
|
|||||||
"count": all_sched.count(),
|
"count": all_sched.count(),
|
||||||
"schedules": [model_to_dict(x, recurse=not short_output) for x in all_sched],
|
"schedules": [model_to_dict(x, recurse=not short_output) for x in all_sched],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_sched
|
return return_sched
|
||||||
|
|
||||||
|
|
||||||
@ -85,7 +84,6 @@ async def get_one_schedule(schedule_id: int):
|
|||||||
r_sched = model_to_dict(this_sched)
|
r_sched = model_to_dict(this_sched)
|
||||||
else:
|
else:
|
||||||
r_sched = None
|
r_sched = None
|
||||||
db.close()
|
|
||||||
return r_sched
|
return r_sched
|
||||||
|
|
||||||
|
|
||||||
@ -127,10 +125,8 @@ async def patch_schedule(
|
|||||||
|
|
||||||
if this_sched.save() == 1:
|
if this_sched.save() == 1:
|
||||||
r_sched = model_to_dict(this_sched)
|
r_sched = model_to_dict(this_sched)
|
||||||
db.close()
|
|
||||||
return r_sched
|
return r_sched
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=500, detail=f"Unable to patch schedule {schedule_id}"
|
status_code=500, detail=f"Unable to patch schedule {schedule_id}"
|
||||||
)
|
)
|
||||||
@ -170,7 +166,6 @@ async def post_schedules(sched_list: ScheduleList, token: str = Depends(oauth2_s
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_sched, 15):
|
for batch in chunked(new_sched, 15):
|
||||||
Schedule.insert_many(batch).on_conflict_ignore().execute()
|
Schedule.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_sched)} schedules"
|
return f"Inserted {len(new_sched)} schedules"
|
||||||
|
|
||||||
@ -189,7 +184,6 @@ async def delete_schedule(schedule_id: int, token: str = Depends(oauth2_scheme))
|
|||||||
)
|
)
|
||||||
|
|
||||||
count = this_sched.delete_instance()
|
count = this_sched.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Schedule {this_sched} has been deleted"
|
return f"Schedule {this_sched} has been deleted"
|
||||||
|
|||||||
@ -62,7 +62,6 @@ async def get_standings(
|
|||||||
"standings": [model_to_dict(x, recurse=not short_output) for x in div_teams],
|
"standings": [model_to_dict(x, recurse=not short_output) for x in div_teams],
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_standings
|
return return_standings
|
||||||
|
|
||||||
|
|
||||||
@ -93,7 +92,6 @@ async def patch_standings(
|
|||||||
try:
|
try:
|
||||||
this_stan = Standings.get_by_id(stan_id)
|
this_stan = Standings.get_by_id(stan_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"No team found with id {stan_id}")
|
raise HTTPException(status_code=404, detail=f"No team found with id {stan_id}")
|
||||||
|
|
||||||
if wins:
|
if wins:
|
||||||
@ -102,7 +100,6 @@ async def patch_standings(
|
|||||||
this_stan.losses = losses
|
this_stan.losses = losses
|
||||||
|
|
||||||
this_stan.save()
|
this_stan.save()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return model_to_dict(this_stan)
|
return model_to_dict(this_stan)
|
||||||
|
|
||||||
@ -122,7 +119,6 @@ async def post_standings(season: int, token: str = Depends(oauth2_scheme)):
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_teams, 16):
|
for batch in chunked(new_teams, 16):
|
||||||
Standings.insert_many(batch).on_conflict_ignore().execute()
|
Standings.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_teams)} standings"
|
return f"Inserted {len(new_teams)} standings"
|
||||||
|
|
||||||
@ -135,7 +131,6 @@ async def recalculate_standings(season: int, token: str = Depends(oauth2_scheme)
|
|||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
code = Standings.recalculate(season)
|
code = Standings.recalculate(season)
|
||||||
db.close()
|
|
||||||
if code == 69:
|
if code == 69:
|
||||||
raise HTTPException(status_code=500, detail=f"Error recreating Standings rows")
|
raise HTTPException(status_code=500, detail=f"Error recreating Standings rows")
|
||||||
return f"Just recalculated standings for season {season}"
|
return f"Just recalculated standings for season {season}"
|
||||||
|
|||||||
@ -123,7 +123,6 @@ async def get_games(
|
|||||||
"count": all_games.count(),
|
"count": all_games.count(),
|
||||||
"games": [model_to_dict(x, recurse=not short_output) for x in all_games],
|
"games": [model_to_dict(x, recurse=not short_output) for x in all_games],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_games
|
return return_games
|
||||||
|
|
||||||
|
|
||||||
@ -132,11 +131,9 @@ async def get_games(
|
|||||||
async def get_one_game(game_id: int) -> Any:
|
async def get_one_game(game_id: int) -> Any:
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
g_result = model_to_dict(this_game)
|
g_result = model_to_dict(this_game)
|
||||||
db.close()
|
|
||||||
return g_result
|
return g_result
|
||||||
|
|
||||||
|
|
||||||
@ -158,7 +155,6 @@ async def patch_game(
|
|||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
if game_num is not None:
|
if game_num is not None:
|
||||||
@ -253,7 +249,6 @@ async def post_games(game_list: GameList, token: str = Depends(oauth2_scheme)) -
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_games, 16):
|
for batch in chunked(new_games, 16):
|
||||||
StratGame.insert_many(batch).on_conflict_ignore().execute()
|
StratGame.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_games)} games"
|
return f"Inserted {len(new_games)} games"
|
||||||
|
|
||||||
@ -267,7 +262,6 @@ async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
|||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
this_game.away_score = None
|
this_game.away_score = None
|
||||||
@ -278,10 +272,8 @@ async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
|||||||
|
|
||||||
if this_game.save() == 1:
|
if this_game.save() == 1:
|
||||||
g_result = model_to_dict(this_game)
|
g_result = model_to_dict(this_game)
|
||||||
db.close()
|
|
||||||
return g_result
|
return g_result
|
||||||
else:
|
else:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=500, detail=f"Unable to wipe game {game_id}")
|
raise HTTPException(status_code=500, detail=f"Unable to wipe game {game_id}")
|
||||||
|
|
||||||
|
|
||||||
@ -294,11 +286,9 @@ async def delete_game(game_id: int, token: str = Depends(oauth2_scheme)) -> Any:
|
|||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
|
||||||
|
|
||||||
count = this_game.delete_instance()
|
count = this_game.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"StratGame {game_id} has been deleted"
|
return f"StratGame {game_id} has been deleted"
|
||||||
|
|||||||
@ -594,5 +594,4 @@ async def get_batting_totals(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
@ -20,10 +20,8 @@ logger = logging.getLogger("discord_app")
|
|||||||
@handle_db_errors
|
@handle_db_errors
|
||||||
async def get_one_play(play_id: int):
|
async def get_one_play(play_id: int):
|
||||||
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
||||||
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
||||||
db.close()
|
|
||||||
return r_play
|
return r_play
|
||||||
|
|
||||||
|
|
||||||
@ -37,12 +35,10 @@ async def patch_play(
|
|||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
if StratPlay.get_or_none(StratPlay.id == play_id) is None:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
||||||
|
|
||||||
StratPlay.update(**new_play.dict()).where(StratPlay.id == play_id).execute()
|
StratPlay.update(**new_play.dict()).where(StratPlay.id == play_id).execute()
|
||||||
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
r_play = model_to_dict(StratPlay.get_by_id(play_id))
|
||||||
db.close()
|
|
||||||
return r_play
|
return r_play
|
||||||
|
|
||||||
|
|
||||||
@ -93,7 +89,6 @@ async def post_plays(p_list: PlayList, token: str = Depends(oauth2_scheme)):
|
|||||||
with db.atomic():
|
with db.atomic():
|
||||||
for batch in chunked(new_plays, 20):
|
for batch in chunked(new_plays, 20):
|
||||||
StratPlay.insert_many(batch).on_conflict_ignore().execute()
|
StratPlay.insert_many(batch).on_conflict_ignore().execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
return f"Inserted {len(new_plays)} plays"
|
return f"Inserted {len(new_plays)} plays"
|
||||||
|
|
||||||
@ -107,11 +102,9 @@ async def delete_play(play_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_play = StratPlay.get_or_none(StratPlay.id == play_id)
|
this_play = StratPlay.get_or_none(StratPlay.id == play_id)
|
||||||
if not this_play:
|
if not this_play:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
raise HTTPException(status_code=404, detail=f"Play ID {play_id} not found")
|
||||||
|
|
||||||
count = this_play.delete_instance()
|
count = this_play.delete_instance()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count == 1:
|
if count == 1:
|
||||||
return f"Play {play_id} has been deleted"
|
return f"Play {play_id} has been deleted"
|
||||||
@ -130,11 +123,9 @@ async def delete_plays_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
|||||||
|
|
||||||
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
||||||
if not this_game:
|
if not this_game:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
raise HTTPException(status_code=404, detail=f"Game ID {game_id} not found")
|
||||||
|
|
||||||
count = StratPlay.delete().where(StratPlay.game == this_game).execute()
|
count = StratPlay.delete().where(StratPlay.game == this_game).execute()
|
||||||
db.close()
|
|
||||||
|
|
||||||
if count > 0:
|
if count > 0:
|
||||||
return f"Deleted {count} plays matching Game ID {game_id}"
|
return f"Deleted {count} plays matching Game ID {game_id}"
|
||||||
@ -155,5 +146,4 @@ async def post_erun_check(token: str = Depends(oauth2_scheme)):
|
|||||||
(StratPlay.e_run == 1) & (StratPlay.run == 0)
|
(StratPlay.e_run == 1) & (StratPlay.run == 0)
|
||||||
)
|
)
|
||||||
count = all_plays.execute()
|
count = all_plays.execute()
|
||||||
db.close()
|
|
||||||
return count
|
return count
|
||||||
|
|||||||
@ -361,5 +361,4 @@ async def get_fielding_totals(
|
|||||||
"week": this_week,
|
"week": this_week,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
db.close()
|
|
||||||
return return_stats
|
return return_stats
|
||||||
|
|||||||
@ -348,7 +348,6 @@ async def get_pitching_totals(
|
|||||||
)
|
)
|
||||||
|
|
||||||
return_stats["count"] = len(return_stats["stats"])
|
return_stats["count"] = len(return_stats["stats"])
|
||||||
db.close()
|
|
||||||
if csv:
|
if csv:
|
||||||
return Response(
|
return Response(
|
||||||
content=complex_data_to_csv(return_stats["stats"]), media_type="text/csv"
|
content=complex_data_to_csv(return_stats["stats"]), media_type="text/csv"
|
||||||
|
|||||||
@ -210,5 +210,4 @@ async def get_plays(
|
|||||||
"count": all_plays.count(),
|
"count": all_plays.count(),
|
||||||
"plays": [model_to_dict(x, recurse=not short_output) for x in all_plays],
|
"plays": [model_to_dict(x, recurse=not short_output) for x in all_plays],
|
||||||
}
|
}
|
||||||
db.close()
|
|
||||||
return return_plays
|
return return_plays
|
||||||
|
|||||||
@ -98,7 +98,6 @@ async def get_transactions(
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
db.close()
|
|
||||||
return return_trans
|
return return_trans
|
||||||
|
|
||||||
|
|
||||||
@ -116,7 +115,6 @@ async def patch_transactions(
|
|||||||
|
|
||||||
these_moves = Transaction.select().where(Transaction.moveid == move_id)
|
these_moves = Transaction.select().where(Transaction.moveid == move_id)
|
||||||
if these_moves.count() == 0:
|
if these_moves.count() == 0:
|
||||||
db.close()
|
|
||||||
raise HTTPException(status_code=404, detail=f"Move ID {move_id} not found")
|
raise HTTPException(status_code=404, detail=f"Move ID {move_id} not found")
|
||||||
|
|
||||||
if frozen is not None:
|
if frozen is not None:
|
||||||
@ -128,7 +126,6 @@ async def patch_transactions(
|
|||||||
x.cancelled = cancelled
|
x.cancelled = cancelled
|
||||||
x.save()
|
x.save()
|
||||||
|
|
||||||
db.close()
|
|
||||||
return f"Updated {these_moves.count()} transactions"
|
return f"Updated {these_moves.count()} transactions"
|
||||||
|
|
||||||
|
|
||||||
@ -178,7 +175,6 @@ async def post_transactions(
|
|||||||
for batch in chunked(all_moves, 15):
|
for batch in chunked(all_moves, 15):
|
||||||
Transaction.insert_many(batch).on_conflict_ignore().execute()
|
Transaction.insert_many(batch).on_conflict_ignore().execute()
|
||||||
|
|
||||||
db.close()
|
|
||||||
return f"{len(all_moves)} transactions have been added"
|
return f"{len(all_moves)} transactions have been added"
|
||||||
|
|
||||||
|
|
||||||
@ -192,7 +188,6 @@ async def delete_transactions(move_id, token: str = Depends(oauth2_scheme)):
|
|||||||
delete_query = Transaction.delete().where(Transaction.moveid == move_id)
|
delete_query = Transaction.delete().where(Transaction.moveid == move_id)
|
||||||
|
|
||||||
count = delete_query.execute()
|
count = delete_query.execute()
|
||||||
db.close()
|
|
||||||
if count > 0:
|
if count > 0:
|
||||||
return f"Removed {count} transactions"
|
return f"Removed {count} transactions"
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user