vinindkoeb/app/models/mail_template.py
carsten bf0affcdc6 Add mail template CRUD (task 7a), per-route and per-event-type
MailTemplate holds subject + body_html with plain {{variable}}
placeholders (no loops/conditionals — the user edits these directly
and is comfortable generating list-shaped content like the wine
catalog in code instead, per the real "Kvitering" email reviewed in
old-emails/). One template per (route, event_type), matching the
three CLAUDE.md mail-events (round_announced, order_confirmed,
payment_confirmed); UNIQUE(route_id, event_type) enforces that and
IntegrityError is caught as 409, same pattern as prior tasks.

Route gains nullable sender_name/sender_email — the real old emails
show a per-route sender identity (e.g. "Finn Gram <finn@vinindkoeb.dk>"
for this route), so it lives on Route rather than being duplicated
across each of its templates.

This is data + CRUD only. Actual Postal sending, the round-announce
action, and per-participant send logging are task 7b; a Postal
webhook receiver for delivery status is task 7c — both need real
Postal API access, still to be arranged.

old-emails/ (real historical order/participant data) is gitignored,
same treatment as old_participants.json.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-28 19:43:47 +02:00

55 lines
1.4 KiB
Python

from enum import Enum
from typing import TYPE_CHECKING, Optional
from sqlalchemy import Column, UniqueConstraint
from sqlalchemy import Enum as SAEnum
from sqlmodel import Field, Relationship, SQLModel
if TYPE_CHECKING:
from app.models.route import Route
class MailEventType(str, Enum):
ROUND_ANNOUNCED = "round_announced"
ORDER_CONFIRMED = "order_confirmed"
PAYMENT_CONFIRMED = "payment_confirmed"
class MailTemplateBase(SQLModel):
event_type: MailEventType = Field(
sa_column=Column(
SAEnum(
MailEventType,
name="mail_event_type",
native_enum=False,
values_callable=lambda enum_cls: [member.value for member in enum_cls],
),
nullable=False,
),
)
subject: str
body_html: str
class MailTemplate(MailTemplateBase, table=True):
__tablename__ = "mail_template"
__table_args__ = (UniqueConstraint("route_id", "event_type", name="uq_mail_template_route_event"),)
id: Optional[int] = Field(default=None, primary_key=True)
route_id: int = Field(foreign_key="route.id", ondelete="CASCADE")
route: "Route" = Relationship(back_populates="mail_templates")
class MailTemplateCreate(MailTemplateBase):
route_id: int
class MailTemplateUpdate(SQLModel):
subject: Optional[str] = None
body_html: Optional[str] = None
class MailTemplatePublic(MailTemplateBase):
id: int
route_id: int