Full CRUD on PurchaseRound and WineOffering, org-scoped via two new
join-based dependencies.py helpers (get_round_in_organization,
get_wine_offering_in_organization) mirroring task 5's pattern. New
rounds always start as draft regardless of what the client posts,
avoiding a confusing creation-time constraint error.
POST /purchase-rounds/{id}/copy implements "kopiér fra forrige runde":
duplicates every round field (dates, texts) and every wine offering
(incl. category) into a fresh draft, leaving the source untouched.
PurchaseRound DELETE gets a three-tier policy based on status: draft
deletable by any admin, open never deletable, closed only by an
elevated superadmin. This is runtime-conditional on the loaded row, so
the elevation check was extracted out of get_current_active_superuser
into a standalone require_elevated_superuser(user, token) helper that
both the dependency and this handler call directly. WineOffering
delete has no such tier — an offering with real order lines is already
blocked by the existing RESTRICT FK, caught here as a 409.
Also seeds the 8 wine categories (empty table blocked any offering
creation) via a plain Alembic data migration, idempotent and
unconditional (no secret involved, unlike the task-4 superuser seed).
Verified end-to-end: dates-required 409, open round successfully
patched with dates, wine offerings created, round copied (new draft,
duplicated offerings with new ids/same category), open-round delete
403, closed-round delete 403 then 204 after /auth/elevate, wine
offering delete 204, draft round delete 204 (cascades its remaining
offering). DB left clean, participant count unaffected.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
from datetime import datetime
|
|
from decimal import Decimal
|
|
from enum import Enum
|
|
from typing import TYPE_CHECKING, List, Optional
|
|
|
|
from sqlalchemy import CheckConstraint, Column
|
|
from sqlalchemy import Enum as SAEnum
|
|
from sqlmodel import Field, Relationship, SQLModel
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.order import Order
|
|
from app.models.route import Route
|
|
from app.models.wine_offering import WineOffering
|
|
|
|
|
|
class PurchaseRoundStatus(str, Enum):
|
|
DRAFT = "draft"
|
|
OPEN = "open"
|
|
CLOSED = "closed"
|
|
|
|
|
|
class PurchaseRoundBase(SQLModel):
|
|
name: str
|
|
status: PurchaseRoundStatus = Field(
|
|
default=PurchaseRoundStatus.DRAFT,
|
|
sa_column=Column(
|
|
SAEnum(
|
|
PurchaseRoundStatus,
|
|
name="purchase_round_status",
|
|
native_enum=False,
|
|
values_callable=lambda enum_cls: [member.value for member in enum_cls],
|
|
),
|
|
nullable=False,
|
|
),
|
|
)
|
|
opens_at: Optional[datetime] = Field(default=None)
|
|
order_deadline_at: Optional[datetime] = Field(default=None)
|
|
pickup_at: Optional[datetime] = Field(default=None)
|
|
eur_dkk_rate: Optional[Decimal] = Field(default=None, max_digits=10, decimal_places=4)
|
|
intro_text: Optional[str] = Field(default=None)
|
|
pickup_info_text: Optional[str] = Field(default=None)
|
|
|
|
|
|
class PurchaseRound(PurchaseRoundBase, table=True):
|
|
__tablename__ = "purchase_round"
|
|
__table_args__ = (
|
|
CheckConstraint(
|
|
"status = 'draft' OR "
|
|
"(opens_at IS NOT NULL AND order_deadline_at IS NOT NULL AND pickup_at IS NOT NULL)",
|
|
name="ck_purchase_round_dates_required_unless_draft",
|
|
),
|
|
)
|
|
|
|
id: Optional[int] = Field(default=None, primary_key=True)
|
|
route_id: int = Field(foreign_key="route.id", ondelete="CASCADE")
|
|
|
|
route: "Route" = Relationship(back_populates="purchase_rounds")
|
|
wine_offerings: List["WineOffering"] = Relationship(
|
|
back_populates="purchase_round", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
|
|
)
|
|
orders: List["Order"] = Relationship(
|
|
back_populates="purchase_round", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
|
|
)
|
|
|
|
|
|
class PurchaseRoundCreate(PurchaseRoundBase):
|
|
route_id: int
|
|
|
|
|
|
class PurchaseRoundUpdate(SQLModel):
|
|
name: Optional[str] = None
|
|
status: Optional[PurchaseRoundStatus] = None
|
|
opens_at: Optional[datetime] = None
|
|
order_deadline_at: Optional[datetime] = None
|
|
pickup_at: Optional[datetime] = None
|
|
eur_dkk_rate: Optional[Decimal] = None
|
|
intro_text: Optional[str] = None
|
|
pickup_info_text: Optional[str] = None
|
|
|
|
|
|
class PurchaseRoundPublic(PurchaseRoundBase):
|
|
id: int
|
|
route_id: int
|