"""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.present import _chapters, _ordered_bindings from app.retrieval import ( FilePayload, MailPayload, RetrievalFailure, load_external, load_file, ) _NODE_KINDS = {"binding", "annotation"} _DIRECTION = {"in": "Eingang", "out": "Ausgang"} _KIND_LABEL = { "note": "Notiz", "file": "Datei", "email": "E-Mail", "paperless": "Paperless", "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 = "" 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 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 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.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"}: 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)) issues.extend(_reference_issues(conn, actor, page, leaves)) 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) _attach_replies(conn, actor, page, entries, leaves) _stitch_replies(files, entries) files = _with_overview(matter["title"], generated, 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] = [] for chapter in chapters: items = grouped.get(chapter["id"], []) if not items: continue children.append( ExportNode( id=f"chapter:{chapter['id']}", kind="chapter", title=chapter["title"], exportable=True, reason=None, content_kind=None, children=[_binding_node(item) for item in items], label="Kapitel", ) ) for binding in grouped.get(None, []): children.append(_binding_node(binding)) 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 _binding_node(binding: dict) -> 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", ) ) return node 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 == "imap": return "email" if source == "paperless": return "paperless" if source == "url": return "url" return element["kind"] 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 "").strip() if text: 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: folders = [child for child in node.children if _is_folder(child) and _included(child, chosen)] numbers = {child.id: index + 1 for index, child in enumerate(folders)} for child in node.children: if child.id in numbers: folder = prefix + [f"{numbers[child.id]:02d} {_segment(child.title)}"] if child.kind == "binding" and child.content_kind == "email": _emit_email(child, folder, add, rewrite, entries, loaded, notice_for, generated) else: emit(child, folder) else: _emit_loose(child, prefix, add, entries, loaded, notice_for, generated) emit(root, []) return files, entries def _emit_email(node, folder, add, rewrite, entries, loaded, notice_for, generated: str) -> None: mail = loaded[node.id] assert 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)) def _emit_loose(node, prefix, add, entries, loaded, notice_for, generated: str) -> None: 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(prefix + [f"{_segment(node.title)}.md"], _note_markdown(node, str(payload), generated).encode("utf-8")) entries.append(_entry(node, path, [], None)) elif isinstance(payload, FilePayload): path = add(prefix + [payload.filename], payload.data) entries.append(_entry(node, path, [(payload.filename, 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(prefix + [_annotation_filename(child, prefix)], _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)) def _entry(node: ExportNode, path: str | None, links, notice: str | None) -> Entry: return Entry( node.id, node.title, _label(node), path, links, node.dates or {}, node.direction, node.summary, notice, [], _recorded(node), ) 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, entries: list[Entry], files: list[tuple[str, bytes]]): html_doc = _html_overview(title, generated, entries) markdown = _markdown_overview(title, generated, entries) return [("index.html", html_doc.encode("utf-8")), ("inhalt.md", markdown.encode("utf-8")), *files] def _html_overview(title: str, generated: str, entries: list[Entry]) -> str: items = [] for entry in entries: bits = [f"
{html.escape(entry.kind_label)}
"] facts = _fact_lines(entry, generated) if facts: bits.append("Kurzfassung: {html.escape(entry.summary)}
") for label, path in _entry_links(entry): bits.append(f'') for reply_title, path in entry.replies: bits.append( f'Antwort auf {html.escape(reply_title)}
' ) if entry.notice: bits.append(f"{html.escape(entry.notice)}
") items.append(f"Die bestätigte Auswahl enthält keinen ausgebbaren Inhalt.
" return ( "" f"Erzeugt am {html.escape(generated)}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.
" f"{body}" ) def _markdown_overview(title: str, generated: str, entries: list[Entry]) -> str: lines = [ f"# Übergabe {title}", "", f"Erzeugt am {generated}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.", "", ] for entry in entries: lines.append(f"## {entry.title}") lines.append("") lines.append(entry.kind_label) lines.append("") 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.append("") lines.append(entry.notice) lines.append("") return "\n".join(lines).rstrip() + "\n" 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.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 _is_folder(node: ExportNode) -> bool: return node.kind in {"chapter", "matter"} or (node.kind == "binding" and node.content_kind == "email") def _included(node: ExportNode, chosen: set[str]) -> bool: if node.kind == "binding" and node.content_kind == "email": return node.id in chosen 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) -> 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(" .") return (text or "Inhalt")[:80] 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"