Étape 9 : suppression du statut d'affectation (plus de confirmation)
Une affectation est valide dès sa création ; la retirer = supprimer la ligne.
- Modèle : suppression de AssignmentStatus, ACTIVE_ASSIGNMENT_STATUSES et de
Assignment.status ; migration 0003 (drop_column, alembic check vert)
- Loader de validation : toutes les affectations du bénévole comptent, plus de
filtre par statut
- API : AssignmentCreate/Update/Read sans status ; création → validation
toujours exécutée (blocage si violation bloquante) ; PATCH ne porte plus que
source/locked
- Matrice : MatrixShift.{confirmed, proposed} → {assigned} (compte unique)
- Seed : affectations sans statut, résumé « Affectations » (260)
- Tests mis à jour (matrice, loader, modèle, API) ; 128 tests au total
- CLAUDE.md : glossaire, modèle, sémantique de validation, périmètre
Frontend : les références au statut (propositions, badges confirmé/proposé,
écran de confirmation mobile, mock, schema.ts) restent à retirer séparément.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
8a879ca5c3
commit
464635656a
@@ -1,5 +1,6 @@
|
||||
"""CRUD des affectations. Toute écriture passe par le moteur de validation :
|
||||
une violation bloquante interdit l'enregistrement, les avertissements sont
|
||||
"""CRUD des affectations. Une affectation est valide dès sa création (pas
|
||||
d'étape de confirmation). La création passe par le moteur de validation : une
|
||||
violation bloquante interdit l'enregistrement, les avertissements sont
|
||||
renvoyés."""
|
||||
|
||||
import uuid
|
||||
@@ -23,15 +24,7 @@ from app.auth.deps import (
|
||||
require_staff,
|
||||
)
|
||||
from app.db import get_session
|
||||
from app.models import (
|
||||
ACTIVE_ASSIGNMENT_STATUSES,
|
||||
Account,
|
||||
Assignment,
|
||||
Edition,
|
||||
Role,
|
||||
Shift,
|
||||
Volunteer,
|
||||
)
|
||||
from app.models import Account, Assignment, Edition, Role, Shift, Volunteer
|
||||
from app.validation import has_blocking, validate
|
||||
from app.validation.loader import build_validation_input
|
||||
|
||||
@@ -114,32 +107,27 @@ def create_assignment(
|
||||
status_code=409, detail={"code": "assignment.duplicate"}
|
||||
)
|
||||
|
||||
# La validation ne s'applique qu'aux affectations actives : créer
|
||||
# directement une affectation DECLINEE ou ANNULEE n'a rien à valider.
|
||||
violations: list = []
|
||||
if payload.status in ACTIVE_ASSIGNMENT_STATUSES:
|
||||
violations = validate(
|
||||
build_validation_input(
|
||||
session=session,
|
||||
edition=edition,
|
||||
volunteer=volunteer,
|
||||
shift=shift,
|
||||
)
|
||||
violations = validate(
|
||||
build_validation_input(
|
||||
session=session,
|
||||
edition=edition,
|
||||
volunteer=volunteer,
|
||||
shift=shift,
|
||||
)
|
||||
)
|
||||
if has_blocking(violations):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail={
|
||||
"code": "error.validation_failed",
|
||||
"violations": [v.model_dump() for v in violations],
|
||||
},
|
||||
)
|
||||
if has_blocking(violations):
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail={
|
||||
"code": "error.validation_failed",
|
||||
"violations": [v.model_dump() for v in violations],
|
||||
},
|
||||
)
|
||||
|
||||
assignment = Assignment(
|
||||
edition_id=edition.id,
|
||||
volunteer_id=volunteer.id,
|
||||
shift_id=shift.id,
|
||||
status=payload.status,
|
||||
source=payload.source,
|
||||
locked=payload.locked,
|
||||
)
|
||||
@@ -202,29 +190,17 @@ def update_assignment(
|
||||
for field, value in changes.items():
|
||||
setattr(assignment, field, value)
|
||||
|
||||
warnings: list = []
|
||||
if assignment.status in ACTIVE_ASSIGNMENT_STATUSES:
|
||||
volunteer = session.get(Volunteer, assignment.volunteer_id)
|
||||
shift = session.get(Shift, assignment.shift_id)
|
||||
warnings = validate(
|
||||
build_validation_input(
|
||||
session=session,
|
||||
edition=edition,
|
||||
volunteer=volunteer,
|
||||
shift=shift,
|
||||
exclude_assignment_id=assignment.id,
|
||||
)
|
||||
# `source` / `locked` ne changent pas le résultat de validation ; on le
|
||||
# rejoue seulement pour renvoyer les avertissements courants.
|
||||
warnings = validate(
|
||||
build_validation_input(
|
||||
session=session,
|
||||
edition=edition,
|
||||
volunteer=session.get(Volunteer, assignment.volunteer_id),
|
||||
shift=session.get(Shift, assignment.shift_id),
|
||||
exclude_assignment_id=assignment.id,
|
||||
)
|
||||
if has_blocking(warnings):
|
||||
session.rollback()
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail={
|
||||
"code": "error.validation_failed",
|
||||
"violations": [v.model_dump() for v in warnings],
|
||||
},
|
||||
)
|
||||
|
||||
)
|
||||
session.commit()
|
||||
session.refresh(assignment)
|
||||
return AssignmentWriteResult(assignment=assignment, warnings=warnings)
|
||||
|
||||
@@ -8,14 +8,7 @@ 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,
|
||||
)
|
||||
from app.models import Account, Assignment, Edition, Position, Shift
|
||||
|
||||
router = APIRouter(prefix="/editions/{edition_id}/matrix", tags=["matrix"])
|
||||
|
||||
@@ -41,13 +34,11 @@ def get_matrix(
|
||||
)
|
||||
)
|
||||
counts = {
|
||||
(shift_id, status): total
|
||||
for shift_id, status, total in session.exec(
|
||||
select(
|
||||
Assignment.shift_id, Assignment.status, func.count()
|
||||
)
|
||||
shift_id: total
|
||||
for shift_id, total in session.exec(
|
||||
select(Assignment.shift_id, func.count())
|
||||
.where(Assignment.edition_id == edition.id)
|
||||
.group_by(Assignment.shift_id, Assignment.status)
|
||||
.group_by(Assignment.shift_id)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -60,12 +51,7 @@ def get_matrix(
|
||||
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
|
||||
),
|
||||
assigned=counts.get(shift.id, 0),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from app.models import (
|
||||
AssignmentSource,
|
||||
AssignmentStatus,
|
||||
PositionCategory,
|
||||
Role,
|
||||
TshirtSize,
|
||||
@@ -129,13 +128,11 @@ class ShiftRead(ApiModel):
|
||||
class AssignmentCreate(ApiModel):
|
||||
volunteer_id: uuid.UUID
|
||||
shift_id: uuid.UUID
|
||||
status: AssignmentStatus = AssignmentStatus.PROPOSED
|
||||
source: AssignmentSource = AssignmentSource.MANUAL
|
||||
locked: bool = False
|
||||
|
||||
|
||||
class AssignmentUpdate(ApiModel):
|
||||
status: AssignmentStatus | None = None
|
||||
source: AssignmentSource | None = None
|
||||
locked: bool | None = None
|
||||
|
||||
@@ -145,7 +142,6 @@ class AssignmentRead(ApiModel):
|
||||
edition_id: uuid.UUID
|
||||
volunteer_id: uuid.UUID
|
||||
shift_id: uuid.UUID
|
||||
status: AssignmentStatus
|
||||
source: AssignmentSource
|
||||
locked: bool
|
||||
created_at: dt.datetime
|
||||
@@ -176,8 +172,7 @@ class MatrixShift(ApiModel):
|
||||
ends_at: dt.datetime
|
||||
min_staff: int
|
||||
ideal_staff: int
|
||||
confirmed: int
|
||||
proposed: int
|
||||
assigned: int
|
||||
|
||||
|
||||
class MatrixPosition(ApiModel):
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Affectation : suppression du statut (plus d'étape de confirmation)
|
||||
|
||||
Revision ID: 0003_drop_assignment_status
|
||||
Revises: 0002_account_password
|
||||
Create Date: 2026-09-10
|
||||
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0003_drop_assignment_status"
|
||||
down_revision: str | None = "0002_account_password"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
_STATUS = sa.Enum(
|
||||
"PROPOSED", "CONFIRMED", "DECLINED", "CANCELLED",
|
||||
name="assignmentstatus", native_enum=False,
|
||||
)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.drop_column("assignment", "status")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.add_column(
|
||||
"assignment",
|
||||
sa.Column(
|
||||
"status", _STATUS, server_default="PROPOSED", nullable=False
|
||||
),
|
||||
)
|
||||
@@ -20,9 +20,7 @@ from app.models.domain import (
|
||||
VolunteerSkill,
|
||||
)
|
||||
from app.models.enums import (
|
||||
ACTIVE_ASSIGNMENT_STATUSES,
|
||||
AssignmentSource,
|
||||
AssignmentStatus,
|
||||
AvailabilityStatus,
|
||||
PositionCategory,
|
||||
Role,
|
||||
@@ -30,11 +28,9 @@ from app.models.enums import (
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ACTIVE_ASSIGNMENT_STATUSES",
|
||||
"Account",
|
||||
"Assignment",
|
||||
"AssignmentSource",
|
||||
"AssignmentStatus",
|
||||
"Availability",
|
||||
"AvailabilityStatus",
|
||||
"DayPart",
|
||||
|
||||
@@ -16,7 +16,6 @@ from sqlmodel import Field, Relationship, SQLModel, UniqueConstraint
|
||||
from app.models.base import TimestampMixin
|
||||
from app.models.enums import (
|
||||
AssignmentSource,
|
||||
AssignmentStatus,
|
||||
AvailabilityStatus,
|
||||
PositionCategory,
|
||||
Role,
|
||||
@@ -293,7 +292,9 @@ class Availability(TimestampMixin, table=True):
|
||||
class Assignment(TimestampMixin, table=True):
|
||||
"""Décision de l'organisation : « tu tiens tel créneau ».
|
||||
|
||||
À ne pas confondre avec Availability (déclaration du bénévole).
|
||||
Existe ⇒ le bénévole est affecté (pas d'étape de confirmation). Retirer une
|
||||
affectation, c'est supprimer la ligne. À ne pas confondre avec Availability
|
||||
(déclaration du bénévole).
|
||||
"""
|
||||
|
||||
__table_args__ = (
|
||||
@@ -312,14 +313,6 @@ class Assignment(TimestampMixin, table=True):
|
||||
shift_id: uuid.UUID = Field(
|
||||
foreign_key="shift.id", index=True, ondelete="CASCADE"
|
||||
)
|
||||
status: AssignmentStatus = Field(
|
||||
default=AssignmentStatus.PROPOSED,
|
||||
sa_column=Column(
|
||||
SAEnum(AssignmentStatus, native_enum=False),
|
||||
nullable=False,
|
||||
server_default=AssignmentStatus.PROPOSED.value,
|
||||
),
|
||||
)
|
||||
source: AssignmentSource = Field(
|
||||
default=AssignmentSource.MANUAL,
|
||||
sa_column=Column(
|
||||
|
||||
@@ -19,13 +19,6 @@ class AvailabilityStatus(enum.StrEnum):
|
||||
UNAVAILABLE = "UNAVAILABLE"
|
||||
|
||||
|
||||
class AssignmentStatus(enum.StrEnum):
|
||||
PROPOSED = "PROPOSED"
|
||||
CONFIRMED = "CONFIRMED"
|
||||
DECLINED = "DECLINED"
|
||||
CANCELLED = "CANCELLED"
|
||||
|
||||
|
||||
class AssignmentSource(enum.StrEnum):
|
||||
SOLVER = "SOLVER"
|
||||
MANUAL = "MANUAL"
|
||||
@@ -51,10 +44,3 @@ class TshirtSize(enum.StrEnum):
|
||||
XL = "XL"
|
||||
XXL = "XXL"
|
||||
XXXL = "XXXL"
|
||||
|
||||
|
||||
# Statuts d'affectation « actifs » : seuls ceux-ci comptent pour les règles de
|
||||
# chevauchement, de cumul horaire et de repos (voir app/validation).
|
||||
ACTIVE_ASSIGNMENT_STATUSES: frozenset[AssignmentStatus] = frozenset(
|
||||
{AssignmentStatus.PROPOSED, AssignmentStatus.CONFIRMED}
|
||||
)
|
||||
|
||||
+4
-13
@@ -20,7 +20,6 @@ from app.models import (
|
||||
Account,
|
||||
Assignment,
|
||||
AssignmentSource,
|
||||
AssignmentStatus,
|
||||
Availability,
|
||||
AvailabilityStatus,
|
||||
DayPart,
|
||||
@@ -225,7 +224,7 @@ class Summary:
|
||||
f" Postes : {self.positions}",
|
||||
f" Créneaux : {self.shifts}",
|
||||
f" Créneaux sous l'effectif minimal (trous) : {self.understaffed_shifts}",
|
||||
f" Affectations actives : {self.assignments}",
|
||||
f" Affectations : {self.assignments}",
|
||||
f" Avertissements actifs : {sum(self.warnings.values())}",
|
||||
]
|
||||
for code, count in sorted(self.warnings.items()):
|
||||
@@ -490,11 +489,6 @@ def build(session: Session, *, rng: random.Random | None = None) -> Summary:
|
||||
edition_id=edition.id,
|
||||
volunteer_id=volunteer.id,
|
||||
shift_id=shift.id,
|
||||
status=(
|
||||
AssignmentStatus.CONFIRMED
|
||||
if rng.random() < 0.55
|
||||
else AssignmentStatus.PROPOSED
|
||||
),
|
||||
source=AssignmentSource.MANUAL,
|
||||
)
|
||||
)
|
||||
@@ -662,7 +656,6 @@ def _add_active(session, edition, volunteer, shift, assigned) -> None:
|
||||
edition_id=edition.id,
|
||||
volunteer_id=volunteer.id,
|
||||
shift_id=shift.id,
|
||||
status=AssignmentStatus.CONFIRMED,
|
||||
source=AssignmentSource.MANUAL,
|
||||
)
|
||||
)
|
||||
@@ -671,12 +664,10 @@ def _add_active(session, edition, volunteer, shift, assigned) -> None:
|
||||
|
||||
def _verify(session: Session, edition: Edition, summary: Summary) -> None:
|
||||
session.flush()
|
||||
active = session.exec(
|
||||
select(Assignment)
|
||||
.where(Assignment.edition_id == edition.id)
|
||||
.where(Assignment.status.in_((AssignmentStatus.PROPOSED, AssignmentStatus.CONFIRMED)))
|
||||
assignments = session.exec(
|
||||
select(Assignment).where(Assignment.edition_id == edition.id)
|
||||
)
|
||||
for assignment in active:
|
||||
for assignment in assignments:
|
||||
volunteer = session.get(Volunteer, assignment.volunteer_id)
|
||||
shift = session.get(Shift, assignment.shift_id)
|
||||
for violation in validate(
|
||||
|
||||
@@ -7,7 +7,6 @@ import uuid
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from app.models import (
|
||||
ACTIVE_ASSIGNMENT_STATUSES,
|
||||
Assignment,
|
||||
Availability,
|
||||
DayPart,
|
||||
@@ -65,13 +64,12 @@ def build_validation_input(
|
||||
)
|
||||
code_by_day_part_id = {dp.id: dp.code for dp in day_parts}
|
||||
|
||||
# Toute affectation existante du bénévole compte (une affectation = une
|
||||
# décision, toujours active).
|
||||
stmt = (
|
||||
select(Shift)
|
||||
.join(Assignment, Assignment.shift_id == Shift.id)
|
||||
.where(Assignment.volunteer_id == volunteer.id)
|
||||
.where(
|
||||
Assignment.status.in_(tuple(ACTIVE_ASSIGNMENT_STATUSES))
|
||||
)
|
||||
.where(Shift.id != shift.id)
|
||||
)
|
||||
if exclude_assignment_id is not None:
|
||||
|
||||
@@ -76,9 +76,8 @@ class AvailabilityDeclaration:
|
||||
class ValidationInput:
|
||||
"""Tout ce qu'il faut pour valider une affectation candidate.
|
||||
|
||||
`existing_shifts` = les créneaux des autres affectations **actives**
|
||||
(`PROPOSED` / `CONFIRMED`) du même bénévole ; le filtrage par statut est à
|
||||
la charge de l'appelant.
|
||||
`existing_shifts` = les créneaux des autres affectations du même bénévole
|
||||
(une affectation = une décision, il n'y a pas d'étape de confirmation).
|
||||
"""
|
||||
|
||||
candidate_shift: ShiftInfo
|
||||
|
||||
Reference in New Issue
Block a user