From 2b23992a1a653f81d1fc2f1bc8b52dee1acb40c8 Mon Sep 17 00:00:00 2001 From: Lars Date: Sun, 18 Oct 2026 14:32:05 +0200 Subject: [PATCH] =?UTF-8?q?Ordne=20Anlagen=20und=20Belege=20im=20=C3=9Cber?= =?UTF-8?q?gabepaket=20pro=20Hauptschriftst=C3=BCck=20zu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Statt gemeinsamem anlagen-arbeitsstand und Datumsordnern auf Kapitel-Ebene liegen zugeordnete Dateien unter Dokumentname/Anlagen bzw. Belege, damit mehrere Schreiben im gleichen Kapitel vollständig und eindeutig exportiert werden. Co-authored-by: Cursor --- backend/app/exporting.py | 26 ++++-- backend/tests/test_correspondence.py | 4 +- backend/tests/test_export.py | 129 +++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 7 deletions(-) diff --git a/backend/app/exporting.py b/backend/app/exporting.py index 4a701ed..363b8b7 100644 --- a/backend/app/exporting.py +++ b/backend/app/exporting.py @@ -608,18 +608,34 @@ def _emit_loose(node, prefix, add, entries, loaded, notice_for, generated: str, _emit_related(node, prefix, add, entries, loaded, notice_for, chosen, generated) +def _host_bundle(prefix: list[str], host: ExportNode) -> list[str]: + """Correspondence files stay under the host title, not a shared chapter folder.""" + return prefix + [_segment(host.title)] + + +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 = _host_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, prefix + ["anlagen-arbeitsstand"], add, entries, loaded, notice_for, generated) + _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] - if not picked: - continue - folder = prefix + [_segment(child.title)] for item in picked: - _emit_one(item, folder, add, entries, loaded, notice_for, generated) + _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: diff --git a/backend/tests/test_correspondence.py b/backend/tests/test_correspondence.py index 3fedee3..077911c 100644 --- a/backend/tests/test_correspondence.py +++ b/backend/tests/test_correspondence.py @@ -384,7 +384,7 @@ def test_dispatch_keeps_the_secured_stand_and_distinct_mail_parts(client, db, mo selected = [node["id"] for node in nodes if node["kind"] in {"dispatch_item", "evidence"}] archive = _zip(ada, matter["id"], selected) names = archive.namelist() - snapshot = next(name for name in names if name.endswith("/lage.pdf") and "/anlagen/" not in name) + snapshot = next(name for name in names if name.endswith("/Anlagen/lage.pdf")) assert archive.read(snapshot) == PDF_B eml_name = next(name for name in names if name.endswith("nachricht.eml")) assert b"Fachlicher Zusatz" in archive.read(eml_name) @@ -517,7 +517,7 @@ def test_dispatch_keeps_the_secured_stand_and_distinct_mail_parts(client, db, mo assert gaps assert all("Später geänderter Text" not in full.read(name).decode("utf-8") for name in gaps) assert all(b"%PDF" not in full.read(name) for name in gaps) - secured = next(name for name in full.namelist() if name.endswith("/lage.pdf") and "/anlagen/" not in name) + secured = next(name for name in full.namelist() if name.endswith("/Anlagen/lage.pdf")) assert full.read(secured) == PDF_B diff --git a/backend/tests/test_export.py b/backend/tests/test_export.py index 882fc95..bd4333b 100644 --- a/backend/tests/test_export.py +++ b/backend/tests/test_export.py @@ -777,3 +777,132 @@ def test_nested_chapter_export_keeps_path_and_complete_mail(client, db, monkeypa assert overview.index("Anschreiben") < overview.index("Heizkostenbeleg") assert "Hausmeisterbeleg" not in overview assert "Ausgelassen" not in overview + + +def test_correspondence_attachments_stay_under_each_host_in_one_chapter(client, db): + ada = Actor(client, "ada-host-bundle@example.test", "Ada Host Bundle") + register(db, ada.email, ada.name) + ada.login() + matter = ada.request("POST", "/api/v1/matters", json={"title": "Gemeinsames Kapitel"}, status=201) + chapter = ada.request( + "POST", + f"/api/v1/matters/{matter['id']}/chapters", + json={"title": "Schriftwechsel"}, + status=201, + ) + first = ada.request( + "POST", + "/api/v1/notes", + json={ + "title": "Erstes Schreiben", + "document": document("Text eins"), + "context": {"kind": "matter", "id": matter["id"]}, + "chapter_id": chapter["id"], + }, + status=201, + ) + second = ada.request( + "POST", + "/api/v1/notes", + json={ + "title": "Zweites Schreiben", + "document": document("Text zwei"), + "context": {"kind": "matter", "id": matter["id"]}, + "chapter_id": chapter["id"], + }, + status=201, + ) + att_a1 = ada.request( + "POST", + "/api/v1/files", + data={"title": "Anlage A1", "context_kind": "matter", "context_id": matter["id"]}, + files={"upload": ("a1.pdf", PDF_A, "application/pdf")}, + status=201, + ) + att_a2 = ada.request( + "POST", + "/api/v1/files", + data={"title": "Anlage A2", "context_kind": "matter", "context_id": matter["id"]}, + files={"upload": ("a2.pdf", PDF_B, "application/pdf")}, + status=201, + ) + att_b1 = ada.request( + "POST", + "/api/v1/files", + data={"title": "Anlage B1", "context_kind": "matter", "context_id": matter["id"]}, + files={"upload": ("b1.pdf", PDF_C, "application/pdf")}, + status=201, + ) + att_b2 = ada.request( + "POST", + "/api/v1/files", + data={"title": "Anlage B2", "context_kind": "matter", "context_id": matter["id"]}, + files={"upload": ("a1.pdf", PDF_A, "application/pdf")}, + status=201, + ) + receipt = ada.request( + "POST", + "/api/v1/notes", + json={ + "title": "Einlieferungsbeleg", + "document": document("Eingeliefert"), + "context": {"kind": "matter", "id": matter["id"]}, + "dates": {"document_on": "2026-04-01"}, + }, + status=201, + ) + detail = ada.request("GET", f"/api/v1/matters/{matter['id']}") + host_a = next(item for item in detail["bindings"] if item["element"]["title"] == "Erstes Schreiben") + host_b = next(item for item in detail["bindings"] if item["element"]["title"] == "Zweites Schreiben") + ada.request( + "POST", + f"/api/v1/elements/{first['id']}/attachments", + json={ + "expected_version": host_a["element"]["version"], + "context": {"kind": "matter", "id": matter["id"]}, + "attachment_element_ids": [att_a1["id"], att_a2["id"]], + "fold": "attachment", + }, + status=201, + ) + ada.request( + "POST", + f"/api/v1/elements/{second['id']}/attachments", + json={ + "expected_version": host_b["element"]["version"], + "context": {"kind": "matter", "id": matter["id"]}, + "attachment_element_ids": [att_b1["id"], att_b2["id"]], + "fold": "attachment", + }, + status=201, + ) + ada.request( + "POST", + f"/api/v1/elements/{first['id']}/dispatches", + json={ + "expected_version": host_a["element"]["version"], + "context": {"kind": "matter", "id": matter["id"]}, + "channel": "post", + "sent_on": "2026-04-02", + "counterpart": "Post", + "attachment_element_ids": [], + "evidence": [{"element_id": receipt["id"], "role": "submission", "fold": "keep"}], + }, + status=201, + ) + tree = ada.request("GET", f"/api/v1/matters/{matter['id']}/export-tree")["tree"] + selected = [ + node["id"] + for node in _walk(tree) + if node["kind"] in {"binding", "attachment", "evidence"} and node["exportable"] + ] + response = _post_zip(ada, matter["id"], {"context": {"kind": "matter", "id": matter["id"]}, "selected": selected}) + assert response.status_code == 200, response.text + names = _archive(response.content).namelist() + assert not any("anlagen-arbeitsstand" in name.lower() for name in names) + first_paths = [name for name in names if "Erstes Schreiben/Erstes Schreiben/Anlagen/" in name.replace("\\", "/")] + second_paths = [name for name in names if "Zweites Schreiben/Zweites Schreiben/Anlagen/" in name.replace("\\", "/")] + assert sorted(name.rsplit("/", 1)[-1] for name in first_paths) == ["a1.pdf", "a2.pdf"] + assert sorted(name.rsplit("/", 1)[-1] for name in second_paths) == ["a1.pdf", "b1.pdf"] + assert any("Erstes Schreiben/Erstes Schreiben/Belege/" in name.replace("\\", "/") for name in names) + assert not any(name.replace("\\", "/").startswith("01 Schriftwechsel/2026-04") for name in names)