Spaces:
Running on T4
Running on T4
|
Download backend/CONTRIBUTING.md from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 5.13 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/CONTRIBUTING.md
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/CONTRIBUTING.md
-
curl -L -o CONTRIBUTING.md https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/CONTRIBUTING.md
5.13 kB
Backend rules
These rules apply to every change in backend/. The design is in DESIGN.md. The frontend spec and its JSON examples are in docs/reference/. Those examples use camelCase; we use snake_case for the same fields.
Layers (one job each)
controller.py: HTTP only.- Declare the
routerand the endpoints. Parse requests intoRequestModelsubclasses, call one service method and returnResponseModelsubclasses. - No SQL and no business rules.
- Declare the
service.py: business rules.- A plain class that receives its repositories and integrations through the constructor, for example
TeamService(db, storage). - It raises
app.core.errorsexceptions and never returns HTTP responses.
- A plain class that receives its repositories and integrations through the constructor, for example
repository.py: database only.- Subclass
BaseRepository[Model]and setmodel = .... - Add only the queries the module needs. Never commit: the request's unit of work (
get_db) commits.
- Subclass
schemas.py: request models subclasscommon.schemas.RequestModel, which rejects unknown fields and trims strings. Response models subclassResponseModel.- Wiring: controllers build services from dependencies (
Db,Storage,CurrentAccount,CurrentCoach,CurrentPlayer,Pagingfromapp.core.dependencies), either directly or through a smalldef get_x_service(db: Db) -> XServicedependency.
Shared code: use it, don't copy it
| Need | Use |
|---|---|
| CRUD | app.common.base_repository.BaseRepository (get, get_or_404, first_by, list_by, exists_by, create, update, delete) |
| Errors | app.core.errors: ValidationFailed (400), Unauthenticated (401), PlanLimitReached (402), Forbidden (403), NotFound (404), Conflict (409, pass code=), TooLarge (413), Unprocessable (422), RateLimited (429), ServiceUnavailable (503). Pass code= for spec codes such as EMAIL_TAKEN and fields= for field errors. |
| Lists | app.core.pagination (Paging dependency, paginate(rows_fetched_with_limit_plus_1, limit, key) -> (page, next_cursor), encode_cursor) and common.schemas.Page[T], which returns {items, next_cursor} |
| Plan and usage | app.modules.billing.entitlement.EntitlementService(db) (get, ensure_can_analyze) |
| Retried writes | app.modules.idempotency.service.run_idempotent (Idempotency-Key header; pass scope=<path id> when the route has a target id) |
| Session list items | app.modules.sessions.summary (summary_query, load_summaries, SessionSummary) |
| Team shape | app.modules.teams.schemas.TeamOut |
| Review shape | app.modules.reviews.schemas.ReviewOut |
| Storage | app.integrations.storage.StorageClient (presign_put, presign_get, head, put_bytes, delete_many) |
| Media URLs | app.common.media (media_ref, upload_ref) build every MediaRef |
| Claiming an upload | app.common.uploads.claim_upload (lock, owner, kind, already used, bytes present, set attached_at; each module passes its own UploadErrors) |
| Analysis consent | app.common.consent.analysis_consent_blocked(db, account_id, require_accepted) |
| Labels and tones | app.common.labels (DISCIPLINES, FOCUS_LABEL, tone, tone_counts, drill_catalog) |
| Account names | app.common.names.account_names(session, ids) |
| Tokens and passwords | app.core.security |
| Rate limits | app.core.rate_limit (RateLimiter and one named getter per limiter, e.g. get_academy_apply_limiter; tests reset all limiters automatically) |
Do not edit these shared files. If one needs a change, describe it in your report instead:
app/core/*app/common/*- any
models.py app/models.pyapp/main.pymigrations/tests/conftest.pytests/helpers.pybilling/entitlement.pyidempotency/service.pysessions/summary.pyteams/schemas.pyandreviews/schemas.py: you may add classes, but don't changeTeamOutorReviewOutpyproject.toml
A module may read another module's tables only through its own repository queries on the shared models. Never import another module's service or repository.
Style
- Every JSON key is snake_case. Response shapes are minimal: no field the app doesn't need, and nothing derivable is stored.
- Keep comment density low: a module docstring and short comments only where the reason is not obvious.
- Line length is 120.
ruff check app testsmust pass.
Tests
- Integration tests go in
tests/integration/test_<module>.pyand call real endpoints with theclientfixture andtests.helpers.register. The harness gives each test run a fresh Postgres database (migrated by Alembic) and an in-process S3 server. - Unit tests for pure logic go in
tests/unit/. - Running tests: each parallel run uses its own database. Run
TEST_DATABASE_NAME=app_test_<yourname> .venv/bin/python -m pytest tests/...frombackend/. - Coverage: every endpoint gets a success test plus a test for each error code it can return.
- External services (FCM, email, Apple, Google): define a small client class in
app/integrations/and use a fake of it in tests viaapp.dependency_overrides.