"""Hierarchical export of one matter into a self-contained handover package.""" from __future__ import annotations import html import io import zipfile from dataclasses import dataclass, field from datetime import datetime, timezone from pathlib import PurePosixPath from urllib.parse import quote from uuid import UUID from app.access.policy import ( Ctx, audit, element_rights, get_element, get_matter, matter_rights, ) from app.core.errors import not_found, unprocessable from app.core.timeutil import dump_dt, utcnow from app.correspondence import GapNote, export_children, load_snapshot_file from app.mailpack import parse_mail from app.present import _chapters, _ordered_bindings from app.retrieval import ( FilePayload, MailPayload, RetrievalFailure, load_external, load_file, ) _NODE_KINDS = {"binding", "annotation", "attachment", "dispatch_item", "evidence"} _DIRECTION = {"in": "Eingang", "out": "Ausgang"} _KIND_LABEL = { "note": "Notiz", "file": "Datei", "email": "E-Mail", "paperless": "Paperless", "source": "Quelle", "url": "Webadresse", "annotation": "Anmerkung", "chapter": "Kapitel", "matter": "Teilvorgang", "root": "Vorgang", } @dataclass class ExportNode: id: str kind: str title: str exportable: bool reason: str | None content_kind: str | None children: list[ExportNode] = field(default_factory=list) element_id: str | None = None summary: str | None = None direction: str | None = None dates: dict | None = None annotation_body: str | None = None annotation_author: str | None = None annotation_at: str | None = None label: str = "" relation: str | None = None edition: str | None = None gap_reason: str | None = None snapshot_id: str | None = None def public(self) -> dict: return { "id": self.id, "kind": self.kind, "title": self.title, "label": self.label or _KIND_LABEL.get(self.kind, ""), "exportable": self.exportable, "reason": self.reason, "content_kind": self.content_kind, "children": [child.public() for child in self.children], } @dataclass class Issue: node_id: str title: str code: str message: str choices: list[dict] = field(default_factory=list) def public(self) -> dict: body = { "node_id": self.node_id, "title": self.title, "code": self.code, "message": self.message, } if self.choices: body["choices"] = self.choices return body @dataclass class Notice: node_id: str message: str def public(self) -> dict: return {"node_id": self.node_id, "message": self.message} @dataclass class Entry: node_id: str title: str kind_label: str path: str | None links: list[tuple[str, str]] dates: dict direction: str | None summary: str | None notice: str | None replies: list[tuple[str, str]] recorded_at: str | None notes: tuple[str, ...] = () def public(self) -> dict: return { "node_id": self.node_id, "title": self.title, "kind_label": self.kind_label, "path": self.path, "links": [{"label": label, "path": path} for label, path in self.links], } @dataclass class OverviewNode: kind: str title: str label: str | None = None entry: Entry | None = None children: list[OverviewNode] = field(default_factory=list) @dataclass class ExportPlan: matter_title: str generated_at: str ready: bool issues: list[Issue] notices: list[Notice] entries: list[Entry] files: list[tuple[str, bytes]] tree: ExportNode def review(self) -> dict: return { "ready": self.ready, "matter_title": self.matter_title, "issues": [item.public() for item in self.issues], "notices": [item.public() for item in self.notices], "entries": [item.public() for item in self.entries], } def export_tree(conn, actor: UUID, matter_id: UUID, context: Ctx | None) -> dict: matter, page = _require_matter(conn, actor, matter_id, context) root = _matter_node(conn, actor, matter, page, root=True) return {"matter_id": str(matter["id"]), "context": None if context is None else context.as_dict(), "tree": root.public()} def review_export( conn, actor: UUID, matter_id: UUID, context: Ctx | None, selected: list[str], assignments: dict[str, UUID], media_root: str, ) -> ExportPlan: return _plan(conn, actor, matter_id, context, selected, assignments, media_root) def build_export( conn, actor: UUID, matter_id: UUID, context: Ctx | None, selected: list[str], assignments: dict[str, UUID], media_root: str, ) -> tuple[str, bytes]: plan = _plan(conn, actor, matter_id, context, selected, assignments, media_root) if not plan.ready: detail = " ".join(f"{item.title}: {item.message}" for item in plan.issues[:8]) raise unprocessable(detail or "Das Paket ist nicht vollständig.") matter = get_matter(conn, matter_id) audit(conn, actor, matter["home_scope_id"], "export_created", "matter", matter_id, context) return _zip_name(plan.matter_title), _zip_bytes(plan.files) def _plan(conn, actor, matter_id, context, selected, assignments, media_root) -> ExportPlan: matter, page = _require_matter(conn, actor, matter_id, context) root = _matter_node(conn, actor, matter, page, root=True) chosen = _chosen(root, selected) leaves = [node for node in _walk(root) if node.id in chosen and node.kind in _NODE_KINDS] issues: list[Issue] = [] notices: list[Notice] = [] loaded: dict[str, FilePayload | MailPayload | str] = {} hashes = _local_hashes(conn, root, chosen) for node in leaves: if not node.exportable: issues.append(Issue(node.id, node.title, "rights", node.reason or "Für die Übergabe fehlt die Freigabebefugnis.")) continue if node.kind == "annotation": loaded[node.id] = node.annotation_body or "" continue if node.gap_reason and node.kind in {"dispatch_item", "evidence"}: loaded[node.id] = GapNote(node.gap_reason) continue if node.snapshot_id and node.kind in {"dispatch_item", "evidence"}: result = load_snapshot_file(conn, UUID(node.snapshot_id), media_root) if isinstance(result, RetrievalFailure): issues.append(Issue(node.id, node.title, result.code, result.message, [dict(item) for item in result.choices])) continue loaded[node.id] = result continue if node.content_kind == "note": loaded[node.id] = _note_text(conn, UUID(node.element_id)) continue if node.content_kind == "file": result = load_file(conn, UUID(node.element_id), media_root) elif node.content_kind in {"email", "paperless", "url", "source"}: result = load_external(conn, actor, UUID(node.element_id), assignments.get(node.id)) else: result = RetrievalFailure("capability", "Für diesen Inhalt ist kein Originalabruf vorgesehen.") if isinstance(result, RetrievalFailure): issues.append(Issue(node.id, node.title, result.code, result.message, [dict(item) for item in result.choices])) continue loaded[node.id] = result if isinstance(result, MailPayload): notices.extend(_retention_notices(node, result, hashes)) notices.extend(_bundle_notices(root, chosen)) notices.extend(_correspondence_export_notices(root, chosen)) issues.extend(_reference_issues(conn, actor, page, leaves)) issues.extend(_attachment_load_issues(root, chosen, loaded)) generated = dump_dt(utcnow()) or "" if issues: entries = [ Entry(node.id, node.title, _label(node), None, [], node.dates or {}, node.direction, node.summary, None, [], _recorded(node)) for node in leaves ] return ExportPlan(matter["title"], generated, False, issues, notices, entries, [], root) files, entries = _layout(root, chosen, loaded, notices, generated) emit_issues = _attachment_emit_issues(root, chosen, loaded, entries) if emit_issues: return ExportPlan( matter["title"], generated, False, emit_issues, notices, [ Entry(node.id, node.title, _label(node), None, [], node.dates or {}, node.direction, node.summary, None, [], _recorded(node)) for node in leaves ], [], root, ) _attach_replies(conn, actor, page, entries, leaves) _stitch_replies(files, entries) files = _with_overview(matter["title"], generated, root, entries, files) return ExportPlan(matter["title"], generated, True, [], notices, entries, files, root) def _require_matter(conn, actor: UUID, matter_id: UUID, context: Ctx | None): matter = get_matter(conn, matter_id) if matter is None: raise not_found() # A matter context that names the matter itself is the direct view, as with notes. access = None if context is not None and context.kind == "matter" and context.id == matter["id"] else context if not matter_rights(conn, actor, matter, access).read: raise not_found() return matter, context or Ctx("matter", matter_id) def _matter_node(conn, actor, matter, page: Ctx, root: bool) -> ExportNode: mode = matter.get("content_order") or "position" direction = matter.get("content_order_dir") or "asc" chapters = _chapters(conn, "matter", matter["id"], mode, direction) bindings = _ordered_bindings(conn, actor, "matter", matter["id"], page, mode, direction) grouped: dict[str | None, list] = {} for binding in bindings: grouped.setdefault(binding.get("chapter_id"), []).append(binding) children: list[ExportNode] = [] children.extend(_chapter_nodes(conn, actor, page, chapters, grouped)) for binding in grouped.get(None, []): if not _stands_in_chapter(binding): continue children.append(_binding_node(conn, actor, binding, page)) rows = conn.execute( "SELECT * FROM matters WHERE parent_matter_id = %s AND deleted_at IS NULL ORDER BY title, id", (matter["id"],), ).fetchall() for child in rows: if not matter_rights(conn, actor, child, page).read: continue children.append(_matter_node(conn, actor, child, page, root=False)) return ExportNode( id=f"matter:{matter['id']}", kind="matter", title=matter["title"], exportable=True, reason=None, content_kind=None, children=children, label="Vorgang" if root else "Teilvorgang", ) def _stands_in_chapter(binding: dict) -> bool: """A folded attachment or receipt stays under its letter, not as another chapter row.""" pack = binding.get("correspondence") or {} if pack.get("fold") in {"attachment", "evidence"}: return False if pack.get("hosts"): return False return True def _chapter_nodes(conn, actor, page, chapters: list[dict], grouped: dict[str | None, list]) -> list[ExportNode]: by_parent: dict[str | None, list[dict]] = {} for chapter in chapters: by_parent.setdefault(chapter.get("parent_chapter_id"), []).append(chapter) def build(parent_id: str | None, path: str) -> list[ExportNode]: nodes: list[ExportNode] = [] for chapter in by_parent.get(parent_id, []): chapter_path = f"{path} / {chapter['title']}" if path else chapter["title"] nested = build(chapter["id"], chapter_path) items = [] for item in grouped.get(chapter["id"], []): if not _stands_in_chapter(item): continue node = _binding_node(conn, actor, item, page) node.relation = f"Kapitel: {chapter_path}" items.append(node) if not items and not nested: continue nodes.append( ExportNode( id=f"chapter:{chapter['id']}", kind="chapter", title=chapter["title"], exportable=True, reason=None, content_kind=None, children=[*items, *nested], label="Unterkapitel" if parent_id else "Kapitel", ) ) return nodes return build(None, "") def _binding_node(conn, actor, binding: dict, page) -> ExportNode: element = binding["element"] rights = binding["rights"] content_kind = _content_kind(element) exportable = bool(rights.get("read") and rights.get("share")) reason = None if exportable else "Für die Übergabe fehlt die Freigabebefugnis." dates = element.get("dates") or {} node = ExportNode( id=f"binding:{binding['id']}", kind="binding", title=element["title"], exportable=exportable, reason=reason, content_kind=content_kind, element_id=element["id"], summary=element.get("summary"), direction=(binding.get("communication") or {}).get("direction"), dates=dates, label=_KIND_LABEL.get(content_kind, "Inhalt"), ) for note in binding.get("annotations") or []: node.children.append( ExportNode( id=f"annotation:{note['id']}", kind="annotation", title=f"Anmerkung zu {element['title']}", exportable=exportable, reason=reason, content_kind="annotation", element_id=element["id"], annotation_body=note.get("body") or "", annotation_author=note.get("author_display_name") or "", annotation_at=note.get("created_at"), label="Anmerkung", ) ) for spec in export_children(conn, actor, binding, page): node.children.append(_spec_node(spec)) return node def _spec_node(spec: dict) -> ExportNode: if spec["kind"] == "dispatch": return ExportNode( id=spec["id"], kind="dispatch", title=spec["title"], exportable=True, reason=None, content_kind=None, children=[_spec_node(child) for child in spec["children"]], label=spec.get("label") or "Versand", ) element = spec.get("element") if spec["kind"] == "dispatch_item": content_kind = _item_content_kind(spec) else: content_kind = _content_kind(element) if element else "file" rights = spec.get("rights") or {} exportable = bool(rights.get("read") and rights.get("share")) return ExportNode( id=spec["id"], kind=spec["kind"], title=spec["title"], exportable=exportable, reason=None if exportable else "Für die Übergabe fehlt die Freigabebefugnis.", content_kind=content_kind, element_id=(element or {}).get("id") or spec.get("element_id"), dates=spec.get("dates"), label={"attachment": "Anlage", "dispatch_item": "Versandfassung", "evidence": "Nachweis"}.get(spec["kind"], "Inhalt"), relation=spec.get("relation"), edition=spec.get("edition"), gap_reason=spec.get("gap_reason"), snapshot_id=spec.get("snapshot_id"), ) def _item_content_kind(spec: dict) -> str: if spec.get("gap_reason"): return "gap" media = spec.get("content_type") or "" if media == "message/rfc822": return "email" if media == "application/json": return "note" return "file" def _content_kind(element: dict) -> str: if element["kind"] == "note": return "note" if element["kind"] == "file": return "file" source = (element.get("external") or {}).get("source") if source == "url": return "url" from app.sourceregistry import get_source module = get_source(source) if source else None if module is None: return "source" if module.representation == "email": return "email" return "source" def _chosen(root: ExportNode, selected: list[str]) -> set[str]: known = {node.id for node in _walk(root) if node.kind in _NODE_KINDS} unknown = [item for item in selected if item not in known] if unknown: raise unprocessable("Die Auswahl enthält einen unbekannten oder nicht sichtbaren Inhalt.") if not selected: raise unprocessable("Die Auswahl enthält keinen Inhalt.") return set(selected) def _walk(node: ExportNode): yield node for child in node.children: yield from _walk(child) def _note_text(conn, element_id: UUID) -> str: row = conn.execute("SELECT document_json FROM note_bodies WHERE element_id = %s", (element_id,)).fetchone() document = row["document_json"] if row else {} parts = [] for block in document.get("blocks") or []: if isinstance(block, dict): text = str(block.get("text") or "") if text.strip(): parts.append(text) return "\n\n".join(parts) def _local_hashes(conn, root: ExportNode, chosen: set[str]) -> list[tuple[str, str, bool]]: found = [] for node in _walk(root): if node.kind != "binding" or node.content_kind != "file" or not node.element_id: continue asset = conn.execute( "SELECT sha256, status FROM media_assets WHERE element_id = %s", (UUID(node.element_id),), ).fetchone() if asset is None or asset["status"] != "active" or not asset["sha256"]: continue found.append((node.id, asset["sha256"], node.id in chosen)) return found def _retention_notices(node: ExportNode, mail: MailPayload, hashes: list[tuple[str, str, bool]]) -> list[Notice]: notices = [] seen: set[str] = set() for attachment in mail.parsed.attachments: if any(digest == attachment.sha256 and not selected for _node_id, digest, selected in hashes): if attachment.sha256 in seen: continue seen.add(attachment.sha256) notices.append( Notice( node.id, f"Die Anlage „{attachment.filename}“ bleibt Bestandteil dieser E-Mail. " "Ein separates Vorkommen derselben Datei ist nicht ausgewählt.", ) ) return notices def _reference_issues(conn, actor, page: Ctx, leaves: list[ExportNode]) -> list[Issue]: issues = [] selected_elements = {node.element_id for node in leaves if node.kind == "binding" and node.element_id} for node in leaves: if node.kind != "binding" or not node.element_id: continue rows = conn.execute( """ SELECT refers_to_element_id FROM element_references WHERE element_id = %s ORDER BY created_at, refers_to_element_id """, (UUID(node.element_id),), ).fetchall() for row in rows: target = get_element(conn, row["refers_to_element_id"]) if target is None or not element_rights(conn, actor, target, page).read: issues.append(Issue(node.id, node.title, "reference", "Ein Antwortbezug lässt sich nicht auflösen.")) continue if str(target["id"]) not in selected_elements and str(target["id"]) != node.element_id: continue return issues def _layout(root, chosen, loaded, notices, generated: str): used: set[str] = {"index.html", "inhalt.md"} files: list[tuple[str, bytes]] = [] entries: list[Entry] = [] notice_for = {} for notice in notices: notice_for.setdefault(notice.node_id, []).append(notice.message) def add(parts: list[str], data: bytes) -> str: path = _unique_path(parts, used) files.append((path, data)) return path def rewrite(path: str, data: bytes) -> None: for index, (existing, _data) in enumerate(files): if existing == path: files[index] = (path, data) return def emit(node: ExportNode, prefix: list[str]) -> None: numbered = _ordered_numbered_children(node, chosen) numbers = {child.id: index + 1 for index, child in enumerate(numbered)} for child in node.children: if child.id in numbers: folder = prefix + [_folder_name(child.title, numbers[child.id], prefix)] if child.kind == "binding": if child.content_kind == "email": _emit_email(child, folder, add, rewrite, entries, loaded, notice_for, generated, chosen) else: _emit_loose(child, folder, add, entries, loaded, notice_for, generated, chosen) else: emit(child, folder) else: _emit_loose(child, prefix, add, entries, loaded, notice_for, generated, chosen) emit(root, []) return files, entries def _emit_email(node, folder, add, rewrite, entries, loaded, notice_for, generated: str, chosen: set[str]) -> None: mail = loaded.get(node.id) if isinstance(mail, MailPayload): eml = add(folder + ["nachricht.eml"], mail.raw) attachments = [] for attachment in mail.parsed.attachments: path = add(folder + ["anlagen", attachment.filename], attachment.data) attachments.append((attachment.filename, path)) notice = "\n".join(notice_for.get(node.id, [])) text_path = add(folder + ["lesefassung.md"], b"") readable = _mail_markdown(node, mail, text_path, eml, attachments, notice, generated) rewrite(text_path, readable.encode("utf-8")) links = [("EML-Original", eml), ("Lesefassung", text_path), *attachments] entries.append(_entry(node, eml, links, notice or None)) for child in node.children: if child.kind == "annotation" and child.id in loaded: path = add(folder + [_annotation_filename(child, folder)], _annotation_markdown(child).encode("utf-8")) entries.append(_entry(child, path, [], None)) _emit_related(node, folder, add, entries, loaded, notice_for, chosen, generated) def _emit_loose(node, prefix, add, entries, loaded, notice_for, generated: str, chosen: set[str]) -> None: bundle = _binding_bundle(prefix, node) if node.kind == "binding" else prefix if node.kind == "binding" and node.id in loaded and node.content_kind != "email": payload = loaded[node.id] if node.content_kind == "note": path = add(bundle + [f"{_segment(node.title)}.md"], _note_markdown(node, str(payload), generated).encode("utf-8")) entries.append(_entry(node, path, [], None)) elif isinstance(payload, FilePayload): name = _handover_filename(node, payload.filename) path = add(bundle + [name], payload.data) entries.append(_entry(node, path, [(name, path)], None)) if node.kind == "binding": for child in node.children: if child.kind == "annotation" and child.id in loaded and not (node.content_kind == "email" and node.id in loaded): path = add(bundle + [_annotation_filename(child, bundle)], _annotation_markdown(child).encode("utf-8")) entries.append(_entry(child, path, [], None)) elif node.kind == "annotation" and node.id in loaded: path = add(prefix + [_annotation_filename(node, prefix)], _annotation_markdown(node).encode("utf-8")) entries.append(_entry(node, path, [], None)) if node.kind == "binding": _emit_related(node, prefix, add, entries, loaded, notice_for, chosen, generated) def _binding_bundle(prefix: list[str], _host: ExportNode) -> list[str]: """Main file and correspondence sit inside the numbered entry folder.""" return list(prefix) def _handover_filename(node: ExportNode, original: str) -> str: """Readable name for recipients; file bytes stay unchanged.""" stem = _segment(node.title) suffix = PurePosixPath(original or "").suffix if suffix: return f"{stem}{suffix}" return stem or _file_segment(original) def _related_bucket(node: ExportNode) -> str: if node.kind == "attachment": return "Anlagen" if node.kind == "evidence": return "Belege" if node.kind == "dispatch_item": relation = node.relation or "" if "Anlage → Versand" in relation: return "Anlagen" return "Belege" return "Belege" def _emit_related(node, prefix, add, entries, loaded, notice_for, chosen, generated: str) -> None: bundle = _binding_bundle(prefix, node) for child in node.children: if child.kind == "attachment" and child.id in chosen and child.id in loaded: _emit_one(child, bundle + ["Anlagen"], add, entries, loaded, notice_for, generated) if child.kind != "dispatch": continue picked = [item for item in child.children if item.id in chosen and item.id in loaded] for item in picked: _emit_one(item, bundle + [_related_bucket(item)], add, entries, loaded, notice_for, generated) def _emit_one(node, prefix, add, entries, loaded, notice_for, generated: str) -> None: payload = loaded[node.id] notice = "\n".join(notice_for.get(node.id, [])) if isinstance(payload, GapNote): path = add(prefix + [f"{_segment(node.title)}-luecke.txt"], payload.reason.encode("utf-8")) entries.append(_entry(node, path, [( "Lücke", path)], notice or payload.reason)) return if isinstance(payload, MailPayload) or (isinstance(payload, FilePayload) and payload.media_type == "message/rfc822"): raw = payload.raw if isinstance(payload, MailPayload) else payload.data eml = add(prefix + ["nachricht.eml"], raw) links = [("EML-Original", eml)] try: parsed = payload.parsed if isinstance(payload, MailPayload) else parse_mail(raw) for attachment in parsed.attachments: path = add(prefix + ["anlagen", attachment.filename], attachment.data) links.append((attachment.filename, path)) except Exception: links.append(("Hinweis", eml)) entries.append(_entry(node, eml, links, notice or None)) return if isinstance(payload, str): path = add(prefix + [f"{_segment(node.title)}.md"], _note_markdown(node, payload, generated).encode("utf-8")) entries.append(_entry(node, path, [], notice or None)) return if isinstance(payload, FilePayload) and payload.media_type == "application/json": path = add(prefix + [f"{_segment(node.title)}.stand.json"], payload.data) text = _json_note(payload.data) reading = add(prefix + [f"{_segment(node.title)}.md"], _note_markdown(node, text, generated).encode("utf-8")) entries.append(_entry(node, reading, [("Gesicherter Text", path), ("Lesefassung", reading)], notice or None)) return if isinstance(payload, FilePayload): name = _handover_filename(node, payload.filename) path = add(prefix + [name], payload.data) entries.append(_entry(node, path, [(name, path)], notice or None)) def _correspondence_export_notices(root: ExportNode, chosen: set[str]) -> list[Notice]: notices: list[Notice] = [] for node in _walk(root): if node.kind != "binding": continue attachments = [child for child in node.children if child.kind == "attachment"] if not attachments: continue blocked = [child for child in attachments if not child.exportable] if blocked: sample = blocked[0] count = len(blocked) word = "Anlage ist" if count == 1 else f"{count} Anlagen sind" notices.append( Notice( sample.id, f"{word} dem Schreiben zugeordnet, aber für die Übergabe nicht freigegeben. " "Sie erscheinen in der Auswahl nicht und fehlen im Paket.", ) ) if node.id not in chosen: continue missing = [child for child in attachments if child.exportable and child.id not in chosen] if missing: sample = missing[0] count = len(missing) word = "Anlage ist" if count == 1 else f"{count} Anlagen sind" notices.append( Notice( sample.id, f"{word} dem Schreiben zugeordnet, aber nicht ausgewählt. Die Ausgabe ist unvollständig.", ) ) return notices def _attachment_load_issues(root: ExportNode, chosen: set[str], loaded: dict) -> list[Issue]: issues: list[Issue] = [] for node in _walk(root): if node.kind != "attachment" or node.id not in chosen or not node.exportable: continue if node.id not in loaded: issues.append( Issue( node.id, node.title, "missing", "Die zugeordnete Anlage konnte nicht für die Ausgabe geladen werden.", ) ) return issues def _attachment_emit_issues(root: ExportNode, chosen: set[str], loaded: dict, entries: list[Entry]) -> list[Issue]: emitted = {entry.node_id for entry in entries} issues: list[Issue] = [] for node in _walk(root): if node.kind != "attachment" or node.id not in chosen or not node.exportable: continue if node.id not in loaded: continue if node.id in emitted: continue issues.append( Issue( node.id, node.title, "missing", "Die ausgewählte Anlage fehlt in der erzeugten Paketstruktur.", ) ) return issues def _json_note(data: bytes) -> str: import json try: document = json.loads(data.decode("utf-8")) except (UnicodeDecodeError, json.JSONDecodeError): return "" parts = [] for block in document.get("blocks") or []: if isinstance(block, dict): text = str(block.get("text") or "").strip() if text: parts.append(text) return "\n\n".join(parts) def _bundle_notices(root: ExportNode, chosen: set[str]) -> list[Notice]: found = [] for node in _walk(root): if node.kind != "dispatch": continue parts = [child for child in node.children if child.kind in {"dispatch_item", "evidence"}] picked = [child for child in parts if child.id in chosen] if not picked: continue if len(picked) < len(parts): found.append( Notice( picked[0].id, "Auszug dieses Versands. Die Ausgabe ist nicht der vollständige Versandstand.", ) ) gaps = [child.gap_reason for child in picked if child.gap_reason] if gaps: found.append(Notice(picked[0].id, "Fehlende Fassung: " + " ".join(gaps))) return found def _entry(node: ExportNode, path: str | None, links, notice: str | None) -> Entry: notes = tuple(part for part in (node.relation, node.edition, node.gap_reason) if part) return Entry( node.id, node.title, _label(node), path, links, node.dates or {}, node.direction, node.summary, notice, [], _recorded(node), notes, ) def _attach_replies(conn, actor, page, entries: list[Entry], leaves: list[ExportNode]) -> None: by_element: dict[str, str] = {} nodes = {node.id: node for node in leaves} for entry in entries: node = nodes.get(entry.node_id) if node and node.kind == "binding" and node.element_id and entry.path and node.element_id not in by_element: by_element[node.element_id] = entry.path for entry in entries: node = nodes.get(entry.node_id) if node is None or node.kind != "binding" or not node.element_id: continue rows = conn.execute( """ SELECT refers_to_element_id FROM element_references WHERE element_id = %s ORDER BY created_at, refers_to_element_id """, (UUID(node.element_id),), ).fetchall() for row in rows: target_id = str(row["refers_to_element_id"]) target = get_element(conn, row["refers_to_element_id"]) if target is None or not element_rights(conn, actor, target, page).read: continue path = by_element.get(target_id) if path is None: continue entry.replies.append((target["title"], path)) def _with_overview(title: str, generated: str, root: ExportNode, entries: list[Entry], files: list[tuple[str, bytes]]): by_id = {entry.node_id: entry for entry in entries} sections = _overview_sections(root, by_id) html_doc = _html_overview(title, generated, sections) markdown = _markdown_overview(title, generated, sections) return [("index.html", html_doc.encode("utf-8")), ("inhalt.md", markdown.encode("utf-8")), *files] def _overview_sections(root: ExportNode, entries: dict[str, Entry]) -> list[OverviewNode]: found: list[OverviewNode] = [] for child in root.children: section = _overview_node(child, entries) if section is not None: found.append(section) return found def _overview_node(node: ExportNode, entries: dict[str, Entry]) -> OverviewNode | None: if node.kind in {"matter", "chapter"}: children = [] for child in node.children: section = _overview_node(child, entries) if section is not None: children.append(section) if not children: return None return OverviewNode("container", node.title, node.label or _KIND_LABEL.get(node.kind, ""), None, children) if node.kind == "binding": return _overview_binding(node, entries) return None def _overview_binding(node: ExportNode, entries: dict[str, Entry]) -> OverviewNode | None: groups: list[OverviewNode] = [] annotations = [ OverviewNode("leaf", child.title, _label(child), entries[child.id]) for child in node.children if child.kind == "annotation" and child.id in entries ] if annotations: groups.append(OverviewNode("group", "Anmerkungen", None, None, annotations)) anlagen = [ OverviewNode("leaf", child.title, _label(child), entries[child.id]) for child in node.children if child.kind == "attachment" and child.id in entries ] if anlagen: groups.append(OverviewNode("group", "Anlagen", None, None, anlagen)) belege: list[OverviewNode] = [] versand: list[OverviewNode] = [] for dispatch in node.children: if dispatch.kind != "dispatch": continue items = [ OverviewNode("leaf", item.title, _label(item), entries[item.id]) for item in dispatch.children if item.kind in {"dispatch_item", "evidence"} and item.id in entries ] if not items: continue bucket = "Belege" if (dispatch.label or "").startswith("Beleg") else (dispatch.label or "Versand") target = belege if bucket == "Belege" else versand target.append(OverviewNode("group", dispatch.title, bucket, None, items)) if belege: groups.append(OverviewNode("group", "Belege", None, None, belege)) if versand: groups.extend(versand) host = entries.get(node.id) if host is None and not groups: return None return OverviewNode("binding", node.title, node.label or _label(node), host, groups) def _overview_toc(sections: list[OverviewNode], counter: list[int]) -> tuple[list[tuple[str, str]], list[int]]: items: list[tuple[str, str]] = [] def walk(nodes: list[OverviewNode]) -> None: for node in nodes: if node.kind not in {"container", "binding"}: continue counter[0] += 1 anchor = f"inhalt-{counter[0]}" label = node.title if node.label and node.kind == "container": label = f"{node.label}: {node.title}" items.append((anchor, label)) if node.kind == "container": walk(node.children) walk(sections) return items, counter def _html_overview(title: str, generated: str, sections: list[OverviewNode]) -> str: counter = [0] toc, _counter = _overview_toc(sections, counter) counter[0] = 0 toc_html = "" if toc: toc_html = ( '" ) body = _html_overview_nodes(sections, generated, counter) or "
Die bestätigte Auswahl enthält keinen ausgebbaren Inhalt.
" return ( "" f"{label}{html.escape(node.title)}
{inner}{title}{subtitle}
{title}{subtitle}
{inner}' + "".join(f'{html.escape(label)}' for label, path in links) + "
" ) for reply_title, path in entry.replies: bits.append(f'') if entry.notice: bits.append(f'{html.escape(entry.notice)}
') return "".join(bits) def _markdown_overview(title: str, generated: str, sections: list[OverviewNode]) -> str: lines = [ f"# Übergabe {title}", "", f"Erzeugt am {generated}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.", "", ] if sections: lines.extend(["## Inhalt", ""]) for anchor, label in _overview_toc(sections, [0])[0]: lines.append(f"- [{label}](#{anchor})") lines.append("") lines.extend(_markdown_overview_nodes(sections, generated, depth=2)) return "\n".join(line for line in lines if line is not None).rstrip() + "\n" def _markdown_overview_nodes(sections: list[OverviewNode], generated: str, depth: int) -> list[str]: lines: list[str] = [] counter = [0] def walk(nodes: list[OverviewNode], level: int) -> None: for node in nodes: if node.kind == "container": counter[0] += 1 anchor = f"inhalt-{counter[0]}" heading = "#" * min(level, 6) label = f"{node.label}: " if node.label else "" lines.extend(["", f"{heading} {label}{node.title}", f"", ""]) walk(node.children, level + 1) elif node.kind == "binding": counter[0] += 1 anchor = f"inhalt-{counter[0]}" heading = "#" * min(level, 6) label = f" ({node.label})" if node.label else "" lines.extend(["", f"{heading} {node.title}{label}", f"", ""]) if node.entry: lines.extend(_markdown_entry_body(node.entry, generated)) for child in node.children: lines.extend(_markdown_group(child, generated, level + 1)) lines.append("") walk(sections, depth) return lines def _markdown_group(node: OverviewNode, generated: str, depth: int) -> list[str]: if node.kind == "leaf": return _markdown_leaf(node.entry, depth) if node.kind != "group": return [] heading = "#" * min(depth, 6) title = node.title if node.label and node.label not in title: title = f"{title} ({node.label})" lines = ["", f"{heading} {title}", ""] for child in node.children: lines.extend(_markdown_group(child, generated, depth + 1)) return lines def _markdown_leaf(entry: Entry | None, depth: int) -> list[str]: if entry is None: return [] indent = " " * max(0, depth - 4) links = _entry_links(entry) notes = f" — {' · '.join(entry.notes)}" if entry.notes else "" if not links: return [f"{indent}- {entry.title} ({entry.kind_label}){notes}"] link = " · ".join(f"[{label}]({_href(path)})" for label, path in links) return [f"{indent}- **{entry.title}** ({entry.kind_label}){notes}: {link}"] def _markdown_entry_body(entry: Entry, generated: str) -> list[str]: lines = [] for fact in _fact_lines(entry, generated): lines.append(f"- {fact}") if entry.summary: lines.append(f"- Kurzfassung: {entry.summary}") for label, path in _entry_links(entry): lines.append(f"- [{label}]({_href(path)})") for reply_title, path in entry.replies: lines.append(f"- Antwort auf [{reply_title}]({_href(path)})") if entry.notice: lines.extend(["", entry.notice]) return lines def _entry_links(entry: Entry) -> list[tuple[str, str]]: if entry.links: return entry.links if entry.path: return [(entry.kind_label, entry.path)] return [] def _fact_lines(entry: Entry, generated: str) -> list[str]: dates = entry.dates or {} lines = [] if dates.get("document_on"): lines.append(f"Fachdatum: {dates['document_on']}") if dates.get("sent_on"): lines.append(f"Gesendet: {dates['sent_on']}") if dates.get("received_on"): lines.append(f"Empfangen: {dates['received_on']}") if entry.recorded_at: lines.append(f"Erfasst am: {entry.recorded_at}") if entry.direction in _DIRECTION: lines.append(_DIRECTION[entry.direction]) lines.extend(entry.notes) lines.append(f"Stand der Ausgabe: {generated}") return lines def _note_markdown(node: ExportNode, body: str, generated: str) -> str: lines = [f"# {node.title}", ""] lines.extend(f"- {fact}" for fact in _fact_lines(_entry(node, None, [], None), generated)) if node.summary: lines.extend(["", f"Kurzfassung: {node.summary}"]) lines.extend(["", body, ""]) return "\n".join(lines) def _mail_markdown(node, mail: MailPayload, text_path: str, eml_path: str, attachments, notice: str, generated: str) -> str: parsed = mail.parsed lines = [f"# {node.title}", "", "Lesefassung. Das unveränderte Original ist die EML-Datei.", ""] lines.extend(f"- {fact}" for fact in _fact_lines(_entry(node, None, [], None), generated)) if parsed.sender: lines.append(f"- Von: {parsed.sender}") if parsed.recipient: lines.append(f"- An: {parsed.recipient}") if parsed.copy: lines.append(f"- Kopie: {parsed.copy}") lines.append(f"- [EML-Original]({_href(_relative(text_path, eml_path))})") lines.extend(["", parsed.text or "Der Text steht im EML-Original.", ""]) if attachments: lines.append("## Anlagen") lines.append("") for label, path in attachments: lines.append(f"- [{label}]({_href(_relative(text_path, path))})") lines.append("") if notice: lines.extend([notice, ""]) return "\n".join(lines) def _stitch_replies(files: list[tuple[str, bytes]], entries: list[Entry]) -> None: positions = {path: index for index, (path, _data) in enumerate(files)} for entry in entries: if not entry.replies: continue target = entry.path if entry.path and entry.path.endswith(".md") else None if target is None: target = next((path for label, path in entry.links if label == "Lesefassung"), None) if target is None or target not in positions: continue extra = ["", "## Antwortbezug", ""] for title, path in entry.replies: extra.append(f"- [{title}]({_href(_relative(target, path))})") extra.append("") index = positions[target] files[index] = (target, files[index][1] + ("\n".join(extra)).encode("utf-8")) def _annotation_markdown(node: ExportNode) -> str: lines = [f"# {node.title}", ""] if node.annotation_author or node.annotation_at: who = node.annotation_author or "Unbekannt" when = node.annotation_at or "" lines.append(f"Verfasst von {who}" + (f", {when}" if when else "") + ".") lines.append("") lines.append(node.annotation_body or "") lines.append("") return "\n".join(lines) def _annotation_filename(node: ExportNode, _prefix) -> str: return f"{_segment(node.title)}.md" def _ordered_numbered_children(node: ExportNode, chosen: set[str]) -> list[ExportNode]: """Chapters, Teilvorgänge and every Schriftstück share one order within the parent.""" items: list[ExportNode] = [] for child in node.children: if child.kind in {"chapter", "matter"} and _included(child, chosen): items.append(child) elif child.kind == "binding" and _included(child, chosen): items.append(child) return items def _included(node: ExportNode, chosen: set[str]) -> bool: if node.kind == "binding" and node.content_kind == "email": return any(child.id in chosen for child in _walk(node) if child.kind in _NODE_KINDS) return any(child.id in chosen for child in _walk(node) if child.kind in _NODE_KINDS) def _label(node: ExportNode) -> str: return node.label or _KIND_LABEL.get(node.content_kind or node.kind, "Inhalt") def _recorded(node: ExportNode) -> str | None: if not node.dates: return None return node.dates.get("recorded_at") def _segment(value: str, limit: int = 80) -> str: cleaned = [] for char in value or "": if char in '\\/:*?"<>|' or ord(char) < 32: cleaned.append(" ") else: cleaned.append(char) text = " ".join("".join(cleaned).split()).strip(" .") if text in {"", ".", ".."}: text = "Inhalt" return text[:limit] def _folder_name(title: str, number: int, prefix: list[str]) -> str: """Keep a readable directory name inside a bounded path. The overview keeps the full title.""" limit = 80 while True: name = f"{number:02d} {_segment(title, limit)}" if len("/".join([*prefix, name])) <= 180 or limit <= 24: return name limit -= 8 def _file_segment(value: str) -> str: name = (value or "datei").replace("\\", "/").split("/")[-1] return _segment(name) or "datei" def _unique_path(parts: list[str], used: set[str]) -> str: folder = [_segment(part) for part in parts[:-1]] filename = _file_segment(parts[-1]) path = "/".join([*folder, filename]) if path not in used: used.add(path) return path stem, dot, ext = filename.rpartition(".") if not dot: stem, extension = filename, "" else: extension = f".{ext}" number = 2 while True: candidate = "/".join([*folder, f"{stem} ({number}){extension}"]) if candidate not in used: used.add(candidate) return candidate number += 1 def _href(path: str) -> str: return "/".join(quote(part) for part in PurePosixPath(path).parts) def _relative(from_path: str, to_path: str) -> str: source = PurePosixPath(from_path).parent.parts target = PurePosixPath(to_path).parts shared = 0 while shared < len(source) and shared < len(target) and source[shared] == target[shared]: shared += 1 hops = [".."] * (len(source) - shared) return "/".join([*hops, *target[shared:]]) or "." def _zip_bytes(files: list[tuple[str, bytes]]) -> bytes: buffer = io.BytesIO() with zipfile.ZipFile(buffer, "w", compression=zipfile.ZIP_DEFLATED) as archive: for path, data in files: info = zipfile.ZipInfo(path) info.flag_bits |= 0x800 info.compress_type = zipfile.ZIP_DEFLATED info.date_time = datetime.now(timezone.utc).timetuple()[:6] archive.writestr(info, data) return buffer.getvalue() def _zip_name(title: str) -> str: return f"Übergabe {_segment(title)}.zip"