- app/auth/ : jetons (lien magique aléatoire haché SHA-256, JWT HS256), dépendances get_current_account / current_account_in_edition / require_roles + require_coordinator / require_staff, et ensure_can_write_position/_shift - Endpoints app/api/auth.py : POST .../auth/magic-link (202 constant, pas de divulgation), POST .../auth/verify (JWT, jeton à usage unique), GET /auth/me - Envoi d'email stubé : le lien est journalisé (logger app.auth, identifiants seulement, aucune donnée nominative) - app/api/accounts.py : CRUD des comptes réservé à la COORDINATION - Rôles appliqués sur toute l'API : COORDINATION accès complet ; RÉFÉRENT lecture globale + écriture sur ses seuls postes/créneaux/affectations ; BÉNÉVOLE sa fiche et ses affectations, aucune écriture - imports.py aligné sur la dépendance get_edition (404 avant 403) - Secret JWT porté à >= 32 octets (config, .env.example, docker-compose) - seed : crée un compte VOLUNTEER pour chaque bénévole avec email (104 comptes) - conftest : fixtures make_account / coordinator / as_account / raw_client ; la suite existante s'exécute en COORDINATION - 13 tests d'authentification + de rôles ; 119 tests au total ; alembic check toujours vert (Account / MagicLinkToken déjà dans la migration 0001) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
"""Matrice poste × créneau d'une édition : source de l'écran principal."""
|
||
|
||
from fastapi import APIRouter, Depends
|
||
from sqlalchemy import func
|
||
from sqlmodel import Session, select
|
||
|
||
from app.api.deps import get_edition
|
||
from app.api.schemas import MatrixPosition, MatrixResponse, MatrixShift
|
||
from app.auth.deps import require_staff
|
||
from app.db import get_session
|
||
from app.models import (
|
||
Account,
|
||
Assignment,
|
||
AssignmentStatus,
|
||
Edition,
|
||
Position,
|
||
Shift,
|
||
)
|
||
|
||
router = APIRouter(prefix="/editions/{edition_id}/matrix", tags=["matrix"])
|
||
|
||
|
||
@router.get("", response_model=MatrixResponse)
|
||
def get_matrix(
|
||
edition: Edition = Depends(get_edition),
|
||
_guard: Account = Depends(require_staff),
|
||
session: Session = Depends(get_session),
|
||
) -> MatrixResponse:
|
||
positions = list(
|
||
session.exec(
|
||
select(Position)
|
||
.where(Position.edition_id == edition.id)
|
||
.order_by(Position.name)
|
||
)
|
||
)
|
||
shifts = list(
|
||
session.exec(
|
||
select(Shift)
|
||
.where(Shift.edition_id == edition.id)
|
||
.order_by(Shift.starts_at)
|
||
)
|
||
)
|
||
counts = {
|
||
(shift_id, status): total
|
||
for shift_id, status, total in session.exec(
|
||
select(
|
||
Assignment.shift_id, Assignment.status, func.count()
|
||
)
|
||
.where(Assignment.edition_id == edition.id)
|
||
.group_by(Assignment.shift_id, Assignment.status)
|
||
)
|
||
}
|
||
|
||
shifts_by_position: dict = {}
|
||
for shift in shifts:
|
||
shifts_by_position.setdefault(shift.position_id, []).append(
|
||
MatrixShift(
|
||
shift_id=shift.id,
|
||
starts_at=shift.starts_at,
|
||
ends_at=shift.ends_at,
|
||
min_staff=shift.min_staff,
|
||
ideal_staff=shift.ideal_staff,
|
||
confirmed=counts.get(
|
||
(shift.id, AssignmentStatus.CONFIRMED), 0
|
||
),
|
||
proposed=counts.get(
|
||
(shift.id, AssignmentStatus.PROPOSED), 0
|
||
),
|
||
)
|
||
)
|
||
|
||
return MatrixResponse(
|
||
edition_id=edition.id,
|
||
positions=[
|
||
MatrixPosition(
|
||
id=position.id,
|
||
name=position.name,
|
||
category=position.category,
|
||
location=position.location,
|
||
shifts=shifts_by_position.get(position.id, []),
|
||
)
|
||
for position in positions
|
||
],
|
||
)
|