diff --git a/backend/app/exporting.py b/backend/app/exporting.py index e1fca0e..ef18ed4 100644 --- a/backend/app/exporting.py +++ b/backend/app/exporting.py @@ -136,6 +136,15 @@ class Entry: } +@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 @@ -263,7 +272,7 @@ def _plan(conn, actor, matter_id, context, selected, assignments, media_root) -> ) _attach_replies(conn, actor, page, entries, leaves) _stitch_replies(files, entries) - files = _with_overview(matter["title"], generated, entries, files) + files = _with_overview(matter["title"], generated, root, entries, files) return ExportPlan(matter["title"], generated, True, [], notices, entries, files, root) @@ -866,67 +875,309 @@ def _attach_replies(conn, actor, page, entries: list[Entry], leaves: list[Export 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) +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 _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.
" +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"Erzeugt am {html.escape(generated)}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.
" - f"{body}" + "" + f"