Files
F-r-ldraledigplannerare/backend/app/routers/leave_wishes.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

145 lines
6.1 KiB
Python

"""HTTP routes for leave wishes.
Routers stay thin: validate via Pydantic, call the service, map domain errors
to HTTP. Business logic (recurrence materialisation, parent_mode validation,
month-day range checks) lives in LeaveWishService. See PLANNING_ENGINE_SPEC.md §4.
A repeating wish is stored as one row per year (see app/entities/leave_wish.py),
so PUT/DELETE address a single year by default. `?scope=series` widens them to
every year of the series the addressed row belongs to — the "this year" vs
"every year" choice the calendar's period popover offers. On a standalone
occurrence the two scopes are the same thing.
"""
from typing import Annotated, Literal
from uuid import UUID
from fastapi import APIRouter, HTTPException, Query, status
from app.dependencies import LeaveWishServiceDep, WorkspaceIdDep, WorkspaceScopeDep
from app.models.leave_wish import (
LeaveWishCreate,
LeaveWishResponse,
LeaveWishUpdate,
)
from app.utils.errors import NotFoundError, ValidationError
ScopeQuery = Annotated[
Literal["occurrence", "series"],
Query(description="'occurrence' = this year only; 'series' = every year of the wish"),
]
router = APIRouter(prefix="/api/leave-wishes", tags=["leave-wishes"])
@router.post("", response_model=LeaveWishResponse, status_code=status.HTTP_201_CREATED)
async def create_leave_wish(
payload: LeaveWishCreate, service: LeaveWishServiceDep, scope: WorkspaceScopeDep
) -> LeaveWishResponse:
scope.check_workspace(payload.workspace_id)
if payload.child_id is not None:
await scope.check_child(payload.child_id)
if payload.parent_personal_data_uuid is not None:
await scope.check_member(payload.parent_personal_data_uuid)
try:
entity = await service.create(
workspace_id=payload.workspace_id,
child_id=payload.child_id,
name=payload.name,
name_message_key=payload.name_message_key,
window_start_month=payload.window_start_month,
window_start_day=payload.window_start_day,
window_end_month=payload.window_end_month,
window_end_day=payload.window_end_day,
target_weeks=payload.target_weeks,
target_days=payload.target_days,
recurrence=payload.recurrence,
first_year=payload.first_year,
last_year=payload.last_year,
priority=payload.priority,
parent_mode=payload.parent_mode,
parent_personal_data_uuid=payload.parent_personal_data_uuid,
window_mode=payload.window_mode,
series_id=payload.series_id,
wants_dubbeldag=payload.wants_dubbeldag,
)
except ValidationError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, str(exc)) from exc
return LeaveWishResponse.from_entity(entity)
@router.get("/{workspace_id}", response_model=list[LeaveWishResponse])
async def list_leave_wishes(
workspace_id: WorkspaceIdDep, service: LeaveWishServiceDep
) -> list[LeaveWishResponse]:
"""List all leave wishes for a workspace. Matches PLANNING_ENGINE_SPEC.md §4
(`GET /api/leave-wishes/{workspace_id}` — list-by-workspace, not get-by-id)."""
entities = await service.list_for_workspace(workspace_id)
return [LeaveWishResponse.from_entity(e) for e in entities]
@router.put("/{leave_wish_id}", response_model=LeaveWishResponse)
async def update_leave_wish(
leave_wish_id: UUID,
payload: LeaveWishUpdate,
service: LeaveWishServiceDep,
workspace_scope: WorkspaceScopeDep,
scope: ScopeQuery = "occurrence",
) -> LeaveWishResponse:
if payload.child_id is not None:
await workspace_scope.check_child(payload.child_id)
if payload.parent_personal_data_uuid is not None:
await workspace_scope.check_member(payload.parent_personal_data_uuid)
fields_set = payload.model_fields_set
try:
entity = await service.update(
leave_wish_id=leave_wish_id,
workspace_id=workspace_scope.workspace_id,
scope=scope,
child_id=payload.child_id,
name=payload.name,
name_message_key=payload.name_message_key,
window_start_month=payload.window_start_month,
window_start_day=payload.window_start_day,
window_end_month=payload.window_end_month,
window_end_day=payload.window_end_day,
target_weeks=payload.target_weeks,
target_days=payload.target_days,
recurrence=payload.recurrence,
first_year=payload.first_year,
last_year=payload.last_year,
priority=payload.priority,
parent_mode=payload.parent_mode,
parent_personal_data_uuid=payload.parent_personal_data_uuid,
window_mode=payload.window_mode,
wants_dubbeldag=payload.wants_dubbeldag,
_clear_child_id="child_id" in fields_set and payload.child_id is None,
_clear_last_year="last_year" in fields_set and payload.last_year is None,
_clear_parent_personal_data_uuid=(
"parent_personal_data_uuid" in fields_set
and payload.parent_personal_data_uuid is None
),
_clear_name_message_key=(
"name_message_key" in fields_set and payload.name_message_key is None
),
_clear_target_days="target_days" in fields_set and payload.target_days is None,
)
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
return LeaveWishResponse.from_entity(entity)
@router.delete("/{leave_wish_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_leave_wish(
leave_wish_id: UUID,
service: LeaveWishServiceDep,
workspace_scope: WorkspaceScopeDep,
scope: ScopeQuery = "occurrence",
) -> None:
try:
await service.delete(leave_wish_id, workspace_id=workspace_scope.workspace_id, scope=scope)
except NotFoundError as exc:
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc