Files
tsumugu/backend/app/correspondence.py
T
LarsandCursor d6b70d8f92
Deploy Development / deploy (push) Successful in 28s
Test Suite / pytest-backend (push) Failing after 2m18s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 10s
Schließe die Connector-Kapselung ab, damit Paperless, IMAP und weitere Quellen über dieselbe Anbindung laufen.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-19 15:00:50 +02:00

1464 lines
55 KiB
Python

"""Anlagenverwendung, dokumentierter Versand und gesicherte Fassung.
An attachment use is a role in one container. It does not copy the file and
does not grant access. A dispatch freezes the transmitted stand. Later edits
of the working papers do not rewrite that stand. A missing historical original
stays a gap and is never filled with today's file.
"""
from __future__ import annotations
import json
import re
from dataclasses import dataclass
from datetime import date
from pathlib import Path
from uuid import UUID, uuid4
from psycopg.types.json import Json
from app.access.policy import (
Ctx,
audit,
bump_version,
element_rights,
get_element,
require_case,
require_matter,
)
from app.core.errors import Problem, conflict, not_found, unprocessable
from app.core.timeutil import dump_date, dump_dt
from app.media.port import LEGACY_PROFILE, LocalAdapter, instance_for_write, publish, read_clear_bytes, stored_path
from app.media.storage import display_filename, sha256
from app.retrieval import FilePayload, MailPayload, RetrievalFailure, load_external, load_file
CHANNELS = {"email": "per E-Mail", "post": "per Post", "other": "sonstiger Weg"}
EVIDENCE_ROLES = {
"email": "E-Mail/Versanddokumentation",
"submission": "Einlieferung",
"delivery": "Zustellung",
"other": "sonstiger Nachweis",
}
_HISTORICAL_GAP = (
"Historische Fassung liegt nicht vor. "
"Die aktuelle Arbeitsfassung wurde nicht als Versandstand übernommen."
)
@dataclass(frozen=True)
class GapNote:
reason: str
def open_container(conn, actor: UUID, context: Ctx, placement: Ctx | None, need: str) -> tuple[dict, Ctx, Ctx]:
"""Rights follow the checked context. The relationship lives on the container."""
container = placement or context
if container.kind == "case":
if placement is not None and (context.kind != "case" or context.id != placement.id):
raise not_found()
row, _rights = require_case(conn, actor, container.id, need)
page = Ctx("case", container.id)
return row, page, page
if container.kind != "matter":
raise unprocessable("Der Zusammenhang fehlt.")
if context.kind == "matter" and context.id != container.id:
raise not_found()
rights_context = context if context.kind == "case" else None
row, _rights = require_matter(conn, actor, container.id, rights_context, need)
page = rights_context or Ctx("matter", container.id)
return row, page, Ctx("matter", container.id)
def assign_attachments(
conn,
actor: UUID,
host_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
attachment_ids: list[UUID],
fold: str,
cleanup: list[Path],
) -> dict:
del cleanup
if fold not in {"keep", "attachment"}:
raise unprocessable("Die Darstellung der Vorkommen ist nicht festgelegt.")
if not attachment_ids:
raise unprocessable("Es ist keine Anlage ausgewählt.")
if len(attachment_ids) != len(set(attachment_ids)):
raise unprocessable("Dieselbe Anlage ist mehrfach ausgewählt.")
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
host = _bound_element(conn, actor, host_id, page, scope)
bump_version(conn, "elements", host_id, expected)
created = []
for attachment_id in attachment_ids:
created.append(_assign_one(conn, actor, container_row, page, scope, host, attachment_id, fold))
audit(conn, actor, container_row["home_scope_id"], "attachments_assigned", "element", host_id, page)
return {"attachments": created}
def order_attachments(
conn,
actor: UUID,
host_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
attachment_ids: list[UUID],
) -> dict:
if len(attachment_ids) != len(set(attachment_ids)):
raise unprocessable("Dieselbe Anlage steht mehrfach in der Reihenfolge.")
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
_bound_element(conn, actor, host_id, page, scope)
bump_version(conn, "elements", host_id, expected)
rows = _uses(conn, scope, host_id)
current = [row["attachment_element_id"] for row in rows]
if set(attachment_ids) != set(current) or len(attachment_ids) != len(current):
raise unprocessable("Die Anlagenreihenfolge ist unvollständig. Bitte neu laden.")
for position, attachment_id in enumerate(attachment_ids):
conn.execute(
"""
UPDATE attachment_uses
SET position = %s
WHERE context_kind = %s AND context_id = %s
AND host_element_id = %s AND attachment_element_id = %s
""",
(position, scope.kind, scope.id, host_id, attachment_id),
)
audit(conn, actor, container_row["home_scope_id"], "attachments_ordered", "element", host_id, page)
return {"attachment_element_ids": [str(item) for item in attachment_ids]}
def detach_attachment(
conn,
actor: UUID,
host_id: UUID,
attachment_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
) -> None:
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
_bound_element(conn, actor, host_id, page, scope)
bump_version(conn, "elements", host_id, expected)
deleted = conn.execute(
"""
DELETE FROM attachment_uses
WHERE context_kind = %s AND context_id = %s
AND host_element_id = %s AND attachment_element_id = %s
RETURNING id
""",
(scope.kind, scope.id, host_id, attachment_id),
).fetchone()
if deleted is None:
raise not_found()
_clear_attachment_fold(conn, scope, attachment_id)
audit(conn, actor, container_row["home_scope_id"], "attachment_detached", "element", host_id, page)
def set_fold(
conn,
actor: UUID,
binding_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
fold: str | None,
) -> dict:
if fold not in {None, "attachment", "evidence"}:
raise unprocessable("Diese Darstellung ist nicht vorgesehen.")
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
binding = conn.execute("SELECT * FROM bindings WHERE id = %s", (binding_id,)).fetchone()
if binding is None or binding["context_kind"] != scope.kind or binding["context_id"] != scope.id:
raise not_found()
element = _readable(conn, actor, binding["element_id"], page)
if element is None:
raise not_found()
bump_version(conn, "elements", element["id"], expected)
if fold == "attachment" and not _is_attachment(conn, scope, element["id"]):
raise unprocessable("Nur eine zugeordnete Anlage kann so dargestellt werden.")
if fold == "evidence" and not _is_evidence(conn, scope, element["id"]):
raise unprocessable("Nur ein zugeordneter Nachweis kann so dargestellt werden.")
conn.execute("UPDATE bindings SET list_fold = %s WHERE id = %s", (fold, binding_id))
audit(conn, actor, container_row["home_scope_id"], "binding_folded", "element", element["id"], page)
return {"id": str(binding_id), "fold": fold}
def record_dispatch(
conn,
actor: UUID,
host_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
channel: str,
sent_on: date | None,
counterpart: str | None,
attachment_ids: list[UUID],
evidence: list[dict],
historical_gap: bool,
capture_editions: bool,
media_root: str,
cleanup: list[Path],
) -> dict:
if channel not in CHANNELS:
raise unprocessable("Der Versandweg ist nicht vorgesehen.")
if not capture_editions and attachment_ids:
raise unprocessable("Ein Beleg sichert das Schreiben nicht als Versandfassung.")
if not capture_editions and not evidence:
raise unprocessable("Ein Beleg braucht mindestens einen Nachweis.")
if len(attachment_ids) != len(set(attachment_ids)):
raise unprocessable("Dieselbe Anlage ist mehrfach im Versand genannt.")
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
host = _bound_element(conn, actor, host_id, page, scope)
bump_version(conn, "elements", host_id, expected)
known = {row["attachment_element_id"] for row in _uses(conn, scope, host_id)}
if any(item not in known for item in attachment_ids):
raise unprocessable("Mitversendet werden nur Anlagen dieses Schreibens.")
counterpart_text = _counterpart(counterpart)
dispatch = conn.execute(
"""
INSERT INTO dispatches (
home_scope_id, context_kind, context_id, host_element_id, channel,
sent_on, counterpart, completeness, provenance, recorded_by
)
VALUES (%s, %s, %s, %s, %s, %s, %s, 'gap', %s, %s)
RETURNING *
""",
(
container_row["home_scope_id"],
scope.kind,
scope.id,
host_id,
channel,
sent_on,
counterpart_text,
"reconstructed" if historical_gap else "captured",
actor,
),
).fetchone()
states = []
if capture_editions:
states.append(_capture_item(conn, actor, dispatch, host, "host", 0, page, historical_gap, media_root, cleanup))
ordered = [row for row in _uses(conn, scope, host_id) if row["attachment_element_id"] in set(attachment_ids)]
position_of = {item: index for index, item in enumerate(attachment_ids)}
ordered.sort(key=lambda row: position_of[row["attachment_element_id"]])
for index, row in enumerate(ordered, start=1):
element = _readable(conn, actor, row["attachment_element_id"], page)
if element is None:
raise not_found()
states.append(
_capture_item(conn, actor, dispatch, element, "attachment", index, page, historical_gap, media_root, cleanup)
)
evidence_rows = []
for item in evidence:
evidence_rows.append(
_add_evidence(
conn,
actor,
container_row,
page,
scope,
dispatch,
item["element_id"],
item["role"],
item.get("fold") or "keep",
media_root,
cleanup,
log=False,
)
)
completeness = _completeness(states) if capture_editions else "secured"
conn.execute(
"UPDATE dispatches SET completeness = %s, updated_at = now() WHERE id = %s",
(completeness, dispatch["id"]),
)
audit(conn, actor, container_row["home_scope_id"], "dispatch_recorded", "element", host_id, page)
return dispatch_view(conn, actor, dispatch["id"], page, scope)
def add_dispatch_evidence(
conn,
actor: UUID,
dispatch_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
element_id: UUID,
role: str,
fold: str,
media_root: str,
cleanup: list[Path],
) -> dict:
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
dispatch = _dispatch_in(conn, dispatch_id, scope)
_bump_dispatch(conn, dispatch_id, expected)
row = _add_evidence(
conn, actor, container_row, page, scope, dispatch, element_id, role, fold, media_root, cleanup, log=True
)
return row
def detach_dispatch_evidence(
conn,
actor: UUID,
dispatch_id: UUID,
element_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
) -> None:
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
dispatch = _dispatch_in(conn, dispatch_id, scope)
_bump_dispatch(conn, dispatch_id, expected)
deleted = conn.execute(
"""
DELETE FROM dispatch_evidence
WHERE dispatch_id = %s AND element_id = %s
RETURNING id
""",
(dispatch_id, element_id),
).fetchone()
if deleted is None:
raise not_found()
_clear_evidence_fold(conn, scope, element_id)
leftover_evidence = conn.execute(
"SELECT id FROM dispatch_evidence WHERE dispatch_id = %s", (dispatch_id,)
).fetchone()
leftover_items = conn.execute(
"SELECT id FROM dispatch_items WHERE dispatch_id = %s", (dispatch_id,)
).fetchone()
if leftover_evidence is None and leftover_items is None:
conn.execute("DELETE FROM dispatches WHERE id = %s", (dispatch_id,))
else:
_log(conn, dispatch_id, actor, "Nachweis aus diesem Versand gelöst")
audit(conn, actor, container_row["home_scope_id"], "dispatch_evidence_detached", "element", dispatch["host_element_id"], page)
def correct_dispatch(
conn,
actor: UUID,
dispatch_id: UUID,
expected: int,
context: Ctx,
placement: Ctx | None,
channel: str | None,
sent_on: date | None,
clear_sent_on: bool,
counterpart: str | None,
clear_counterpart: bool,
) -> dict:
container_row, page, scope = open_container(conn, actor, context, placement, "edit")
dispatch = _dispatch_in(conn, dispatch_id, scope)
if channel is None and sent_on is None and not clear_sent_on and counterpart is None and not clear_counterpart:
raise unprocessable("Es fehlt eine Berichtigung.")
if channel is not None and channel not in CHANNELS:
raise unprocessable("Der Versandweg ist nicht vorgesehen.")
_bump_dispatch(conn, dispatch_id, expected)
changes = []
if channel is not None and channel != dispatch["channel"]:
conn.execute("UPDATE dispatches SET channel = %s WHERE id = %s", (channel, dispatch_id))
changes.append(f"Weg: {CHANNELS[channel]}")
if clear_sent_on:
conn.execute("UPDATE dispatches SET sent_on = NULL WHERE id = %s", (dispatch_id,))
changes.append("Versanddatum: undatiert")
elif sent_on is not None and sent_on != dispatch["sent_on"]:
conn.execute("UPDATE dispatches SET sent_on = %s WHERE id = %s", (sent_on, dispatch_id))
changes.append(f"Versanddatum: {sent_on.isoformat()}")
if clear_counterpart:
conn.execute("UPDATE dispatches SET counterpart = NULL WHERE id = %s", (dispatch_id,))
changes.append("Gegenüber: unbekannt")
elif counterpart is not None:
text = _counterpart(counterpart)
conn.execute("UPDATE dispatches SET counterpart = %s WHERE id = %s", (text, dispatch_id))
changes.append("Gegenüber berichtigt")
if not changes:
raise unprocessable("Die Angaben sind unverändert.")
conn.execute("UPDATE dispatches SET updated_at = now() WHERE id = %s", (dispatch_id,))
_log(conn, dispatch_id, actor, "Berichtigung: " + "; ".join(changes))
audit(conn, actor, container_row["home_scope_id"], "dispatch_corrected", "element", dispatch["host_element_id"], page)
return dispatch_view(conn, actor, dispatch_id, page, scope)
def dispatch_view(conn, actor: UUID, dispatch_id: UUID, page: Ctx, scope: Ctx) -> dict:
dispatch = _dispatch_in(conn, dispatch_id, scope)
host = _readable(conn, actor, dispatch["host_element_id"], page)
if host is None:
raise not_found()
return _public_dispatch(conn, actor, dispatch, page)
def read_snapshot(
conn,
actor: UUID,
dispatch_id: UUID,
item_id: UUID,
context: Ctx,
placement: Ctx | None,
media_root: str,
) -> FilePayload | GapNote:
_container, page, scope = open_container(conn, actor, context, placement, "read")
dispatch = _dispatch_in(conn, dispatch_id, scope)
if _readable(conn, actor, dispatch["host_element_id"], page) is None:
raise not_found()
item = conn.execute(
"SELECT * FROM dispatch_items WHERE id = %s AND dispatch_id = %s",
(item_id, dispatch_id),
).fetchone()
if item is None or _readable(conn, actor, item["element_id"], page) is None:
raise not_found()
if item["source_state"] != "snapshot":
return GapNote(item["gap_reason"] or "Für diesen Bestandteil liegt keine gesicherte Fassung vor.")
return _snapshot_file(conn, media_root, dispatch_item_id=item["id"])
def load_snapshot_file(conn, snapshot_id: UUID, media_root: str) -> FilePayload | RetrievalFailure:
row = conn.execute("SELECT * FROM dispatch_snapshots WHERE id = %s", (snapshot_id,)).fetchone()
if row is None:
return RetrievalFailure("missing", "Die gesicherte Fassung ist nicht mehr vorhanden.")
profile = row["protection_profile"] or "legacy-plaintext"
if profile != "legacy-plaintext":
return RetrievalFailure(
"unsupported",
"Dieser Inhalt ist geschützt und kann hier nicht im Klartext gelesen werden.",
)
try:
data = read_clear_bytes(
conn,
instance_id=row["storage_instance_id"],
generation=row["storage_generation"],
object_key=row["object_key"] or f"dispatch/{row['id']}",
media_root=media_root,
protection_profile=profile,
expected_sha256=row["sha256"],
)
except Problem as exc:
if exc.code == "processing":
return RetrievalFailure("missing", "Die gesicherte Fassung lässt sich nicht unverändert lesen.")
return RetrievalFailure("missing", "Die gesicherte Fassung ist nicht mehr vorhanden.")
if sha256(data) != row["sha256"]:
return RetrievalFailure("missing", "Die gesicherte Fassung lässt sich nicht unverändert lesen.")
return FilePayload(row["original_filename"], row["content_type"], data, row["sha256"])
def export_children(conn, actor: UUID, binding: dict, page: Ctx) -> list[dict]:
"""Selectable export nodes for one visible binding. Unreadable parts are omitted."""
element_id = UUID(binding["element"]["id"])
scope = Ctx(binding_context_kind(binding), UUID(binding_context_id(binding)))
nodes = []
host_title = binding["element"]["title"]
for use in _readable_uses(conn, actor, scope, element_id, page):
element = use["element"]
nodes.append(
{
"id": f"attachment:{use['id']}",
"kind": "attachment",
"title": element["title"],
"element": element,
"rights": element["rights"],
"relation": f"Schreiben → Anlage · {host_title}",
"edition": "Aktuelle Arbeitsfassung",
"dates": element.get("dates") or {},
}
)
for dispatch in _dispatch_rows(conn, scope, element_id):
if _readable(conn, actor, dispatch["host_element_id"], page) is None:
continue
children = []
for item in _item_rows(conn, dispatch["id"]):
element = _readable(conn, actor, item["element_id"], page)
if element is None:
continue
rights = element_rights(conn, actor, element, page)
label = "Schreiben → Versand" if item["role"] == "host" else "Schreiben → Anlage → Versand"
sent = dump_date(dispatch["sent_on"]) or "undatiert"
edition = (
f"Gesicherte Versandfassung · Versanddatum {sent}"
if item["source_state"] == "snapshot"
else "Lücke, keine gesicherte historische Fassung"
)
children.append(
{
"id": f"dispatch-item:{item['id']}",
"kind": "dispatch_item",
"title": item["title"],
"element_id": str(item["element_id"]),
"rights": rights.as_dict(),
"relation": label,
"edition": edition,
"gap_reason": item["gap_reason"] if item["source_state"] == "gap" else None,
"snapshot_id": _snapshot_id(conn, dispatch_item_id=item["id"]),
"dates": {"document_on": dump_date(item["document_on"]), "sent_on": sent if dispatch["sent_on"] else None},
"filename": item["filename"],
"content_type": item["content_type"],
}
)
for proof in _evidence_rows(conn, dispatch["id"]):
element = _readable(conn, actor, proof["element_id"], page)
if element is None:
continue
body = _element_public(conn, element, page)
rights = element_rights(conn, actor, element, page)
children.append(
{
"id": f"evidence:{proof['id']}",
"kind": "evidence",
"title": element["title"],
"element": body,
"rights": rights.as_dict(),
"relation": f"Schreiben → Versand → Nachweis · {EVIDENCE_ROLES[proof['role']]}",
"edition": (
"Identifiziertes Nachweisoriginal"
if proof["source_state"] == "identified"
else "Nachweis ohne gesicherte Originalfassung"
),
"gap_reason": proof["gap_reason"] if proof["source_state"] == "gap" else None,
"snapshot_id": _snapshot_id(conn, evidence_id=proof["id"]),
"dates": body.get("dates") or {},
"role": proof["role"],
}
)
if not children:
continue
item_rows = _item_rows(conn, dispatch["id"])
sent = dump_date(dispatch["sent_on"])
if item_rows:
title = f"Versand {sent or 'undatiert'} · {CHANNELS[dispatch['channel']]}"
label = "Versand"
else:
if not sent:
for proof in _evidence_rows(conn, dispatch["id"]):
dates = _dates(conn, proof["element_id"])
sent = dump_date(dates["document_on"]) or dump_date(dates["sent_on"])
if sent:
break
title = sent or "Beleg"
label = "Beleg"
nodes.append(
{
"id": f"dispatch:{dispatch['id']}",
"kind": "dispatch",
"title": title,
"label": label,
"children": children,
"completeness": dispatch["completeness"],
"provenance": dispatch["provenance"],
}
)
return nodes
def attach_correspondence(conn, actor: UUID, bindings: list[dict], page: Ctx) -> None:
if not bindings:
return
scopes: dict[tuple[str, str], list[dict]] = {}
for binding in bindings:
key = (binding_context_kind(binding), binding_context_id(binding))
scopes.setdefault(key, []).append(binding)
for (kind, raw_id), group in scopes.items():
scope = Ctx(kind, UUID(raw_id))
_attach_scope(conn, actor, group, page, scope)
def binding_context_kind(binding: dict) -> str:
container = binding.get("container") or {}
return container.get("kind") or binding.get("_context_kind") or "matter"
def binding_context_id(binding: dict) -> str:
container = binding.get("container") or {}
return container.get("id") or binding.get("_context_id") or ""
def purge_element_dispatch_files(conn, element_id: UUID, media_root: str) -> list[Path]:
"""Drop stored bytes for a deleted element. The manifest of a remaining dispatch shows the gap."""
paths: list[Path] = []
host_dispatches = conn.execute(
"SELECT id FROM dispatches WHERE host_element_id = %s",
(element_id,),
).fetchall()
for dispatch in host_dispatches:
paths.extend(_delete_dispatch_files(conn, dispatch["id"], media_root))
conn.execute("DELETE FROM dispatches WHERE id = %s", (dispatch["id"],))
item_ids = [
row["id"]
for row in conn.execute("SELECT id FROM dispatch_items WHERE element_id = %s", (element_id,)).fetchall()
]
for item_id in item_ids:
snap = conn.execute(
"SELECT id FROM dispatch_snapshots WHERE dispatch_item_id = %s",
(item_id,),
).fetchone()
if snap is not None:
paths.append(_snapshot_disk(conn, snap["id"], media_root))
conn.execute("DELETE FROM dispatch_snapshots WHERE id = %s", (snap["id"],))
conn.execute(
"""
UPDATE dispatch_items
SET source_state = 'gap',
gap_reason = 'Die Fassung wurde mit der Unterlage gelöscht.',
note_document = NULL,
content_sha256 = NULL
WHERE id = %s
""",
(item_id,),
)
evidence_ids = [
row["id"]
for row in conn.execute("SELECT id FROM dispatch_evidence WHERE element_id = %s", (element_id,)).fetchall()
]
for evidence_id in evidence_ids:
snap = conn.execute(
"SELECT id FROM dispatch_snapshots WHERE evidence_id = %s",
(evidence_id,),
).fetchone()
if snap is not None:
paths.append(_snapshot_disk(conn, snap["id"], media_root))
conn.execute("DELETE FROM dispatch_snapshots WHERE id = %s", (snap["id"],))
conn.execute(
"""
UPDATE dispatch_evidence
SET source_state = 'gap', gap_reason = 'Der Nachweis wurde mit der Unterlage gelöscht.',
identity_sha256 = NULL
WHERE id = %s
""",
(evidence_id,),
)
affected = conn.execute(
"""
SELECT DISTINCT dispatch_id FROM dispatch_items
WHERE element_id = %s
""",
(element_id,),
).fetchall()
for row in affected:
states = [
item["source_state"]
for item in conn.execute(
"SELECT source_state FROM dispatch_items WHERE dispatch_id = %s",
(row["dispatch_id"],),
).fetchall()
]
conn.execute(
"UPDATE dispatches SET completeness = %s, updated_at = now() WHERE id = %s",
(_completeness(states), row["dispatch_id"]),
)
return paths
def _assign_one(conn, actor, container_row, page: Ctx, scope: Ctx, host: dict, attachment_id: UUID, fold: str) -> dict:
if attachment_id == host["id"]:
raise unprocessable("Ein Schriftstück kann nicht Anlage seiner selbst sein.")
element = _readable(conn, actor, attachment_id, page)
if element is None:
raise not_found()
existing = conn.execute(
"""
SELECT id FROM attachment_uses
WHERE context_kind = %s AND context_id = %s
AND host_element_id = %s AND attachment_element_id = %s
""",
(scope.kind, scope.id, host["id"], attachment_id),
).fetchone()
if existing is not None:
raise unprocessable("Diese Anlage ist dem Schreiben bereits zugeordnet.")
if _reaches(conn, scope, attachment_id, host["id"]):
raise unprocessable("Diese Zuordnung würde einen Anlagenkreislauf erzeugen.")
position = conn.execute(
"""
SELECT COALESCE(MAX(position) + 1, 0) AS next_position
FROM attachment_uses
WHERE context_kind = %s AND context_id = %s AND host_element_id = %s
""",
(scope.kind, scope.id, host["id"]),
).fetchone()["next_position"]
row = conn.execute(
"""
INSERT INTO attachment_uses (
home_scope_id, context_kind, context_id, host_element_id,
attachment_element_id, position, created_by
)
VALUES (%s, %s, %s, %s, %s, %s, %s)
RETURNING id, position
""",
(container_row["home_scope_id"], scope.kind, scope.id, host["id"], attachment_id, position, actor),
).fetchone()
if fold == "attachment":
_set_binding_fold(conn, scope, attachment_id, "attachment")
return {"id": str(row["id"]), "element_id": str(attachment_id), "position": row["position"]}
def _add_evidence(conn, actor, container_row, page, scope, dispatch, element_id, role, fold, media_root, cleanup, log: bool) -> dict:
if role not in EVIDENCE_ROLES:
raise unprocessable("Die Rolle des Nachweises ist nicht vorgesehen.")
if fold not in {"keep", "evidence"}:
raise unprocessable("Die Darstellung des Nachweises ist nicht festgelegt.")
element = _readable(conn, actor, element_id, page)
if element is None:
raise not_found()
if conn.execute(
"SELECT id FROM dispatch_evidence WHERE dispatch_id = %s AND element_id = %s",
(dispatch["id"], element_id),
).fetchone():
raise unprocessable("Dieser Nachweis ist dem Versand bereits zugeordnet.")
identified, gap_reason, message_id, digest, comparisons, snapshot = _identify_evidence(
conn, actor, dispatch, element, page, media_root, cleanup, container_row["home_scope_id"]
)
proof = conn.execute(
"""
INSERT INTO dispatch_evidence (
dispatch_id, element_id, role, source_state, gap_reason, message_id,
identity_sha256, comparisons, manual, created_by
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, true, %s)
RETURNING *
""",
(
dispatch["id"],
element_id,
role,
"identified" if identified else "gap",
gap_reason,
message_id,
digest,
Json(comparisons),
actor,
),
).fetchone()
if snapshot is not None:
_store_snapshot(
conn,
container_row["home_scope_id"],
actor,
snapshot,
media_root,
cleanup,
evidence_id=proof["id"],
)
if fold == "evidence":
_set_binding_fold(conn, scope, element_id, "evidence")
if log:
_log(conn, dispatch["id"], actor, f"Nachweis ergänzt: {EVIDENCE_ROLES[role]}")
audit(conn, actor, container_row["home_scope_id"], "dispatch_evidence", "element", element_id, page)
return {
"id": str(proof["id"]),
"element_id": str(element_id),
"role": role,
"role_label": EVIDENCE_ROLES[role],
"source_state": proof["source_state"],
"gap_reason": gap_reason,
"manual": True,
"comparisons": comparisons,
}
def _identify_evidence(conn, actor, dispatch, element, page, media_root, cleanup, scope_id):
del cleanup, scope_id
comparisons = []
if element["kind"] == "external_ref":
loaded = load_external(conn, actor, element["id"], None)
if isinstance(loaded, RetrievalFailure):
return False, loaded.message, None, None, [], None
if isinstance(loaded, MailPayload):
comparisons = _compare_mail(conn, actor, dispatch["id"], page, loaded)
payload = FilePayload("nachweis.eml", "message/rfc822", loaded.raw, sha256(loaded.raw))
return True, None, loaded.parsed.message_id or None, payload.sha256, comparisons, payload
payload = FilePayload(loaded.filename, loaded.media_type, loaded.data, sha256(loaded.data))
return True, None, None, payload.sha256, [], payload
if element["kind"] == "file":
loaded = load_file(conn, element["id"], media_root)
if isinstance(loaded, RetrievalFailure):
return False, loaded.message, None, None, [], None
payload = FilePayload(loaded.filename, loaded.media_type, loaded.data, sha256(loaded.data))
return True, None, None, payload.sha256, [], payload
if element["kind"] == "note":
text = _note_bytes(conn, element["id"])
if text is None:
return False, "Der Nachweistext liegt nicht vor.", None, None, [], None
payload = FilePayload(display_filename(element["title"] + ".json"), "application/json", text, sha256(text))
return True, None, None, payload.sha256, [], payload
return False, "Für diesen Nachweis liegt kein sicherbares Original vor.", None, None, [], None
def _compare_mail(conn, actor: UUID, dispatch_id: UUID, page: Ctx, mail: MailPayload) -> list[dict]:
known = []
for item in _item_rows(conn, dispatch_id):
element = _readable(conn, actor, item["element_id"], page)
if element is None or not item["content_sha256"]:
continue
known.append(
{
"element_id": str(item["element_id"]),
"title": item["title"],
"sha256": item["content_sha256"],
"filename": item["filename"] or item["title"],
}
)
return _comparison_rows(mail, known)
def _base_filename(filename: str) -> str:
return re.sub(r" \(\d+\)(\.[^.]+)$", r"\1", filename)
def _comparison_rows(mail: MailPayload, known: list[dict]) -> list[dict]:
rows = []
for attachment in mail.parsed.attachments:
base = _base_filename(attachment.filename)
same_bytes = [item for item in known if item["sha256"] == attachment.sha256]
same_name = [
item
for item in known
if item["sha256"] != attachment.sha256 and (item["filename"] == attachment.filename or item["filename"] == base)
]
if same_bytes:
disposition = "same_bytes"
matches = same_bytes
elif same_name:
disposition = "same_name_different_bytes"
matches = same_name
else:
disposition = "distinct"
matches = []
rows.append(
{
"filename": attachment.filename,
"sha256": attachment.sha256,
"disposition": disposition,
"manual": True,
"matches": [{"element_id": item["element_id"], "title": item["title"]} for item in matches],
}
)
return rows
def _capture_item(conn, actor, dispatch, element, role: str, position: int, page: Ctx, historical_gap: bool, media_root: str, cleanup: list[Path]) -> str:
dates = _dates(conn, element["id"])
title = element["title"]
filename = None
content_type = None
digest = None
byte_size = None
note_document = None
external_source = None
external_id = None
gap_reason = None
payload = None
if historical_gap:
state = "gap"
gap_reason = _HISTORICAL_GAP
else:
payload, gap_reason, note_document, external_source, external_id = _working_bytes(
conn, actor, element, media_root
)
if payload is None:
state = "gap"
else:
state = "snapshot"
filename = payload.filename
content_type = payload.media_type
digest = sha256(payload.data)
byte_size = len(payload.data)
item = conn.execute(
"""
INSERT INTO dispatch_items (
dispatch_id, role, position, element_id, title, document_on, filename,
content_type, byte_size, content_sha256, source_state, gap_reason,
note_document, external_source, external_id
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING id
""",
(
dispatch["id"],
role,
position,
element["id"],
title,
dates["document_on"],
filename,
content_type,
byte_size,
digest,
state,
gap_reason,
Json(note_document) if note_document is not None else None,
external_source,
external_id,
),
).fetchone()
if payload is not None:
_store_snapshot(
conn,
dispatch["home_scope_id"],
actor,
payload,
media_root,
cleanup,
dispatch_item_id=item["id"],
)
del page
return state
def _working_bytes(conn, actor, element, media_root: str):
if element["kind"] == "note":
raw = _note_bytes(conn, element["id"])
if raw is None:
return None, "Der Text des Schreibens liegt nicht vor.", None, None, None
document = json.loads(raw.decode("utf-8"))
payload = FilePayload(display_filename(element["title"] + ".json"), "application/json", raw, sha256(raw))
return payload, None, document, None, None
if element["kind"] == "file":
loaded = load_file(conn, element["id"], media_root)
if isinstance(loaded, RetrievalFailure):
return None, loaded.message, None, None, None
return loaded, None, None, None, None
if element["kind"] == "external_ref":
ref = conn.execute(
"SELECT source, external_id FROM external_references WHERE element_id = %s",
(element["id"],),
).fetchone()
loaded = load_external(conn, actor, element["id"], None)
source = ref["source"] if ref else None
external_id = ref["external_id"] if ref else None
if isinstance(loaded, RetrievalFailure):
return None, loaded.message, None, source, external_id
if isinstance(loaded, MailPayload):
payload = FilePayload("nachricht.eml", "message/rfc822", loaded.raw, sha256(loaded.raw))
return payload, None, None, source, external_id
return loaded, None, None, source, external_id
return None, "Für diesen Inhalt ist keine sicherbare Fassung vorgesehen.", None, None, None
def _store_snapshot(conn, scope_id, actor, payload: FilePayload, media_root: str, cleanup: list[Path], dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> UUID:
snapshot_id = uuid4()
instance = instance_for_write(conn, media_root)
object_key = f"dispatch/{snapshot_id}"
content_revision = str(uuid4())
adapter = LocalAdapter(instance["root_path"])
publish(adapter, object_key, payload.data)
cleanup.append(stored_path(conn, instance["id"], instance["generation"], object_key, media_root))
digest = sha256(payload.data)
conn.execute(
"""
INSERT INTO dispatch_snapshots (
id, dispatch_item_id, evidence_id, home_scope_id, owner_subject_id,
original_filename, content_type, byte_size, sha256,
storage_instance_id, storage_generation, object_key, content_revision,
protection_profile, key_responsibility
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'none')
""",
(
snapshot_id,
dispatch_item_id,
evidence_id,
scope_id,
actor,
display_filename(payload.filename),
payload.media_type or "application/octet-stream",
len(payload.data),
digest,
instance["id"],
instance["generation"],
object_key,
content_revision,
LEGACY_PROFILE,
),
)
return snapshot_id
def _snapshot_file(conn, media_root: str, dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> FilePayload:
if dispatch_item_id is not None:
row = conn.execute(
"SELECT * FROM dispatch_snapshots WHERE dispatch_item_id = %s",
(dispatch_item_id,),
).fetchone()
else:
row = conn.execute(
"SELECT * FROM dispatch_snapshots WHERE evidence_id = %s",
(evidence_id,),
).fetchone()
if row is None:
raise not_found()
profile = row["protection_profile"] or "legacy-plaintext"
data = read_clear_bytes(
conn,
instance_id=row["storage_instance_id"],
generation=row["storage_generation"],
object_key=row["object_key"] or f"dispatch/{row['id']}",
media_root=media_root,
protection_profile=profile,
expected_sha256=row["sha256"],
)
return FilePayload(row["original_filename"], row["content_type"], data, row["sha256"])
def _snapshot_disk(conn, snapshot_id: UUID, media_root: str) -> Path:
row = conn.execute(
"""
SELECT id, storage_instance_id, storage_generation, object_key
FROM dispatch_snapshots WHERE id = %s
""",
(snapshot_id,),
).fetchone()
if row is None:
return stored_path(conn, None, "legacy-local-1", f"dispatch/{snapshot_id}", media_root)
return stored_path(
conn,
row["storage_instance_id"],
row["storage_generation"],
row["object_key"] or f"dispatch/{row['id']}",
media_root,
)
def _snapshot_id(conn, dispatch_item_id: UUID | None = None, evidence_id: UUID | None = None) -> str | None:
if dispatch_item_id is not None:
row = conn.execute(
"SELECT id FROM dispatch_snapshots WHERE dispatch_item_id = %s",
(dispatch_item_id,),
).fetchone()
else:
row = conn.execute(
"SELECT id FROM dispatch_snapshots WHERE evidence_id = %s",
(evidence_id,),
).fetchone()
return None if row is None else str(row["id"])
def _public_dispatch(conn, actor: UUID, dispatch, page: Ctx) -> dict:
items = []
for item in _item_rows(conn, dispatch["id"]):
if _readable(conn, actor, item["element_id"], page) is None:
continue
items.append(
{
"id": str(item["id"]),
"role": item["role"],
"position": item["position"],
"element_id": str(item["element_id"]),
"title": item["title"],
"document_on": dump_date(item["document_on"]),
"filename": item["filename"],
"source_state": item["source_state"],
"gap_reason": item["gap_reason"],
"content_sha256": item["content_sha256"],
}
)
evidence = []
for proof in _evidence_rows(conn, dispatch["id"]):
element = _readable(conn, actor, proof["element_id"], page)
if element is None:
continue
evidence.append(_public_evidence(conn, proof, element, page))
return {
"id": str(dispatch["id"]),
"host_element_id": str(dispatch["host_element_id"]),
"channel": dispatch["channel"],
"channel_label": CHANNELS[dispatch["channel"]],
"sent_on": dump_date(dispatch["sent_on"]),
"undated": dispatch["sent_on"] is None,
"counterpart": dispatch["counterpart"],
"completeness": dispatch["completeness"],
"provenance": dispatch["provenance"],
"recorded_at": dump_dt(dispatch["recorded_at"]),
"version": dispatch["version"],
"items": items,
"evidence": evidence,
"corrections": [
{
"id": str(row["id"]),
"corrected_at": dump_dt(row["corrected_at"]),
"summary": row["summary"],
}
for row in conn.execute(
"""
SELECT id, corrected_at, summary
FROM dispatch_corrections
WHERE dispatch_id = %s
ORDER BY corrected_at, id
""",
(dispatch["id"],),
).fetchall()
],
}
def _evidence_source(conn, element_id: UUID) -> str | None:
row = conn.execute(
"SELECT source FROM external_references WHERE element_id = %s",
(element_id,),
).fetchone()
return row["source"] if row else None
def _public_evidence(conn, proof, element, page: Ctx) -> dict:
del page
return {
"id": str(proof["id"]),
"element_id": str(element["id"]),
"title": element["title"],
"kind": element["kind"],
"source": _evidence_source(conn, element["id"]),
"role": proof["role"],
"role_label": EVIDENCE_ROLES[proof["role"]],
"source_state": proof["source_state"],
"gap_reason": proof["gap_reason"],
"manual": proof["manual"],
"comparisons": proof["comparisons"] or [],
"document_on": dump_date(_dates(conn, element["id"])["document_on"]),
}
def _attach_scope(conn, actor, bindings: list[dict], page: Ctx, scope: Ctx) -> None:
element_ids = [UUID(item["element"]["id"]) for item in bindings]
folds = {
str(row["id"]): row["list_fold"]
for row in conn.execute(
"SELECT id, list_fold FROM bindings WHERE context_kind = %s AND context_id = %s",
(scope.kind, scope.id),
).fetchall()
}
uses = conn.execute(
"""
SELECT * FROM attachment_uses
WHERE context_kind = %s AND context_id = %s
ORDER BY position, id
""",
(scope.kind, scope.id),
).fetchall()
by_host: dict[UUID, list] = {}
hosts_of: dict[UUID, list] = {}
for use in uses:
by_host.setdefault(use["host_element_id"], []).append(use)
hosts_of.setdefault(use["attachment_element_id"], []).append(use)
dispatch_rows = conn.execute(
"""
SELECT * FROM dispatches
WHERE context_kind = %s AND context_id = %s
ORDER BY sent_on NULLS LAST, recorded_at, id
""",
(scope.kind, scope.id),
).fetchall()
by_dispatch_host: dict[UUID, list] = {}
for row in dispatch_rows:
by_dispatch_host.setdefault(row["host_element_id"], []).append(row)
visible = set(element_ids)
for binding in bindings:
element_id = UUID(binding["element"]["id"])
attachments = []
for use in by_host.get(element_id, []):
if use["attachment_element_id"] not in visible and _readable(conn, actor, use["attachment_element_id"], page) is None:
continue
if _readable(conn, actor, use["attachment_element_id"], page) is None:
continue
target = next((item for item in bindings if UUID(item["element"]["id"]) == use["attachment_element_id"]), None)
element = target["element"] if target else _element_public(conn, get_element(conn, use["attachment_element_id"]), page)
attachments.append(
{
"id": str(use["id"]),
"element_id": str(use["attachment_element_id"]),
"binding_id": target["id"] if target else _binding_id(conn, scope, use["attachment_element_id"]),
"title": element["title"],
"kind": element["kind"],
"position": use["position"],
"document_on": (element.get("dates") or {}).get("document_on"),
"label": use["label"],
}
)
hosts = []
for use in hosts_of.get(element_id, []):
if _readable(conn, actor, use["host_element_id"], page) is None:
continue
host_binding = next((item for item in bindings if UUID(item["element"]["id"]) == use["host_element_id"]), None)
host_element = host_binding["element"] if host_binding else _element_public(conn, get_element(conn, use["host_element_id"]), page)
hosts.append({"element_id": str(use["host_element_id"]), "title": host_element["title"]})
dispatches = []
for dispatch in by_dispatch_host.get(element_id, []):
public = _public_dispatch(conn, actor, dispatch, page)
for proof in public["evidence"]:
match = next((item for item in bindings if item["element"]["id"] == proof["element_id"]), None)
proof["listed_fold"] = folds.get(match["id"]) if match else None
dispatches.append(public)
binding["correspondence"] = {
"fold": folds.get(binding["id"]),
"attachments": attachments,
"hosts": hosts,
"dispatches": dispatches,
}
def _element_public(conn, element, page: Ctx) -> dict:
del page
if element is None:
return {"title": "", "kind": "", "dates": {}}
dates = _dates(conn, element["id"])
body = {
"id": str(element["id"]),
"title": element["title"],
"kind": element["kind"],
"dates": {
"document_on": dump_date(dates["document_on"]),
"sent_on": dump_date(dates["sent_on"]),
"received_on": dump_date(dates["received_on"]),
},
}
source = _evidence_source(conn, element["id"])
if source:
body["external"] = {"source": source}
return body
def _readable_uses(conn, actor, scope: Ctx, host_id: UUID, page: Ctx) -> list[dict]:
found = []
for use in _uses(conn, scope, host_id):
element = _readable(conn, actor, use["attachment_element_id"], page)
if element is None:
continue
rights = element_rights(conn, actor, element, page)
body = _element_public(conn, element, page)
body["rights"] = rights.as_dict()
found.append({"id": use["id"], "element": body})
return found
def _bound_element(conn, actor, element_id: UUID, page: Ctx, scope: Ctx) -> dict:
element = _readable(conn, actor, element_id, page)
if element is None:
raise not_found()
binding = conn.execute(
"""
SELECT id FROM bindings
WHERE element_id = %s AND context_kind = %s AND context_id = %s
""",
(element_id, scope.kind, scope.id),
).fetchone()
if binding is None:
raise unprocessable("Das Schriftstück ist in diesem Zusammenhang nicht eingebunden.")
return element
def _readable(conn, actor: UUID, element_id: UUID, page: Ctx):
element = get_element(conn, element_id)
if element is None or not element_rights(conn, actor, element, page).read:
return None
return element
def _uses(conn, scope: Ctx, host_id: UUID):
return conn.execute(
"""
SELECT * FROM attachment_uses
WHERE context_kind = %s AND context_id = %s AND host_element_id = %s
ORDER BY position, id
""",
(scope.kind, scope.id, host_id),
).fetchall()
def _dispatch_rows(conn, scope: Ctx, host_id: UUID):
return conn.execute(
"""
SELECT * FROM dispatches
WHERE context_kind = %s AND context_id = %s AND host_element_id = %s
ORDER BY sent_on NULLS LAST, recorded_at, id
""",
(scope.kind, scope.id, host_id),
).fetchall()
def _item_rows(conn, dispatch_id: UUID):
return conn.execute(
"SELECT * FROM dispatch_items WHERE dispatch_id = %s ORDER BY position, id",
(dispatch_id,),
).fetchall()
def _evidence_rows(conn, dispatch_id: UUID):
return conn.execute(
"SELECT * FROM dispatch_evidence WHERE dispatch_id = %s ORDER BY created_at, id",
(dispatch_id,),
).fetchall()
def _dispatch_in(conn, dispatch_id: UUID, scope: Ctx):
row = conn.execute("SELECT * FROM dispatches WHERE id = %s", (dispatch_id,)).fetchone()
if row is None or row["context_kind"] != scope.kind or row["context_id"] != scope.id:
raise not_found()
return row
def _bump_dispatch(conn, dispatch_id: UUID, expected: int) -> int:
row = conn.execute(
"""
UPDATE dispatches
SET version = version + 1, updated_at = now()
WHERE id = %s AND version = %s
RETURNING version
""",
(dispatch_id, expected),
).fetchone()
if row is None:
current = conn.execute("SELECT id FROM dispatches WHERE id = %s", (dispatch_id,)).fetchone()
if current is None:
raise not_found()
raise conflict()
return int(row["version"])
def _reaches(conn, scope: Ctx, start: UUID, target: UUID) -> bool:
seen = set()
stack = [start]
while stack:
current = stack.pop()
if current == target:
return True
if current in seen:
continue
seen.add(current)
rows = conn.execute(
"""
SELECT attachment_element_id FROM attachment_uses
WHERE context_kind = %s AND context_id = %s AND host_element_id = %s
""",
(scope.kind, scope.id, current),
).fetchall()
stack.extend(row["attachment_element_id"] for row in rows)
return False
def _is_attachment(conn, scope: Ctx, element_id: UUID) -> bool:
return (
conn.execute(
"""
SELECT id FROM attachment_uses
WHERE context_kind = %s AND context_id = %s AND attachment_element_id = %s
""",
(scope.kind, scope.id, element_id),
).fetchone()
is not None
)
def _is_evidence(conn, scope: Ctx, element_id: UUID) -> bool:
return (
conn.execute(
"""
SELECT e.id
FROM dispatch_evidence e
JOIN dispatches d ON d.id = e.dispatch_id
WHERE d.context_kind = %s AND d.context_id = %s AND e.element_id = %s
""",
(scope.kind, scope.id, element_id),
).fetchone()
is not None
)
def _set_binding_fold(conn, scope: Ctx, element_id: UUID, fold: str) -> None:
conn.execute(
"""
UPDATE bindings SET list_fold = %s
WHERE element_id = %s AND context_kind = %s AND context_id = %s
""",
(fold, element_id, scope.kind, scope.id),
)
def _clear_evidence_fold(conn, scope: Ctx, element_id: UUID) -> None:
if _is_evidence(conn, scope, element_id):
return
conn.execute(
"""
UPDATE bindings SET list_fold = NULL
WHERE element_id = %s AND context_kind = %s AND context_id = %s AND list_fold = 'evidence'
""",
(element_id, scope.kind, scope.id),
)
def _clear_attachment_fold(conn, scope: Ctx, element_id: UUID) -> None:
if _is_attachment(conn, scope, element_id):
return
conn.execute(
"""
UPDATE bindings SET list_fold = NULL
WHERE element_id = %s AND context_kind = %s AND context_id = %s AND list_fold = 'attachment'
""",
(element_id, scope.kind, scope.id),
)
def _binding_id(conn, scope: Ctx, element_id: UUID) -> str | None:
row = conn.execute(
"""
SELECT id FROM bindings
WHERE element_id = %s AND context_kind = %s AND context_id = %s
""",
(element_id, scope.kind, scope.id),
).fetchone()
return None if row is None else str(row["id"])
def _dates(conn, element_id: UUID) -> dict:
row = conn.execute("SELECT document_on, sent_on, received_on FROM correspondence_dates WHERE element_id = %s", (element_id,)).fetchone()
if row is None:
return {"document_on": None, "sent_on": None, "received_on": None}
return row
def _note_bytes(conn, element_id: UUID) -> bytes | None:
row = conn.execute("SELECT document_json FROM note_bodies WHERE element_id = %s", (element_id,)).fetchone()
if row is None:
return None
return json.dumps(row["document_json"], ensure_ascii=False, sort_keys=True).encode("utf-8")
def _counterpart(value: str | None) -> str | None:
if value is None:
return None
text = value.strip()
if not text:
return None
if len(text) > 200:
raise unprocessable("Das Gegenüber ist zu lang.")
return text
def _completeness(states: list[str]) -> str:
if states and all(item == "snapshot" for item in states):
return "secured"
if any(item == "snapshot" for item in states):
return "partial"
return "gap"
def _log(conn, dispatch_id: UUID, actor: UUID, summary: str) -> None:
conn.execute(
"""
INSERT INTO dispatch_corrections (dispatch_id, corrected_by, summary)
VALUES (%s, %s, %s)
""",
(dispatch_id, actor, summary[:500]),
)
def _delete_dispatch_files(conn, dispatch_id: UUID, media_root: str) -> list[Path]:
paths = []
rows = conn.execute(
"""
SELECT s.id
FROM dispatch_snapshots s
LEFT JOIN dispatch_items i ON i.id = s.dispatch_item_id
LEFT JOIN dispatch_evidence e ON e.id = s.evidence_id
WHERE i.dispatch_id = %s OR e.dispatch_id = %s
""",
(dispatch_id, dispatch_id),
).fetchall()
for row in rows:
paths.append(_snapshot_disk(conn, row["id"], media_root))
return paths