Files
F-r-ldraledigplannerare/backend/app/routers/settings.py
T
jacobh2 64734ae6e7 Silence the deprecation warnings our own code raises
Starlette renamed HTTP_422_UNPROCESSABLE_ENTITY to HTTP_422_UNPROCESSABLE_CONTENT
and now warns on the old spelling; the value is unchanged at 422, so the 20 call
sites in the routers move over with no wire-format or frontend impact.

Alembic >=1.16 supersedes version_path_separator with path_separator, which
governs both prepend_sys_path and version_locations, and warns when it is absent.

Together this takes pytest from 26 deprecation warnings to 6. The remainder are
slowapi calling asyncio.iscoroutinefunction; they are left visible deliberately,
with no filterwarnings entry, as a reminder to revisit once upstream ships a fix.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 18:48:02 +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