Files
F-r-ldraledigplannerare/backend/app/routers/settings.py
T
jacobh2 4d70c8950d Stop emitting deprecation warnings from our own backend code
`uv run pytest` reported 26 DeprecationWarnings, 20 of them raised by
code we own. Two independent sources, both already renamed upstream.

Starlette renamed HTTP_422_UNPROCESSABLE_ENTITY to
HTTP_422_UNPROCESSABLE_CONTENT, matching the RFC 9110 name for the
status. Rename all 21 call sites across the twelve routers that raise
it. The numeric value is unchanged at 422, so the wire format and the
frontend's error handling are untouched -- this is forward-compat debt
only. The warnings attributed to fastapi/routing.py were these same
handler frames raising through the dependency call, and go with them.

Alembic >=1.16 supersedes `version_path_separator` with
`path_separator`, which governs both `prepend_sys_path` and
`version_locations`; its absence is what triggered the legacy-splitting
warning on every config load. Swap the key in alembic.ini.

Deliberately not fixing slowapi's `asyncio.iscoroutinefunction` warning
(6 of the 26): it is upstream with no released fix, and no
`filterwarnings` entry is added, so it stays visible as a reminder to
drop the pin once one lands.

Verified from backend/: ruff check and format clean, 893 passed with
coverage at 92%, and the warnings summary is now exactly those 6 slowapi
lines. `alembic upgrade head` runs 001 through 008 without the
path_separator warning.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 18:47:29 +02:00

170 lines
5.6 KiB
Python

"""HTTP routes for settings and tips.
Covers:
- Children (CRUD) — uses models/child.py
- Tips endpoint
Leave wishes (formerly "break periods") moved to their own router,
app/routers/leave_wishes.py, at /api/leave-wishes — see PLANNING_ENGINE_SPEC.md §4.
"""
from uuid import UUID
from fastapi import APIRouter, HTTPException, status
from app.dependencies import (
ChildServiceDep,
PersonalDataServiceDep,
TipsServiceDep,
WorkspaceIdDep,
WorkspaceScopeDep,
)
from app.models.child import ChildCreate, ChildResponse, ChildUpdate
from app.models.personal_data import PersonalDataResponse, PersonalDataUpdate
from app.models.tips import TipDismissalCreate, TipResponse
from app.utils.errors import NotFoundError, ValidationError
router = APIRouter(tags=["settings"])
# ---------------------------------------------------------------------------
# Tips
# ---------------------------------------------------------------------------
tips_router = APIRouter(prefix="/api/tips", tags=["tips"])
@tips_router.get("/{workspace_id}", response_model=list[TipResponse])
async def get_tips(
workspace_id: WorkspaceIdDep,
service: TipsServiceDep,
) -> list[TipResponse]:
tips = await service.get_tips(workspace_id)
return [TipResponse.from_domain(tip) for tip in tips]
@tips_router.post("/{workspace_id}/dismissals", status_code=status.HTTP_204_NO_CONTENT)
async def dismiss_tip(
workspace_id: WorkspaceIdDep,
payload: TipDismissalCreate,
service: TipsServiceDep,
) -> None:
try:
await service.dismiss(workspace_id, payload.tip_id)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
# ---------------------------------------------------------------------------
# Children
# ---------------------------------------------------------------------------
child_router = APIRouter(prefix="/api/children", tags=["children"])
def _child_to_response(child) -> ChildResponse:
return ChildResponse.from_entity(child)
@child_router.post("", response_model=ChildResponse, status_code=status.HTTP_201_CREATED)
async def create_child(
payload: ChildCreate,
service: ChildServiceDep,
scope: WorkspaceScopeDep,
) -> ChildResponse:
scope.check_workspace(payload.workspace_id)
try:
child = await service.create(
workspace_id=payload.workspace_id,
name=payload.name,
birth_date=payload.birth_date,
preschool_start_date=payload.preschool_start_date,
preschool_planning_days_per_year=payload.preschool_planning_days_per_year,
)
return _child_to_response(child)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, str(exc)) from exc
@child_router.get("/{child_id}", response_model=ChildResponse)
async def get_child(
child_id: UUID,
service: ChildServiceDep,
scope: WorkspaceScopeDep,
) -> ChildResponse:
try:
child = await service.get(child_id, workspace_id=scope.workspace_id)
return _child_to_response(child)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
@child_router.get("/workspace/{workspace_id}", response_model=list[ChildResponse])
async def list_children(
workspace_id: WorkspaceIdDep,
service: ChildServiceDep,
) -> list[ChildResponse]:
children = await service.list_by_workspace(workspace_id)
return [_child_to_response(c) for c in children]
# Settings endpoint for updating child
settings_child_router = APIRouter(prefix="/api/settings", tags=["settings"])
@settings_child_router.put("/child/{child_id}", response_model=ChildResponse)
async def update_child(
child_id: UUID,
payload: ChildUpdate,
service: ChildServiceDep,
scope: WorkspaceScopeDep,
) -> ChildResponse:
try:
child = await service.update(
child_id=child_id,
name=payload.name,
birth_date=payload.birth_date,
preschool_start_date=payload.preschool_start_date,
preschool_planning_days_per_year=payload.preschool_planning_days_per_year,
workspace_id=scope.workspace_id,
)
return _child_to_response(child)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, str(exc)) from exc
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
@settings_child_router.delete("/{child_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_child(
child_id: UUID,
service: ChildServiceDep,
scope: WorkspaceScopeDep,
) -> None:
try:
await service.delete(child_id, workspace_id=scope.workspace_id)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
@settings_child_router.put("/parent/{personal_data_uuid}", response_model=PersonalDataResponse)
async def update_parent_settings(
personal_data_uuid: UUID,
payload: PersonalDataUpdate,
service: PersonalDataServiceDep,
scope: WorkspaceScopeDep,
) -> PersonalDataResponse:
try:
updated = await service.update(
personal_data_uuid,
salary_type=payload.salary_type,
employment_status=payload.employment_status,
min_vacation_days_per_year=payload.min_vacation_days_per_year,
workspace_id=scope.workspace_id,
)
return PersonalDataResponse.from_entity(updated)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, str(exc)) from exc
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc