vinindkoeb/app/dependencies.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

108 lines
4.1 KiB
Python

from typing import Annotated
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from sqlmodel import Session, select
from app.core.security import decode_access_token
from app.db import SessionDep
from app.models.mail_template import MailTemplate
from app.models.participant import Participant
from app.models.purchase_round import PurchaseRound
from app.models.route import Route
from app.models.user import User
from app.models.wine_offering import WineOffering
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/login")
def get_current_user(session: SessionDep, token: Annotated[str, Depends(oauth2_scheme)]) -> User:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
payload = decode_access_token(token)
if payload is None or payload.get("sub") is None:
raise credentials_exception
user = session.get(User, int(payload["sub"]))
if user is None or not user.is_active:
raise credentials_exception
return user
CurrentUser = Annotated[User, Depends(get_current_user)]
def require_elevated_superuser(current_user: User, token: str) -> None:
if not current_user.is_superadmin:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Not enough privileges")
payload = decode_access_token(token)
if not payload or not payload.get("elevated"):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="This action requires an elevated session — call POST /auth/elevate first",
)
def get_current_active_superuser(
current_user: CurrentUser,
token: Annotated[str, Depends(oauth2_scheme)],
) -> User:
require_elevated_superuser(current_user, token)
return current_user
CurrentSuperuser = Annotated[User, Depends(get_current_active_superuser)]
def get_route_in_organization(session: Session, route_id: int, organization_id: int) -> Route:
route = session.get(Route, route_id)
if route is None or route.organization_id != organization_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Route not found")
return route
def get_participant_in_organization(session: Session, participant_id: int, organization_id: int) -> Participant:
participant = session.exec(
select(Participant)
.join(Route)
.where(Participant.id == participant_id, Route.organization_id == organization_id)
).first()
if participant is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Participant not found")
return participant
def get_round_in_organization(session: Session, round_id: int, organization_id: int) -> PurchaseRound:
purchase_round = session.exec(
select(PurchaseRound)
.join(Route)
.where(PurchaseRound.id == round_id, Route.organization_id == organization_id)
).first()
if purchase_round is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Purchase round not found")
return purchase_round
def get_wine_offering_in_organization(session: Session, wine_offering_id: int, organization_id: int) -> WineOffering:
wine_offering = session.exec(
select(WineOffering)
.join(PurchaseRound)
.join(Route)
.where(WineOffering.id == wine_offering_id, Route.organization_id == organization_id)
).first()
if wine_offering is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Wine offering not found")
return wine_offering
def get_mail_template_in_organization(session: Session, template_id: int, organization_id: int) -> MailTemplate:
mail_template = session.exec(
select(MailTemplate)
.join(Route)
.where(MailTemplate.id == template_id, Route.organization_id == organization_id)
).first()
if mail_template is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Mail template not found")
return mail_template