228 lines
7.7 KiB
Python
228 lines
7.7 KiB
Python
"""Synthetic rental case for the ephemeral test database. Never persists on dev or prod."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
|
|
from app.access.policy import Ctx
|
|
from app.auth.accounts import create_account
|
|
from app.core.errors import not_found
|
|
from app.core.settings import Settings, get_settings
|
|
from app.ops import (
|
|
add_annotation,
|
|
assign_task,
|
|
bind_element,
|
|
create_case,
|
|
create_chapter,
|
|
create_deadline,
|
|
create_external,
|
|
create_file,
|
|
create_matter,
|
|
create_note,
|
|
create_task,
|
|
grant_access,
|
|
link_matter_to_case,
|
|
link_task_deadline,
|
|
)
|
|
|
|
ADA_EMAIL = "ada@example.test"
|
|
BEN_EMAIL = "ben@example.test"
|
|
ADA_PASSWORD = "Beispiel-Ada-2026"
|
|
BEN_PASSWORD = "Beispiel-Ben-2026"
|
|
|
|
SAMPLE_PDF = b"""%PDF-1.1
|
|
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
|
|
2 0 obj<</Type/Pages/Count 1/Kids[3 0 R]>>endobj
|
|
3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 320 160]/Contents 4 0 R>>endobj
|
|
4 0 obj<</Length 55>>stream
|
|
BT /F1 12 Tf 40 90 Td (Mietschreiben Beispiel) Tj ET
|
|
endstream
|
|
endobj
|
|
trailer<</Root 1 0 R>>
|
|
%%EOF
|
|
"""
|
|
|
|
|
|
_LIVE_DATABASES = frozenset({"tsumugu", "tsumugu_dev", "postgres", "template0", "template1"})
|
|
|
|
|
|
def is_test_database(name: str) -> bool:
|
|
lowered = name.strip().lower()
|
|
return bool(lowered) and lowered.endswith("_test") and lowered not in _LIVE_DATABASES
|
|
|
|
|
|
def demo_allowed(settings: Settings) -> bool:
|
|
return (
|
|
settings.tsumugu_env == "development"
|
|
and is_test_database(settings.db_name)
|
|
and settings.seed_demo
|
|
)
|
|
|
|
|
|
def demo_public(settings: Settings) -> dict:
|
|
if not demo_allowed(settings):
|
|
raise not_found()
|
|
return {
|
|
"accounts": [
|
|
{"email": ADA_EMAIL, "password": ADA_PASSWORD, "display_name": "Ada Beispiel"},
|
|
{"email": BEN_EMAIL, "password": BEN_PASSWORD, "display_name": "Ben Beispiel"},
|
|
]
|
|
}
|
|
|
|
|
|
def seed_demo(settings: Settings | None = None) -> bool:
|
|
settings = settings or get_settings()
|
|
if not demo_allowed(settings):
|
|
return False
|
|
cleanup: list[Path] = []
|
|
with psycopg.connect(settings.dsn(), row_factory=dict_row) as conn:
|
|
try:
|
|
if conn.execute("SELECT 1 FROM auth_accounts WHERE email = %s", (ADA_EMAIL,)).fetchone():
|
|
return False
|
|
ada = create_account(conn, ADA_EMAIL, "Ada Beispiel", ADA_PASSWORD, origin="fixture")
|
|
ben = create_account(conn, BEN_EMAIL, "Ben Beispiel", BEN_PASSWORD, origin="fixture")
|
|
room = conn.execute(
|
|
"""
|
|
INSERT INTO data_rooms (id, kind, name, created_by)
|
|
VALUES (gen_random_uuid(), 'organization', 'Haushalt Musterstraße', %s)
|
|
RETURNING id
|
|
""",
|
|
(ada["id"],),
|
|
).fetchone()
|
|
conn.execute(
|
|
"INSERT INTO room_memberships (room_id, subject_id, role) VALUES (%s, %s, 'manager'), (%s, %s, 'member')",
|
|
(room["id"], ada["id"], room["id"], ben["id"]),
|
|
)
|
|
private_case = create_case(conn, ada["id"], ada["id"], "Persönliche Verhandlungsstrategie", None, "explicit")
|
|
shared_case = create_case(conn, ada["id"], ada["id"], "Mietstreit Musterstraße 12", None, "explicit")
|
|
grant_access(conn, ada["id"], "case", shared_case["id"], ben["id"], True, True, False, None)
|
|
strategy = create_matter(conn, ada["id"], ada["id"], "Interne Verhandlungsstrategie", None, None)
|
|
moisture = create_matter(conn, ada["id"], ada["id"], "Mängelanzeige Feuchtigkeit", None, None)
|
|
link_matter_to_case(conn, ada["id"], moisture["id"], shared_case["id"], None)
|
|
link_matter_to_case(conn, ada["id"], strategy["id"], private_case["id"], None)
|
|
note = create_note(
|
|
conn,
|
|
ada["id"],
|
|
ada["id"],
|
|
"Gespräch mit der Hausverwaltung",
|
|
{
|
|
"version": 1,
|
|
"blocks": [
|
|
{
|
|
"type": "paragraph",
|
|
"text": (
|
|
"Am 1. Oktober die Feuchtigkeit im Schlafzimmer angezeigt. "
|
|
"Die Verwaltung will bis Ende November antworten."
|
|
),
|
|
}
|
|
],
|
|
},
|
|
Ctx("case", shared_case["id"]),
|
|
None,
|
|
None,
|
|
)
|
|
matter_binding = bind_element(
|
|
conn,
|
|
ada["id"],
|
|
note["element"]["id"],
|
|
Ctx("case", shared_case["id"]),
|
|
Ctx("matter", moisture["id"]),
|
|
None,
|
|
None,
|
|
)
|
|
add_annotation(
|
|
conn,
|
|
ada["id"],
|
|
note["binding"]["id"],
|
|
"Nur in der Akte: Original im blauen Ordner.",
|
|
Ctx("case", shared_case["id"]),
|
|
)
|
|
add_annotation(
|
|
conn,
|
|
ada["id"],
|
|
matter_binding["id"],
|
|
"Nur in diesem Vorgang: Zeugin Frau Keller erinnern.",
|
|
Ctx("matter", moisture["id"]),
|
|
)
|
|
create_note(
|
|
conn,
|
|
ada["id"],
|
|
ada["id"],
|
|
"Nicht weitergeben",
|
|
{
|
|
"version": 1,
|
|
"blocks": [
|
|
{
|
|
"type": "paragraph",
|
|
"text": "Vergleich nur anbieten, wenn die Gegenseite zuerst einen Betrag nennt.",
|
|
}
|
|
],
|
|
},
|
|
Ctx("case", private_case["id"]),
|
|
None,
|
|
None,
|
|
)
|
|
chapter = create_chapter(conn, ada["id"], "matter", moisture["id"], "Schriftwechsel", 0, None)
|
|
create_file(
|
|
conn,
|
|
ada["id"],
|
|
ada["id"],
|
|
"Schreiben an die Hausverwaltung",
|
|
"Schreiben-Hausverwaltung.pdf",
|
|
SAMPLE_PDF,
|
|
Ctx("matter", moisture["id"]),
|
|
None,
|
|
chapter["id"],
|
|
date(2026, 10, 1),
|
|
date(2026, 10, 2),
|
|
None,
|
|
settings.media_root,
|
|
settings.upload_max_bytes,
|
|
cleanup,
|
|
)
|
|
create_external(
|
|
conn,
|
|
ada["id"],
|
|
ada["id"],
|
|
"Antwort der Hausverwaltung",
|
|
"paperless",
|
|
"4821",
|
|
Ctx("matter", moisture["id"]),
|
|
None,
|
|
chapter["id"],
|
|
date(2026, 10, 8),
|
|
None,
|
|
date(2026, 10, 10),
|
|
)
|
|
task = create_task(
|
|
conn,
|
|
ada["id"],
|
|
ada["id"],
|
|
"Unterlagen für die Antwort zusammenstellen",
|
|
date(2026, 11, 25),
|
|
moisture["id"],
|
|
None,
|
|
)
|
|
assign_task(conn, ada["id"], task["id"], ben["id"], None)
|
|
deadline = create_deadline(
|
|
conn,
|
|
ada["id"],
|
|
ada["id"],
|
|
"Antwort an die Gegenseite",
|
|
date(2026, 11, 30),
|
|
moisture["id"],
|
|
None,
|
|
)
|
|
link_task_deadline(conn, ada["id"], task["id"], deadline["id"], None)
|
|
conn.commit()
|
|
return True
|
|
except Exception:
|
|
conn.rollback()
|
|
for path in cleanup:
|
|
path.unlink(missing_ok=True)
|
|
raise
|