Gib ein Uebergabepaket aus der bestaetigten Auswahl aus.
Die Dev-Umgebung soll die hierarchische Auswahl und das Paket pruefen koennen. Vorhandene Symbolaktionen der Inhaltszeilen bleiben im selben Stand. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7626c56f3d
commit
27cde035e8
21 files changed
+2427
-54
No files matched your search
@@ -0,0 +1,99 @@
|
||||
"""Handover package: selection tree, completeness review, ZIP download."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from urllib.parse import quote
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Request, Response
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import Ctx, parse_query_context
|
||||
from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.schemas import ExportIn
|
||||
from app.core.errors import unprocessable
|
||||
from app.exporting import build_export, export_tree, review_export
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _disposition(filename: str) -> str:
|
||||
# Header values stay ASCII. The readable name travels in filename*.
|
||||
encoded = quote(filename)
|
||||
return f"attachment; filename=\"uebergabe.zip\"; filename*=UTF-8''{encoded}"
|
||||
|
||||
|
||||
def _context(value) -> Ctx | None:
|
||||
return None if value is None else Ctx(value.kind, value.id)
|
||||
|
||||
|
||||
def _selection(body: ExportIn) -> tuple[list[str], dict[str, UUID]]:
|
||||
if len(body.assignments) > 200:
|
||||
raise unprocessable("Die Zuordnung der Zugänge ist zu umfangreich.")
|
||||
return list(body.selected), dict(body.assignments)
|
||||
|
||||
|
||||
@router.get("/api/v1/matters/{matter_id}/export-tree")
|
||||
def get_export_tree(
|
||||
matter_id: UUID,
|
||||
context_kind: str | None = None,
|
||||
context_id: str | None = None,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
return export_tree(
|
||||
conn,
|
||||
principal.account_id,
|
||||
matter_id,
|
||||
parse_query_context(context_kind, context_id),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/v1/matters/{matter_id}/export/review")
|
||||
def post_export_review(
|
||||
matter_id: UUID,
|
||||
body: ExportIn,
|
||||
request: Request,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
selected, assignments = _selection(body)
|
||||
plan = review_export(
|
||||
conn,
|
||||
principal.account_id,
|
||||
matter_id,
|
||||
_context(body.context),
|
||||
selected,
|
||||
assignments,
|
||||
request.app.state.settings.media_root,
|
||||
)
|
||||
return plan.review()
|
||||
|
||||
|
||||
@router.post("/api/v1/matters/{matter_id}/export")
|
||||
def post_export(
|
||||
matter_id: UUID,
|
||||
body: ExportIn,
|
||||
request: Request,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
selected, assignments = _selection(body)
|
||||
filename, payload = build_export(
|
||||
conn,
|
||||
principal.account_id,
|
||||
matter_id,
|
||||
_context(body.context),
|
||||
selected,
|
||||
assignments,
|
||||
request.app.state.settings.media_root,
|
||||
)
|
||||
return Response(
|
||||
content=payload,
|
||||
media_type="application/zip",
|
||||
headers={
|
||||
"Content-Disposition": _disposition(filename),
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
},
|
||||
)
|
||||
@@ -340,3 +340,9 @@ class TaskDependencyIn(APIModel):
|
||||
class ElementReferenceIn(APIModel):
|
||||
refers_to_element_id: UUID
|
||||
context: ContextIn | None = None
|
||||
|
||||
|
||||
class ExportIn(APIModel):
|
||||
context: ContextIn | None = None
|
||||
selected: list[str] = Field(default_factory=list, max_length=2000)
|
||||
assignments: dict[str, UUID] = Field(default_factory=dict)
|
||||
@@ -0,0 +1,793 @@
|
||||
"""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"<h2>{html.escape(entry.title)}</h2>", f"<p>{html.escape(entry.kind_label)}</p>"]
|
||||
facts = _fact_lines(entry, generated)
|
||||
if facts:
|
||||
bits.append("<ul>" + "".join(f"<li>{html.escape(line)}</li>" for line in facts) + "</ul>")
|
||||
if entry.summary:
|
||||
bits.append(f"<p>Kurzfassung: {html.escape(entry.summary)}</p>")
|
||||
for label, path in _entry_links(entry):
|
||||
bits.append(f'<p><a href="{_href(path)}">{html.escape(label)}</a></p>')
|
||||
for reply_title, path in entry.replies:
|
||||
bits.append(
|
||||
f'<p>Antwort auf <a href="{_href(path)}">{html.escape(reply_title)}</a></p>'
|
||||
)
|
||||
if entry.notice:
|
||||
bits.append(f"<p>{html.escape(entry.notice)}</p>")
|
||||
items.append(f"<article>{''.join(bits)}</article>")
|
||||
body = "".join(items) or "<p>Die bestätigte Auswahl enthält keinen ausgebbaren Inhalt.</p>"
|
||||
return (
|
||||
"<!DOCTYPE html><html lang=\"de\"><head><meta charset=\"utf-8\">"
|
||||
f"<title>Übergabe {html.escape(title)}</title>"
|
||||
"<style>body{font-family:Segoe UI,sans-serif;line-height:1.45;max-width:46rem;margin:2rem auto;padding:0 1rem;color:#1c1b18}"
|
||||
"a{color:#085041}article{border-top:1px solid #ddd;padding:1rem 0}</style></head><body>"
|
||||
f"<h1>Übergabe {html.escape(title)}</h1>"
|
||||
f"<p>Erzeugt am {html.escape(generated)}. Fachliche Daten stehen bei den einzelnen Inhalten und sind kein Erzeugungszeitpunkt.</p>"
|
||||
f"{body}</body></html>"
|
||||
)
|
||||
|
||||
|
||||
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"
|
||||
@@ -9,6 +9,7 @@ from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.api.export import router as export_router
|
||||
from app.api.routes import router
|
||||
from app.api.sources import router as sources_router
|
||||
from app.core.errors import Problem, problem_response
|
||||
@@ -115,6 +116,7 @@ def create_app() -> FastAPI:
|
||||
|
||||
app.include_router(router)
|
||||
app.include_router(sources_router)
|
||||
app.include_router(export_router)
|
||||
return app
|
||||
|
||||
|
||||
|
||||
@@ -936,6 +936,7 @@ LABELS = {
|
||||
"annotation_created": "Kontextbezogene Anmerkung erfasst",
|
||||
"file_added": "Datei hinzugefügt",
|
||||
"external_ref_added": "Externe Referenz erfasst",
|
||||
"export_created": "Übergabepaket erzeugt",
|
||||
"binding_created": "Einbindung ergänzt",
|
||||
"binding_removed": "Einbindung entfernt",
|
||||
"element_deleted": "Element gelöscht",
|
||||
|
||||
+31
-13
@@ -8,6 +8,7 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
import psycopg
|
||||
import pytest
|
||||
@@ -28,17 +29,31 @@ os.environ["MEDIA_ROOT"] = tempfile.mkdtemp(prefix="tsumugu-pytest-")
|
||||
|
||||
|
||||
def _reset(settings) -> None:
|
||||
with psycopg.connect(settings.dsn(), autocommit=True) as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
TRUNCATE TABLE
|
||||
media_operations,
|
||||
domain_events,
|
||||
auth_accounts,
|
||||
data_rooms
|
||||
RESTART IDENTITY CASCADE
|
||||
"""
|
||||
)
|
||||
# The suite truncates between tests while the app pool may still release
|
||||
# the previous request's transaction. A deadlock abort would roll back that
|
||||
# transaction and the next login would fail. Waiting and retrying keeps the
|
||||
# reset outside the product path.
|
||||
last_error: Exception | None = None
|
||||
for attempt in range(8):
|
||||
try:
|
||||
with psycopg.connect(settings.dsn(), autocommit=True) as conn:
|
||||
conn.execute("SET lock_timeout = '1500ms'")
|
||||
conn.execute(
|
||||
"""
|
||||
TRUNCATE TABLE
|
||||
media_operations,
|
||||
domain_events,
|
||||
auth_accounts,
|
||||
data_rooms
|
||||
RESTART IDENTITY CASCADE
|
||||
"""
|
||||
)
|
||||
return
|
||||
except (psycopg.errors.DeadlockDetected, psycopg.errors.LockNotAvailable) as exc:
|
||||
last_error = exc
|
||||
time.sleep(0.15 * (attempt + 1))
|
||||
if last_error is not None:
|
||||
raise last_error
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
@@ -83,6 +98,9 @@ def clean(database, client):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db(database):
|
||||
def db(database, clean):
|
||||
with psycopg.connect(database.dsn(), row_factory=psycopg.rows.dict_row) as conn:
|
||||
yield conn
|
||||
try:
|
||||
yield conn
|
||||
finally:
|
||||
conn.rollback()
|
||||
@@ -0,0 +1,602 @@
|
||||
"""Handover package: hierarchy, complete mail, source identity, rights, failures."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import json
|
||||
import zipfile
|
||||
from email.message import EmailMessage
|
||||
from urllib.parse import unquote, urljoin
|
||||
|
||||
import pytest
|
||||
|
||||
from app.core.errors import Problem
|
||||
from app.mailpack import choose_message, parse_mail
|
||||
from app.sources import pull_paperless_original
|
||||
from tests.support import ORIGIN, Actor, document, grant, register
|
||||
|
||||
PDF_A = b"%PDF-1.4\n1 0 obj<<>>endobj\ntrailer<<>>\n%%EOF\nA\n"
|
||||
PDF_B = b"%PDF-1.4\n2 0 obj<<>>endobj\ntrailer<<>>\n%%EOF\nB\n"
|
||||
PDF_C = b"%PDF-1.4\n3 0 obj<<>>endobj\ntrailer<<>>\n%%EOF\nC\n"
|
||||
|
||||
|
||||
def _eml(message_id: str, body: str, attachments: list[tuple[str, bytes]], subject: str = "Schreiben") -> bytes:
|
||||
message = EmailMessage()
|
||||
message["Subject"] = subject
|
||||
message["From"] = "kanzlei@example.test"
|
||||
message["To"] = "ada@example.test"
|
||||
message["Message-ID"] = message_id
|
||||
message["Date"] = "Thu, 12 Mar 2026 10:00:00 +0100"
|
||||
message.set_content(body)
|
||||
for name, data in attachments:
|
||||
message.add_attachment(data, maintype="application", subtype="pdf", filename=name)
|
||||
return message.as_bytes()
|
||||
|
||||
|
||||
def _walk(node):
|
||||
yield node
|
||||
for child in node.get("children") or []:
|
||||
yield from _walk(child)
|
||||
|
||||
|
||||
def _parents(node, parent=None, found=None):
|
||||
found = found if found is not None else {}
|
||||
found[node["id"]] = parent
|
||||
for child in node.get("children") or []:
|
||||
_parents(child, node, found)
|
||||
return found
|
||||
|
||||
|
||||
def _under(node_id: str, title: str, parents: dict) -> bool:
|
||||
current = parents.get(node_id)
|
||||
while current is not None:
|
||||
if current.get("title") == title:
|
||||
return True
|
||||
current = parents.get(current["id"])
|
||||
return False
|
||||
|
||||
|
||||
def _post_zip(actor: Actor, matter_id: str, body: dict):
|
||||
headers = {
|
||||
"Origin": ORIGIN,
|
||||
"X-CSRF-Token": actor.csrf,
|
||||
"Content-Type": "application/json",
|
||||
"X-Scope-Kind": actor.scope["kind"],
|
||||
"X-Scope-Id": actor.scope["id"],
|
||||
}
|
||||
return actor.client.post(f"/api/v1/matters/{matter_id}/export", json=body, headers=headers)
|
||||
|
||||
|
||||
def _archive(blob: bytes) -> zipfile.ZipFile:
|
||||
return zipfile.ZipFile(io.BytesIO(blob))
|
||||
|
||||
|
||||
def _joined(archive: zipfile.ZipFile) -> str:
|
||||
parts = []
|
||||
for name in archive.namelist():
|
||||
parts.append(name)
|
||||
if name.endswith((".md", ".html", ".eml")):
|
||||
parts.append(archive.read(name).decode("utf-8", errors="replace"))
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
def _links_resolve(archive: zipfile.ZipFile) -> None:
|
||||
html = archive.read("index.html").decode("utf-8")
|
||||
names = set(archive.namelist())
|
||||
for chunk in html.split('href="')[1:]:
|
||||
href = unquote(chunk.split('"', 1)[0])
|
||||
assert href in names, href
|
||||
for name in archive.namelist():
|
||||
if not name.endswith(".md"):
|
||||
continue
|
||||
text = archive.read(name).decode("utf-8")
|
||||
base = name.rsplit("/", 1)[0] + "/" if "/" in name else ""
|
||||
for chunk in text.split("](")[1:]:
|
||||
href = unquote(chunk.split(")", 1)[0])
|
||||
if href.startswith("http"):
|
||||
continue
|
||||
target = urljoin(base, href)
|
||||
assert target in names, (name, href, target)
|
||||
|
||||
|
||||
def test_duplicate_attachments_stay_distinct_and_identity_is_exact():
|
||||
raw = _eml("<same@example.test>", "Volltext", [("lage.pdf", PDF_A), ("lage.pdf", PDF_B)])
|
||||
parsed = parse_mail(raw)
|
||||
assert [item.filename for item in parsed.attachments] == ["lage.pdf", "lage (2).pdf"]
|
||||
assert parsed.attachments[0].data == PDF_A
|
||||
assert parsed.attachments[1].data == PDF_B
|
||||
assert parsed.text == "Volltext"
|
||||
right = _eml("<right@example.test>", "richtig", [])
|
||||
wrong = _eml("<wrong@example.test>", "falsch", [])
|
||||
assert choose_message(right, [wrong], "<right@example.test>") == right
|
||||
assert choose_message(wrong, [right], "<right@example.test>") == right
|
||||
with pytest.raises(Exception, match="mehrdeutig"):
|
||||
choose_message(wrong, [right, right], "<right@example.test>")
|
||||
with pytest.raises(Exception, match="nicht eindeutig"):
|
||||
choose_message(wrong, [], "uid:INBOX:4")
|
||||
bare = _eml("", "ohne id", [])
|
||||
# A message built without Message-ID still matches a stored uid fallback.
|
||||
assert choose_message(bare, [], "uid:INBOX:4") == bare
|
||||
|
||||
|
||||
def test_paperless_original_never_uses_preview(monkeypatch):
|
||||
calls = []
|
||||
|
||||
def fake_file(base, token, path, max_bytes=40 * 1024 * 1024):
|
||||
calls.append(path)
|
||||
return None
|
||||
|
||||
monkeypatch.setattr("app.sources._paperless_file", fake_file)
|
||||
monkeypatch.setattr("app.sources._paperless_metadata", lambda *args, **kwargs: None)
|
||||
with pytest.raises(Problem, match="gibt es in Paperless nicht"):
|
||||
pull_paperless_original("http://paper.example", "token", "15", 1000)
|
||||
assert calls == ["/api/documents/15/download/"]
|
||||
|
||||
|
||||
def test_handover_package_follows_the_confirmed_selection(client, db, monkeypatch):
|
||||
raw = _eml(
|
||||
"<export-brief@example.test>",
|
||||
"Der vollständige Brieftext bleibt erhalten.",
|
||||
[("lage.pdf", PDF_A), ("lage.pdf", PDF_B)],
|
||||
)
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: raw)
|
||||
monkeypatch.setattr(
|
||||
"app.retrieval.pull_paperless_original",
|
||||
lambda *args, **kwargs: (PDF_B, "application/pdf", "archiv.pdf"),
|
||||
)
|
||||
ada = Actor(client, "ada-export@example.test", "Ada Export")
|
||||
register(db, ada.email, ada.name)
|
||||
ada.login()
|
||||
matter = ada.request("POST", "/api/v1/matters", json={"title": "Schaden"}, status=201)
|
||||
vertrag = ada.request("POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Vertrag"}, status=201)
|
||||
schrift = ada.request("POST", f"/api/v1/matters/{matter['id']}/chapters", json={"title": "Schrift"}, status=201)
|
||||
note = ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={
|
||||
"title": "Vertragnotiz",
|
||||
"document": document("Nur der Vertrag"),
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"chapter_id": vertrag["id"],
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/files",
|
||||
data={"title": "Einzeldatei", "context_kind": "matter", "context_id": matter["id"], "chapter_id": vertrag["id"]},
|
||||
files={"upload": ("einzel.pdf", PDF_C, "application/pdf")},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/files",
|
||||
data={"title": "Schreiben-Datei", "context_kind": "matter", "context_id": matter["id"], "chapter_id": schrift["id"]},
|
||||
files={"upload": ("schreiben.pdf", PDF_C, "application/pdf")},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={
|
||||
"title": "Randnotiz",
|
||||
"document": document("Randwort bleibt draussen"),
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"chapter_id": schrift["id"],
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
child = ada.request(
|
||||
"POST",
|
||||
"/api/v1/matters",
|
||||
json={"title": "Nebenpunkt", "parent_matter_id": matter["id"]},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={
|
||||
"title": "Teilnotiz",
|
||||
"document": document("Im Teilvorgang"),
|
||||
"context": {"kind": "matter", "id": child["id"]},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
mailbox = ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={
|
||||
"kind": "imap",
|
||||
"name": "Postfach",
|
||||
"host": "mail.example.test",
|
||||
"username": "ada",
|
||||
"password": "secret-pass",
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={
|
||||
"kind": "paperless",
|
||||
"name": "Archiv",
|
||||
"base_url": "http://paper.example",
|
||||
"token": "paperless-token",
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Schreiben mit Anlagen",
|
||||
"source": "imap",
|
||||
"external_id": "<export-brief@example.test>",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"detail": {"connector_id": mailbox["id"], "mailbox": "INBOX", "uid": 9},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Archivstück",
|
||||
"source": "paperless",
|
||||
"external_id": "15",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"chapter_id": vertrag["id"],
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/files",
|
||||
data={"title": "Gespiegelte Anlage", "context_kind": "matter", "context_id": matter["id"]},
|
||||
files={"upload": ("lage.pdf", PDF_A, "application/pdf")},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/files",
|
||||
data={"title": "Nur der Name", "context_kind": "matter", "context_id": matter["id"]},
|
||||
files={"upload": ("lage.pdf", PDF_C, "application/pdf")},
|
||||
status=201,
|
||||
)
|
||||
first = ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={
|
||||
"title": "Anschreiben",
|
||||
"document": document("Erstes Schreiben"),
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"communication_direction": "in",
|
||||
"dates": {"document_on": "2026-03-12"},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
reply = ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={
|
||||
"title": "Antwort",
|
||||
"document": document("Spätere Antwort"),
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"communication_direction": "out",
|
||||
"dates": {"document_on": "2026-05-18"},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/elements/{reply['id']}/references",
|
||||
json={"refers_to_element_id": first["id"], "context": {"kind": "matter", "id": matter["id"]}},
|
||||
status=204,
|
||||
)
|
||||
foreign_parent = ada.request("POST", "/api/v1/matters", json={"title": "Fremder Rahmen"}, status=201)
|
||||
foreign = ada.request(
|
||||
"POST",
|
||||
"/api/v1/matters",
|
||||
json={"title": "Fremder Vorgang", "parent_matter_id": foreign_parent["id"]},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={
|
||||
"title": "Fremdtext",
|
||||
"document": document("Nicht mitnehmen"),
|
||||
"context": {"kind": "matter", "id": foreign["id"]},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/cross-links",
|
||||
json={
|
||||
"target_matter_id": foreign["id"],
|
||||
"target_context": {"kind": "matter", "id": foreign_parent["id"]},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
detail = ada.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
note_binding = next(item["id"] for item in detail["bindings"] if item["element"]["title"] == "Vertragnotiz")
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/bindings/{note_binding}/annotations",
|
||||
json={"body": "Kontextvermerk speziell"},
|
||||
status=201,
|
||||
)
|
||||
tree = ada.request("GET", f"/api/v1/matters/{matter['id']}/export-tree")["tree"]
|
||||
flat = list(_walk(tree))
|
||||
parents = _parents(tree)
|
||||
assert "Fremdtext" not in json.dumps(tree)
|
||||
by_title = {}
|
||||
for node in flat:
|
||||
by_title.setdefault(node["title"], []).append(node)
|
||||
schrift_ids = {
|
||||
node["id"]
|
||||
for node in flat
|
||||
if node["kind"] in {"binding", "annotation"} and _under(node["id"], "Schrift", parents)
|
||||
}
|
||||
selected = {node["id"] for node in flat if node["kind"] in {"binding", "annotation"} and node["exportable"]}
|
||||
selected -= schrift_ids
|
||||
selected.add(next(node["id"] for node in by_title["Schreiben-Datei"]))
|
||||
mirrored = next(node["id"] for node in by_title["Gespiegelte Anlage"])
|
||||
name_only = next(node["id"] for node in by_title["Nur der Name"])
|
||||
selected.discard(mirrored)
|
||||
selected.discard(name_only)
|
||||
body = {"context": {"kind": "matter", "id": matter["id"]}, "selected": sorted(selected)}
|
||||
review = ada.request("POST", f"/api/v1/matters/{matter['id']}/export/review", json=body)
|
||||
assert review["ready"] is True
|
||||
assert any("bleibt Bestandteil" in item["message"] for item in review["notices"])
|
||||
assert all("Nur der Name" not in item["message"] for item in review["notices"])
|
||||
response = _post_zip(ada, matter["id"], body)
|
||||
assert response.status_code == 200, response.text
|
||||
archive = _archive(response.content)
|
||||
names = archive.namelist()
|
||||
assert "index.html" in names and "inhalt.md" in names
|
||||
assert any(name.startswith("01 Vertrag/") for name in names)
|
||||
schrift_names = [name for name in names if name.startswith("02 Schrift/")]
|
||||
assert schrift_names == ["02 Schrift/schreiben.pdf"]
|
||||
assert any("Nebenpunkt" in name and name.endswith("Teilnotiz.md") for name in names)
|
||||
mail_names = [name for name in names if "Schreiben mit Anlagen" in name]
|
||||
assert any(name.endswith("/nachricht.eml") for name in mail_names)
|
||||
assert sorted(name.rsplit("/", 1)[-1] for name in mail_names if "/anlagen/" in name) == ["lage (2).pdf", "lage.pdf"]
|
||||
assert archive.read(next(name for name in mail_names if name.endswith("/anlagen/lage.pdf"))) == PDF_A
|
||||
assert archive.read(next(name for name in mail_names if name.endswith("/anlagen/lage (2).pdf"))) == PDF_B
|
||||
whole = _joined(archive)
|
||||
assert "Der vollständige Brieftext bleibt erhalten." in whole
|
||||
assert "Randwort bleibt draussen" not in whole
|
||||
assert "Gespiegelte Anlage" not in whole
|
||||
assert "Fremdtext" not in whole
|
||||
assert "bleibt Bestandteil" in whole
|
||||
note_file = next(name for name in names if name.endswith("/Vertragnotiz.md"))
|
||||
annotation_file = next(name for name in names if "Anmerkung zu Vertragnotiz" in name)
|
||||
assert "Kontextvermerk speziell" not in archive.read(note_file).decode("utf-8")
|
||||
assert "Kontextvermerk speziell" in archive.read(annotation_file).decode("utf-8")
|
||||
assert "Anschreiben" in whole and "Antwort auf" in whole
|
||||
assert "Erzeugt am" in archive.read("index.html").decode("utf-8")
|
||||
assert "Fachdatum: 2026-03-12" in whole
|
||||
assert "Nur der Vertrag" in whole
|
||||
_links_resolve(archive)
|
||||
reply_only = {
|
||||
"context": body["context"],
|
||||
"selected": [node["id"] for node in by_title["Antwort"]],
|
||||
}
|
||||
narrowed = _post_zip(ada, matter["id"], reply_only)
|
||||
assert narrowed.status_code == 200, narrowed.text
|
||||
assert "Anschreiben" not in _joined(_archive(narrowed.content))
|
||||
changed = ada.request(
|
||||
"PATCH",
|
||||
f"/api/v1/notes/{note['id']}",
|
||||
json={
|
||||
"expected_version": note["version"],
|
||||
"document": document("Neutext der Notiz"),
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
},
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={
|
||||
"title": "Später ergänzt",
|
||||
"document": document("Darf nicht still hinein"),
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
assert "Neutext der Notiz" not in whole
|
||||
assert "Später ergänzt" not in whole
|
||||
again = _post_zip(ada, matter["id"], body)
|
||||
assert again.status_code == 200, again.text
|
||||
second = _joined(_archive(again.content))
|
||||
assert "Neutext der Notiz" in second
|
||||
assert "Später ergänzt" not in second
|
||||
assert changed["version"] == note["version"] + 1
|
||||
|
||||
|
||||
def test_export_names_gaps_and_hides_unreadable_counterparts(client, db, monkeypatch):
|
||||
ada = Actor(client, "ada-gap@example.test", "Ada Gap")
|
||||
ben = Actor(client, "ben-gap@example.test", "Ben Gap")
|
||||
register(db, ada.email, ada.name)
|
||||
register(db, ben.email, ben.name)
|
||||
ada.login()
|
||||
case = ada.request("POST", "/api/v1/cases", json={"title": "Freigabeakte"}, status=201)
|
||||
matter = ada.request("POST", "/api/v1/matters", json={"title": "Geteilt"}, status=201)
|
||||
ada.request("POST", f"/api/v1/matters/{matter['id']}/case-links", json={"case_id": case["id"]}, status=201)
|
||||
visible = ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={"title": "Sichtbare Notiz", "document": document("Sichtbar"), "context": {"kind": "matter", "id": matter["id"]}},
|
||||
status=201,
|
||||
)
|
||||
hidden = ada.request(
|
||||
"POST",
|
||||
"/api/v1/notes",
|
||||
json={"title": "GeheimtitelXQ", "document": document("Verborgen"), "context": {"kind": "matter", "id": matter["id"]}},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
f"/api/v1/elements/{visible['id']}/references",
|
||||
json={"refers_to_element_id": hidden["id"], "context": {"kind": "matter", "id": matter["id"]}},
|
||||
status=204,
|
||||
)
|
||||
ada.request(
|
||||
"PUT",
|
||||
"/api/v1/exceptions",
|
||||
json={
|
||||
"resource_kind": "element",
|
||||
"resource_id": hidden["id"],
|
||||
"context": {"kind": "case", "id": case["id"]},
|
||||
"remove_read": True,
|
||||
},
|
||||
)
|
||||
first = ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={"kind": "imap", "name": "Alt", "host": "mail.example.test", "username": "ada", "password": "secret-pass"},
|
||||
status=201,
|
||||
)
|
||||
second = ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={"kind": "imap", "name": "Neu", "host": "mail.example.test", "username": "ada", "password": "secret-pass"},
|
||||
status=201,
|
||||
)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Verwaiste Post",
|
||||
"source": "imap",
|
||||
"external_id": "<stored@example.test>",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"detail": {"connector_id": first["id"], "mailbox": "INBOX", "uid": 3},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
ada.request("DELETE", f"/api/v1/connectors/{first['id']}", status=204)
|
||||
ada.request(
|
||||
"POST",
|
||||
"/api/v1/connectors",
|
||||
json={"kind": "imap", "name": "Weiter", "host": "mail.example.test", "username": "ada", "password": "secret-pass"},
|
||||
status=201,
|
||||
)
|
||||
calls = []
|
||||
|
||||
def pull(*args, **kwargs):
|
||||
calls.append(args)
|
||||
return _eml("<stored@example.test>", "darf nicht ersatzweise kommen", [])
|
||||
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", pull)
|
||||
context = {"kind": "case", "id": case["id"]}
|
||||
tree = ada.request(
|
||||
"GET",
|
||||
f"/api/v1/matters/{matter['id']}/export-tree",
|
||||
params={"context_kind": "case", "context_id": case["id"]},
|
||||
)["tree"]
|
||||
assert "GeheimtitelXQ" not in json.dumps(tree)
|
||||
leaves = [node for node in _walk(tree) if node["kind"] in {"binding", "annotation"}]
|
||||
visible_node = next(node for node in leaves if node["title"] == "Sichtbare Notiz")
|
||||
orphan = next(node for node in leaves if node["title"] == "Verwaiste Post")
|
||||
review = ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
json={"context": context, "selected": [visible_node["id"], orphan["id"]]},
|
||||
)
|
||||
assert review["ready"] is False
|
||||
assert any(item["code"] == "reference" for item in review["issues"])
|
||||
assert any(item["code"] == "missing" for item in review["issues"])
|
||||
blob = json.dumps(review)
|
||||
assert "GeheimtitelXQ" not in blob
|
||||
assert calls == []
|
||||
assert second["id"] not in blob
|
||||
failed = _post_zip(ada, matter["id"], {"context": context, "selected": [visible_node["id"], orphan["id"]]})
|
||||
assert failed.status_code == 422
|
||||
assert "GeheimtitelXQ" not in failed.text
|
||||
assert failed.headers["content-type"].startswith("application/problem+json") or "problem" in failed.headers["content-type"]
|
||||
|
||||
ambiguous = ada.request(
|
||||
"POST",
|
||||
"/api/v1/external-refs",
|
||||
json={
|
||||
"title": "Ohne Zugang",
|
||||
"source": "imap",
|
||||
"external_id": "<offen@example.test>",
|
||||
"context": {"kind": "matter", "id": matter["id"]},
|
||||
"detail": {"mailbox": "INBOX", "uid": 8},
|
||||
},
|
||||
status=201,
|
||||
)
|
||||
del ambiguous
|
||||
tree = ada.request(
|
||||
"GET",
|
||||
f"/api/v1/matters/{matter['id']}/export-tree",
|
||||
params={"context_kind": "case", "context_id": case["id"]},
|
||||
)["tree"]
|
||||
open_mail = next(node for node in _walk(tree) if node["title"] == "Ohne Zugang")
|
||||
pending = ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
json={"context": context, "selected": [open_mail["id"]]},
|
||||
)
|
||||
assert pending["issues"][0]["code"] == "ambiguous"
|
||||
assert {item["name"] for item in pending["issues"][0]["choices"]} == {"Neu", "Weiter"}
|
||||
monkeypatch.setattr("app.retrieval.EXPORT_BYTE_LIMIT", 40)
|
||||
huge = _eml("<offen@example.test>", "GEKÜRZT-VERBOTEN " * 20, [])
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: huge)
|
||||
assigned = ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
json={"context": context, "selected": [open_mail["id"]], "assignments": {open_mail["id"]: second["id"]}},
|
||||
)
|
||||
assert assigned["issues"][0]["code"] == "too_large"
|
||||
assert "GEKÜRZT-VERBOTEN" not in json.dumps(assigned)
|
||||
wrong = _eml("<andere@example.test>", "FALSCHE-NACHRICHT", [])
|
||||
monkeypatch.setattr("app.retrieval.EXPORT_BYTE_LIMIT", 80 * 1024 * 1024)
|
||||
monkeypatch.setattr("app.retrieval.pull_rfc822", lambda *args, **kwargs: wrong)
|
||||
identity = ada.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
json={"context": context, "selected": [open_mail["id"]], "assignments": {open_mail["id"]: second["id"]}},
|
||||
)
|
||||
assert identity["issues"][0]["code"] == "identity"
|
||||
assert "FALSCHE-NACHRICHT" not in json.dumps(identity)
|
||||
blocked = _post_zip(
|
||||
ada,
|
||||
matter["id"],
|
||||
{"context": context, "selected": [open_mail["id"]], "assignments": {open_mail["id"]: second["id"]}},
|
||||
)
|
||||
assert blocked.status_code == 422
|
||||
assert "FALSCHE-NACHRICHT" not in blocked.text
|
||||
|
||||
grant(ada, case["id"], ben.email, share=False)
|
||||
ben.login()
|
||||
ben_tree = ben.request(
|
||||
"GET",
|
||||
f"/api/v1/matters/{matter['id']}/export-tree",
|
||||
params={"context_kind": "case", "context_id": case["id"]},
|
||||
)["tree"]
|
||||
assert "GeheimtitelXQ" not in json.dumps(ben_tree)
|
||||
shared = next(node for node in _walk(ben_tree) if node["title"] == "Sichtbare Notiz")
|
||||
assert shared["exportable"] is False
|
||||
denied = ben.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
json={"context": context, "selected": [shared["id"]]},
|
||||
)
|
||||
assert denied["issues"][0]["code"] == "rights"
|
||||
ada.login()
|
||||
owner_direct = ada.request("GET", f"/api/v1/matters/{matter['id']}")
|
||||
hidden_binding = next(item["id"] for item in owner_direct["bindings"] if item["element"]["title"] == "GeheimtitelXQ")
|
||||
ben.login()
|
||||
missing = ben.request(
|
||||
"POST",
|
||||
f"/api/v1/matters/{matter['id']}/export/review",
|
||||
json={"context": context, "selected": [f"binding:{hidden_binding}"]},
|
||||
status=422,
|
||||
)
|
||||
assert "GeheimtitelXQ" not in json.dumps(missing)
|
||||
assert "unbekannten oder nicht sichtbaren" in missing["detail"]
|
||||
Reference in new issue
Block a user