Warne vor bereits eingefügten Quelldokumenten und biete Nicht einfügen, erneut einfügen oder zusätzliches Einbinden an.
Deploy Development / deploy (push) Successful in 44s

Prüfstufen hier, darin, Umfeld stehen als Kontoeinstellung bereit. Der Server antwortet bei einem sicheren Treffer mit 409 und bindet auf Wunsch das vorhandene Element zusätzlich ein.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
StommerandClaude Sonnet 5.5 committed 2026-10-25 10:06:52 +01:00
1 parent a6dc24d8fe
commit f925661937
19 files changed
+748 -133

No files matched your search

+55 -1
View File
@@ -36,6 +36,8 @@ from app.access.policy import (
from app.content.documents import clean_document
from app.core.errors import forbidden, not_found, unprocessable, unsupported
from app.core.timeutil import utcnow
from app.preferences import effective_preference
from app.duplicates import already_present, find_existing
from app.media.port import LEGACY_PROFILE, StoredHandle, adapter_for, instance_for_write, publish, removal_for
from app.media.storage import display_filename, sha256, sniff
@@ -1142,7 +1144,7 @@ def add_annotation(conn, actor: UUID, binding_id: UUID, body: str, context: Ctx
return row
def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, external_id: str, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, detail: dict | None = None, communication_direction: str | None = None) -> dict:
def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str, external_id: str, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, detail: dict | None = None, communication_direction: str | None = None, duplicate: str = "insert") -> dict:
ensure_can_create(conn, actor, scope_id)
from app.sourceregistry import accepts_reference
@@ -1150,6 +1152,13 @@ def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str,
raise unprocessable("Diese Quelle ist nicht registriert.")
target, rights_context = _placement(context, placement)
require_container_edit(conn, actor, target, rights_context)
if duplicate == "ask":
# The level is the account's choice. Only a certain match warns: an unclear connection never blocks.
connector = (detail or {}).get("connector_id")
level = effective_preference(conn, actor, "duplicate_check")
found = [item for item in find_existing(conn, actor, source, external_id, str(connector) if connector else None, target, rights_context, level) if item["certain"]]
if found:
raise already_present(found)
element = _new_element(conn, actor, scope_id, title, "external_ref")
conn.execute(
"INSERT INTO external_references (element_id, source, external_id, detail) VALUES (%s, %s, %s, %s)",
@@ -1167,6 +1176,51 @@ def create_external(conn, actor: UUID, scope_id: UUID, title: str, source: str,
return element | {"binding_id": binding["id"]}
def existing_externals(conn, actor: UUID, context: Ctx, placement: Ctx | None, items: list[dict]) -> dict:
"""For each document: where it is already present around the target, at the account's check level."""
target, rights_context = _placement(context, placement)
require_container_edit(conn, actor, target, rights_context)
level = effective_preference(conn, actor, "duplicate_check")
return {
"level": level,
"items": [
item
| {"matches": find_existing(conn, actor, item["source"], item["external_id"], item.get("connector_id"), target, rights_context, level)}
for item in items
],
}
def bind_existing_external(conn, actor: UUID, scope_id: UUID, element_id: UUID, context: Ctx, placement: Ctx | None, chapter_id: UUID | None) -> dict:
"""Bind an element that already holds the document into a further container: one element, one more binding.
The element must be a certain match around the target, readable for the actor, and the actor may share it
there. Nothing is copied and nothing is handed over: rights stay those of each context.
"""
ensure_can_create(conn, actor, scope_id)
target, rights_context = _placement(context, placement)
require_container_edit(conn, actor, target, rights_context)
ref = conn.execute(
"SELECT source, external_id, connector_id, detail FROM external_references WHERE element_id = %s",
(element_id,),
).fetchone()
if ref is None:
raise not_found()
connector = ref["connector_id"] or (ref["detail"] or {}).get("connector_id")
matches = find_existing(conn, actor, ref["source"], ref["external_id"], str(connector) if connector else None, target, rights_context, "around")
match = next((item for item in matches if item["element_id"] == str(element_id) and item["certain"]), None)
if match is None:
raise not_found()
if match["where"] == "here":
raise unprocessable("Das Dokument ist hier bereits eingebunden.")
if not match["can_bind"]:
raise forbidden("Du darfst dieses Element nicht in einen weiteren Zusammenhang einbinden.")
element = get_element(conn, element_id)
binding = insert_binding(conn, element_id, target.kind, target.id, chapter_id, actor)
audit(conn, actor, scope_id, "external_ref_bound", "element", element_id, rights_context or target)
return element | {"binding_id": binding["id"]}
def create_file(conn, actor: UUID, scope_id: UUID, title: str, filename: str, data: bytes, context: Ctx, placement: Ctx | None, chapter_id: UUID | None, document_on, sent_on, received_on, media_root: str, max_bytes: int, rollback_files: list[Path], communication_direction: str | None = None) -> dict:
ensure_can_create(conn, actor, scope_id)
if not data or len(data) > max_bytes: