Spaces:
Running on T4
Running on T4
Download backend/app/modules/drills/service.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/drills/service.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/modules/drills/service.py
-
curl -L -o service.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/drills/service.py
1.66 kB
| """Drill logs (E1, E2). drill_id must be in the AI coach drill library.""" | |
| from datetime import date | |
| from functools import cache | |
| from ai_coach.knowledge import all_drills | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.common.schemas import Items | |
| from app.core.errors import NotFound, ValidationFailed | |
| from app.modules.accounts.models import Account | |
| from app.modules.drills.repository import DrillLogRepository | |
| from app.modules.drills.schemas import DrillLogIn, DrillLogOut | |
| MAX_RANGE_DAYS = 90 | |
| def drill_ids() -> frozenset[str]: | |
| return frozenset(d["id"] for d in all_drills()) | |
| class DrillLogService: | |
| def __init__(self, db: AsyncSession): | |
| self.logs = DrillLogRepository(db) | |
| async def create(self, account: Account, body: DrillLogIn) -> DrillLogOut: | |
| if body.drill_id not in drill_ids(): | |
| raise ValidationFailed(fields={"drill_id": "Unknown drill."}) | |
| if body.session_id and not await self.logs.owns_session(account.id, body.session_id): | |
| raise NotFound() | |
| log = await self.logs.create(owner_id=account.id, **body.model_dump()) | |
| return DrillLogOut.model_validate(log) | |
| async def list(self, account: Account, start: date, end: date) -> Items[DrillLogOut]: | |
| if end < start: | |
| raise ValidationFailed(fields={"to": "Must be on or after from."}) | |
| if (end - start).days > MAX_RANGE_DAYS: | |
| raise ValidationFailed(fields={"to": f"The range can be at most {MAX_RANGE_DAYS} days."}) | |
| logs = await self.logs.in_range(account.id, start, end) | |
| return Items[DrillLogOut](items=[DrillLogOut.model_validate(log) for log in logs]) | |