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 `router` and the endpoints. Parse requests into `RequestModel` subclasses, call one service method and return `ResponseModel` subclasses. | |
| - No SQL and no business rules. | |
| - **`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.errors` exceptions and never returns HTTP responses. | |
| - **`repository.py`:** database only. | |
| - Subclass `BaseRepository[Model]` and set `model = ...`. | |
| - Add only the queries the module needs. Never commit: the request's unit of work (`get_db`) commits. | |
| - **`schemas.py`:** request models subclass `common.schemas.RequestModel`, which rejects unknown fields and trims strings. Response models subclass `ResponseModel`. | |
| - **Wiring:** controllers build services from dependencies (`Db`, `Storage`, `CurrentAccount`, `CurrentCoach`, `CurrentPlayer`, `Paging` from `app.core.dependencies`), either directly or through a small `def get_x_service(db: Db) -> XService` dependency. | |
| ## 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.py` | |
| - `app/main.py` | |
| - `migrations/` | |
| - `tests/conftest.py` | |
| - `tests/helpers.py` | |
| - `billing/entitlement.py` | |
| - `idempotency/service.py` | |
| - `sessions/summary.py` | |
| - `teams/schemas.py` and `reviews/schemas.py`: you may add classes, but don't change `TeamOut` or `ReviewOut` | |
| - `pyproject.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 tests` must pass. | |
| ## Tests | |
| - **Integration tests** go in `tests/integration/test_<module>.py` and call real endpoints with the `client` fixture and `tests.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/...` from `backend/`. | |
| - **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 via `app.dependency_overrides`. | |