Spaces:
Running on T4
Running on T4
Download backend/app/modules/academy_staff/controller.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 4.99 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/academy_staff/controller.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/modules/academy_staff/controller.py
-
curl -L -o controller.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/academy_staff/controller.py
4.99 kB
| """N26-N27 coaching access, N40-N43 academy coaches, N44-N45 access requests, N46-N48 staff codes.""" | |
| from datetime import date | |
| from typing import Annotated, Literal | |
| from uuid import UUID | |
| from fastapi import APIRouter, Header, Query, Response | |
| from fastapi.responses import JSONResponse | |
| from app.common.academy_access import CurrentAdmin, CurrentStaff, staff_context | |
| from app.common.schemas import Page | |
| from app.core.dependencies import CurrentAccount, Db, Paging | |
| from app.modules.academy_staff.schemas import ( | |
| CoachAccessBody, | |
| CoachAccessCreate, | |
| CoachBody, | |
| CoachOut, | |
| CoachUpdate, | |
| DecisionIn, | |
| DecisionOut, | |
| RequestPage, | |
| StaffCodeBody, | |
| StaffCodeCreate, | |
| StaffCodeOut, | |
| ) | |
| from app.modules.academy_staff.service import StaffService | |
| from app.modules.idempotency.service import run_idempotent | |
| router = APIRouter(prefix="/v1", tags=["academy_staff"]) | |
| IdempotencyKey = Annotated[str | None, Header(max_length=128)] | |
| async def get_coach_access(db: Db, account: CurrentAccount) -> CoachAccessBody: | |
| return CoachAccessBody(coach_access=await StaffService(db).coach_access(account, await staff_context(db, account))) | |
| async def apply_for_coach_access(body: CoachAccessCreate, db: Db, account: CurrentAccount, | |
| idempotency_key: IdempotencyKey = None) -> JSONResponse: | |
| created = {"value": True} | |
| async def handler() -> CoachAccessBody: | |
| access, created["value"] = await StaffService(db).apply(account, body.code) | |
| return CoachAccessBody(coach_access=access) | |
| response = await run_idempotent(db, account.id, idempotency_key, body, handler, status_code=201) | |
| if not created["value"]: | |
| response.status_code = 200 | |
| return response | |
| async def list_coaches(db: Db, staff: CurrentStaff, | |
| status: Literal["active", "paused", "removed", "all"] | None = None) -> Page[CoachOut]: | |
| return Page(items=await StaffService(db).list_coaches(staff, status)) | |
| async def get_coach(coach_id: UUID, db: Db, staff: CurrentAdmin, activity_from: date | None = None, | |
| activity_to: date | None = None) -> CoachBody: | |
| return CoachBody(coach=await StaffService(db).get_coach(staff, coach_id, activity_from, activity_to)) | |
| async def update_coach(coach_id: UUID, body: CoachUpdate, db: Db, staff: CurrentAdmin) -> CoachBody: | |
| return CoachBody(coach=await StaffService(db).update_coach(staff, coach_id, body)) | |
| async def remove_coach(coach_id: UUID, reassign_to: Annotated[UUID, Query()], db: Db, | |
| staff: CurrentAdmin) -> Response: | |
| await StaffService(db).remove_coach(staff, coach_id, reassign_to) | |
| return Response(status_code=204) | |
| async def list_requests(db: Db, staff: CurrentAdmin, paging: Paging, | |
| status: Literal["pending", "approved", "declined", "all"] = "all") -> RequestPage: | |
| return await StaffService(db).list_requests(staff, status, paging) | |
| async def decide_request(request_id: UUID, body: DecisionIn, db: Db, staff: CurrentAdmin, | |
| idempotency_key: IdempotencyKey = None) -> JSONResponse: | |
| return await run_idempotent(db, staff.id, idempotency_key, body, | |
| lambda: StaffService(db).decide(staff, request_id, body), status_code=200, | |
| scope=request_id) | |
| async def list_codes(db: Db, staff: CurrentAdmin, paging: Paging, | |
| status: Literal["active", "expired", "revoked", "all"] = "all") -> Page[StaffCodeOut]: | |
| items, next_cursor = await StaffService(db).list_codes(staff, status, paging) | |
| return Page(items=items, next_cursor=next_cursor) | |
| async def create_code(body: StaffCodeCreate, db: Db, staff: CurrentAdmin, | |
| idempotency_key: IdempotencyKey = None) -> JSONResponse: | |
| async def handler() -> StaffCodeBody: | |
| return StaffCodeBody(staff_code=await StaffService(db).create_code(staff, body)) | |
| return await run_idempotent(db, staff.id, idempotency_key, body, handler, status_code=201) | |
| async def revoke_code(code: str, db: Db, staff: CurrentAdmin) -> Response: | |
| await StaffService(db).revoke_code(staff, code) | |
| return Response(status_code=204) | |