Files
plm-lottery/apps/lottery/audit.py
T
davideandClaude Sonnet 5.5 0eba7bcfe6 A2: lottery models and database constraints (WEB-04, ADR-10, ADR-15, ADR-22, DAT-76..79, AUD-47)
LotteryConfig (single row, parameters and the three pause switches), Round
(single active round, own rules, split within the pool), Participation (one
bet per player and round, unique draw position), Movement (signed ledger,
idempotency key per player), ChainTransaction and an append-only AuditEntry
enforced by the model and by SQLite triggers. Every foreign key is PROTECT.
One test per constraint, each checked to hit the intended constraint.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 12:13:10 +01:00

36 lines
997 B
Python

"""The one place audit rows are written (AUD-01, AUD-47).
Call it inside the same database transaction as the change it records (ADR-53), so a change
and its trace commit or roll back together.
"""
from typing import Any
from .models import AuditEntry
def record(
action: str,
*,
actor=None,
actor_label: str | None = None,
reason: str = "",
outcome: str = AuditEntry.Outcome.OK,
subject_type: str = "",
subject_id: object = "",
payload: dict[str, Any] | None = None,
) -> AuditEntry:
"""Append one audit row. `actor` is the user who acted, or None for the system/worker."""
if actor_label is None:
actor_label = actor.get_username() if actor is not None else "system"
return AuditEntry.objects.create(
actor=actor,
actor_label=actor_label,
action=action,
reason=reason,
outcome=outcome,
subject_type=subject_type,
subject_id=str(subject_id),
payload=payload or {},
)