Compare commits
11
Commits
85c0bd1f39
..
V1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
363ecdfd0a | ||
|
|
4bd2d047f5 | ||
|
|
f925661937 | ||
|
|
a6dc24d8fe | ||
|
|
bee48bf4a5 | ||
|
|
6e1b0916a6 | ||
|
|
ffdaf1c721 | ||
|
|
0f2b7a8fab | ||
|
|
38406d3293 | ||
|
|
2f7de33049 | ||
|
|
5e32ba547d |
No files matched your search
+38
-10
@@ -1,16 +1,15 @@
|
||||
name: Test Suite
|
||||
|
||||
# One run per push. The deploy starts at the same time and updates the checkout this suite tests,
|
||||
# so the jobs wait until that checkout contains the pushed commit. A workflow_run trigger
|
||||
# (after the deploy) did not start reliably on this Gitea, see git log of this file.
|
||||
on:
|
||||
push:
|
||||
branches: [develop]
|
||||
workflow_run:
|
||||
workflows: ["Deploy Development"]
|
||||
types: [completed]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
pytest-backend:
|
||||
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: pytest against Dev Postgres (tsumugu_test)
|
||||
@@ -19,6 +18,19 @@ jobs:
|
||||
APP_DIR="/home/lars/docker/tsumugu-dev"
|
||||
COMPOSE_FILE="docker-compose.dev-env.yml"
|
||||
echo "pytest uses Dev Postgres (${APP_DIR}); event=${{ github.event_name }}"
|
||||
if [ "${{ github.event_name }}" = "push" ]; then
|
||||
for i in $(seq 1 180); do
|
||||
if git -c safe.directory='*' -C "$APP_DIR" merge-base --is-ancestor "${{ github.sha }}" HEAD 2>/dev/null; then
|
||||
echo "Dev checkout contains ${{ github.sha }} (attempt $i)"
|
||||
break
|
||||
fi
|
||||
if [ "$i" -eq 180 ]; then
|
||||
echo "the deploy did not reach ${{ github.sha }} within 15 minutes"
|
||||
exit 1
|
||||
fi
|
||||
sleep 5
|
||||
done
|
||||
fi
|
||||
cd "$APP_DIR"
|
||||
for i in $(seq 1 60); do
|
||||
if [ -f "$APP_DIR/backend/pytest.ini" ] && docker compose -f "$COMPOSE_FILE" exec -T backend true 2>/dev/null; then
|
||||
@@ -59,27 +71,43 @@ jobs:
|
||||
'
|
||||
|
||||
smoke-dev:
|
||||
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
|
||||
# After pytest, so the deploy that started with the push has normally finished. The health check retries.
|
||||
needs: pytest-backend
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Non-destructive smoke against the running Dev app
|
||||
run: |
|
||||
set -e
|
||||
echo "live Dev API (tsumugu_dev), read-only health"
|
||||
curl -sf http://localhost:8094/api/health
|
||||
echo
|
||||
curl -sf http://localhost:3094/api/health
|
||||
echo
|
||||
for url in http://localhost:8094/api/health http://localhost:3094/api/health; do
|
||||
for i in $(seq 1 30); do
|
||||
if curl -sf "$url"; then echo; break; fi
|
||||
if [ "$i" -eq 30 ]; then echo "no health answer from $url"; exit 1; fi
|
||||
sleep 10
|
||||
done
|
||||
done
|
||||
echo "LAN smoke OK. Public URL after TLS: https://dev.tsumugu.jinkendo.de"
|
||||
|
||||
frontend-build:
|
||||
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Frontend production build on Dev checkout
|
||||
run: |
|
||||
set -e
|
||||
APP_DIR="/home/lars/docker/tsumugu-dev"
|
||||
if [ "${{ github.event_name }}" = "push" ]; then
|
||||
for i in $(seq 1 180); do
|
||||
if git -c safe.directory='*' -C "$APP_DIR" merge-base --is-ancestor "${{ github.sha }}" HEAD 2>/dev/null; then
|
||||
echo "Dev checkout contains ${{ github.sha }} (attempt $i)"
|
||||
break
|
||||
fi
|
||||
if [ "$i" -eq 180 ]; then
|
||||
echo "the deploy did not reach ${{ github.sha }} within 15 minutes"
|
||||
exit 1
|
||||
fi
|
||||
sleep 5
|
||||
done
|
||||
fi
|
||||
cd "$APP_DIR/frontend"
|
||||
npm ci
|
||||
npm run build
|
||||
@@ -0,0 +1,52 @@
|
||||
"""Placing a source document into a container: a new reference, or one more binding of an existing element."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import JSONResponse
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import Ctx
|
||||
from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.schemas import ExistingIn, ExternalIn
|
||||
from app.ops import bind_existing_external, create_external, existing_externals
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _ctx_model(value) -> Ctx | None:
|
||||
return None if value is None else Ctx(value.kind, value.id)
|
||||
|
||||
|
||||
def place_external(conn, principal: Principal, body: ExternalIn) -> dict:
|
||||
context = Ctx(body.context.kind, body.context.id)
|
||||
placement = _ctx_model(body.placement)
|
||||
if body.bind_element_id is not None:
|
||||
return bind_existing_external(conn, principal.account_id, principal.scope_id, body.bind_element_id, context, placement, body.chapter_id)
|
||||
return create_external(
|
||||
conn,
|
||||
principal.account_id,
|
||||
principal.scope_id,
|
||||
body.title,
|
||||
body.source,
|
||||
body.external_id,
|
||||
context,
|
||||
placement,
|
||||
body.chapter_id,
|
||||
body.dates.document_on,
|
||||
body.dates.sent_on,
|
||||
body.dates.received_on,
|
||||
body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None,
|
||||
duplicate=body.duplicate,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/api/v1/external-refs/existing")
|
||||
def existing(body: ExistingIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
"""Read only: which of these documents are already present around the target. Nothing is written."""
|
||||
items = [
|
||||
{"source": item.source, "external_id": item.external_id, "connector_id": str(item.connector_id) if item.connector_id else None}
|
||||
for item in body.items
|
||||
]
|
||||
result = existing_externals(conn, principal.account_id, Ctx(body.context.kind, body.context.id), _ctx_model(body.placement), items)
|
||||
return JSONResponse(result, headers={"Cache-Control": "no-store"})
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Account preferences: read the effective value, change the own value, bind the team."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi.responses import JSONResponse
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import audit
|
||||
from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.schemas import PreferenceIn
|
||||
from app.preferences import preference_state, store_preference
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
_NO_STORE = {"Cache-Control": "no-store"}
|
||||
|
||||
|
||||
@router.get("/api/v1/auth/preferences/{key}")
|
||||
def get_preference(key: str, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
return JSONResponse(preference_state(conn, principal.account_id, key), headers=_NO_STORE)
|
||||
|
||||
|
||||
@router.put("/api/v1/auth/preferences/{key}")
|
||||
def put_preference(
|
||||
key: str,
|
||||
body: PreferenceIn,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
state = store_preference(conn, principal.account_id, key, body.value, body.team_mode)
|
||||
audit(
|
||||
conn,
|
||||
principal.account_id,
|
||||
None,
|
||||
"preference_changed",
|
||||
"account",
|
||||
principal.account_id,
|
||||
detail={"key": key, "value": body.value, "team_mode": body.team_mode},
|
||||
)
|
||||
return JSONResponse(state, headers=_NO_STORE)
|
||||
@@ -34,6 +34,7 @@ from app.access.policy import (
|
||||
)
|
||||
from app.access.limits import ensure_can_add_member
|
||||
from app.api.deps import Principal, get_conn, get_principal, get_session
|
||||
from app.api.externals import place_external
|
||||
from app.api.schemas import (
|
||||
AcceptInviteIn,
|
||||
AnnotationIn,
|
||||
@@ -113,7 +114,6 @@ from app.ops import (
|
||||
create_chapter,
|
||||
create_cross_link,
|
||||
create_deadline,
|
||||
create_external,
|
||||
create_file,
|
||||
create_matter,
|
||||
create_note,
|
||||
@@ -860,21 +860,7 @@ def post_annotation(
|
||||
|
||||
@router.post("/api/v1/external-refs")
|
||||
def post_external(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
element = create_external(
|
||||
conn,
|
||||
principal.account_id,
|
||||
principal.scope_id,
|
||||
body.title,
|
||||
body.source,
|
||||
body.external_id,
|
||||
Ctx(body.context.kind, body.context.id),
|
||||
_ctx_model(body.placement),
|
||||
body.chapter_id,
|
||||
body.dates.document_on,
|
||||
body.dates.sent_on,
|
||||
body.dates.received_on,
|
||||
body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None,
|
||||
)
|
||||
element = place_external(conn, principal, body)
|
||||
return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]})
|
||||
|
||||
|
||||
|
||||
@@ -352,6 +352,22 @@ class ExternalIn(APIModel):
|
||||
chapter_id: UUID | None = None
|
||||
dates: DatesIn = DatesIn()
|
||||
detail: ExternalDetail | None = None
|
||||
# "ask": a document that is already present at the account's check level answers 409.
|
||||
# "insert": the user chose to insert it again. `bind_element_id` binds that element instead of creating one.
|
||||
duplicate: Literal["ask", "insert"] = "ask"
|
||||
bind_element_id: UUID | None = None
|
||||
|
||||
|
||||
class ExistingItemIn(APIModel):
|
||||
source: str = Field(min_length=1, max_length=40)
|
||||
external_id: str = Field(min_length=1, max_length=200)
|
||||
connector_id: UUID | None = None
|
||||
|
||||
|
||||
class ExistingIn(APIModel):
|
||||
context: ContextIn
|
||||
placement: ContextIn | None = None
|
||||
items: list[ExistingItemIn] = Field(min_length=1, max_length=100)
|
||||
|
||||
|
||||
class ConnectorIn(APIModel):
|
||||
@@ -521,6 +537,13 @@ class DispatchCorrectIn(APIModel):
|
||||
clear_counterpart: bool = False
|
||||
|
||||
|
||||
class PreferenceIn(APIModel):
|
||||
"""`value` replaces the own value; null means: inherit. `team_mode` is for main users and stays as it is when absent."""
|
||||
|
||||
value: str | None = Field(default=None, max_length=40)
|
||||
team_mode: Literal["free", "locked"] | None = None
|
||||
|
||||
|
||||
class DownloadPolicyIn(APIModel):
|
||||
"""Who may save or open the original outside of Tsumugu. `direct` only applies to the account default."""
|
||||
|
||||
|
||||
+29
-87
@@ -4,19 +4,19 @@ from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi.responses import JSONResponse, Response
|
||||
from psycopg import Connection
|
||||
|
||||
from app.access.policy import Ctx, element_rights, get_element, parse_query_context
|
||||
from app.access.policy import element_rights, get_element, parse_query_context
|
||||
from app.api.deps import Principal, get_conn, get_principal
|
||||
from app.api.routes import _created, _ctx_model
|
||||
from app.api.routes import _created
|
||||
from app.api.schemas import ConnectorIn, ExternalIn, SourceAssignmentIn
|
||||
from app.core.errors import DOWNLOAD_REFUSED, forbidden, not_found, unprocessable, unsupported
|
||||
from app.mailpack import _inline_images, parse_mail, sanitize_html
|
||||
from app.remoteimages import RemoteImages
|
||||
from app.media.storage import content_disposition
|
||||
from app.ops import create_external
|
||||
from app.api.externals import place_external
|
||||
from app.retrieval import RetrievalFailure
|
||||
from app.sourcebind import assign_reference
|
||||
from app.sourceport import capability_payload, with_capabilities
|
||||
@@ -28,7 +28,6 @@ from app.sources import (
|
||||
list_connectors,
|
||||
owned_connector,
|
||||
paperless_content,
|
||||
paperless_thumbnail,
|
||||
parse_connector,
|
||||
probe,
|
||||
save_connector,
|
||||
@@ -265,55 +264,23 @@ def _mail_bytes(data: bytes, media_type: str, filename: str, inline: bool = Fals
|
||||
)
|
||||
|
||||
|
||||
class SearchFilters:
|
||||
"""The query parameters every search route shares. A source answers only those it declares."""
|
||||
# These belong to the route. Every other query parameter is a filter the registered module must declare.
|
||||
_ROUTE_PARAMS = frozenset({"q", "connector_id", "mailbox", "before", "uidvalidity"})
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
mode: str = "",
|
||||
title: str = "",
|
||||
correspondent: str = "",
|
||||
document_type: str = "",
|
||||
storage_path: str = "",
|
||||
tag: str = "",
|
||||
tag_mode: str = "",
|
||||
document_from: str = "",
|
||||
document_to: str = "",
|
||||
added_from: str = "",
|
||||
added_to: str = "",
|
||||
asn: str = "",
|
||||
custom: str = "",
|
||||
sort: str = "",
|
||||
cursor: str = "",
|
||||
):
|
||||
self.values = {
|
||||
"mode": mode,
|
||||
"title": title,
|
||||
"correspondent": correspondent,
|
||||
"document_type": document_type,
|
||||
"storage_path": storage_path,
|
||||
"tag": tag,
|
||||
"tag_mode": tag_mode,
|
||||
"document_from": document_from,
|
||||
"document_to": document_to,
|
||||
"added_from": added_from,
|
||||
"added_to": added_to,
|
||||
"asn": asn,
|
||||
"custom": custom,
|
||||
"sort": sort,
|
||||
"cursor": cursor,
|
||||
}
|
||||
|
||||
def _filters(request: Request) -> dict:
|
||||
return {name: value for name, value in request.query_params.items() if name not in _ROUTE_PARAMS}
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/paperless/documents")
|
||||
def search_paperless(
|
||||
request: Request,
|
||||
q: str = "",
|
||||
connector_id: UUID | None = None,
|
||||
filters: SearchFilters = Depends(),
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
return _run_search("paperless", conn, principal.account_id, q.strip(), connector_id, "", None, filters.values)
|
||||
return _run_search("paperless", conn, principal.account_id, q.strip(), connector_id, "", None, _filters(request))
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/{kind}/facets")
|
||||
@@ -344,14 +311,18 @@ def preview_paperless_document(
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/paperless/documents/{document_id}/thumb")
|
||||
def thumbnail_paperless_document(
|
||||
document_id: str,
|
||||
@router.get("/api/v1/sources/{kind}/thumbnail")
|
||||
def thumbnail_source(
|
||||
kind: str,
|
||||
connector_id: UUID,
|
||||
item: str,
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
data, media = paperless_thumbnail(conn, principal.account_id, document_id, connector_id)
|
||||
module = require_source(kind)
|
||||
if not module.capabilities["search"].get("result_preview") or module.thumbnail is None:
|
||||
raise unsupported("Diese Quelle hat keine Vorschaubilder.")
|
||||
data, media = module.thumbnail(conn, principal.account_id, connector_id, item)
|
||||
return Response(content=data, media_type=media, headers=_PRIVATE)
|
||||
|
||||
|
||||
@@ -366,19 +337,19 @@ def list_mailboxes(
|
||||
|
||||
@router.get("/api/v1/sources/imap/messages")
|
||||
def search_mail(
|
||||
request: Request,
|
||||
connector_id: UUID,
|
||||
q: str = "",
|
||||
mailbox: str = "",
|
||||
before: int | None = None,
|
||||
uidvalidity: int | None = None,
|
||||
filters: SearchFilters = Depends(),
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
if before is not None and before < 1:
|
||||
raise unprocessable("Die Seitenangabe ist ungültig.")
|
||||
return _run_search(
|
||||
"imap", conn, principal.account_id, q.strip(), connector_id, mailbox.strip(), before, filters.values, uidvalidity
|
||||
"imap", conn, principal.account_id, q.strip(), connector_id, mailbox.strip(), before, _filters(request), uidvalidity
|
||||
)
|
||||
|
||||
|
||||
@@ -399,21 +370,7 @@ def read_mail(
|
||||
def link_source(body: ExternalIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
||||
if get_source(body.source) is None:
|
||||
raise unprocessable("Verknüpft werden registrierte Quellen.")
|
||||
element = create_external(
|
||||
conn,
|
||||
principal.account_id,
|
||||
principal.scope_id,
|
||||
body.title,
|
||||
body.source,
|
||||
body.external_id,
|
||||
Ctx(body.context.kind, body.context.id),
|
||||
_ctx_model(body.placement),
|
||||
body.chapter_id,
|
||||
body.dates.document_on,
|
||||
body.dates.sent_on,
|
||||
body.dates.received_on,
|
||||
body.detail.model_dump(mode="json", exclude_none=True) if body.detail else None,
|
||||
)
|
||||
element = place_external(conn, principal, body)
|
||||
return _created(f"/api/v1/elements/{element['id']}", {"id": str(element["id"]), "title": element["title"]})
|
||||
|
||||
|
||||
@@ -434,29 +391,14 @@ def _connector_parts(body: ConnectorIn, current: dict | None) -> tuple[str, dict
|
||||
return parse_connector(body.kind, body.name, flat, body.config, current)
|
||||
|
||||
|
||||
_SEARCH_FLAGS = {
|
||||
"mode": "mode",
|
||||
"title": "title",
|
||||
"correspondent": "correspondent",
|
||||
"document_type": "document_type",
|
||||
"storage_path": "storage_path",
|
||||
"tag": "tags",
|
||||
"tag_mode": "tag_mode",
|
||||
"document_from": "document_range",
|
||||
"document_to": "document_range",
|
||||
"added_from": "added_range",
|
||||
"added_to": "added_range",
|
||||
"asn": "asn",
|
||||
"custom": "custom_fields",
|
||||
"sort": "sort",
|
||||
"cursor": "continuation",
|
||||
}
|
||||
|
||||
|
||||
def _run_search(kind: str, conn, owner, query: str, connector_id: UUID | None, mailbox: str, before: int | None, filters: dict, uidvalidity: int | None = None) -> dict:
|
||||
module = require_source(kind)
|
||||
search = module.capabilities["search"]
|
||||
refused = [name for name, value in filters.items() if str(value or "").strip() and not search.get(_SEARCH_FLAGS.get(name, name))]
|
||||
refused = [
|
||||
name
|
||||
for name, value in filters.items()
|
||||
if str(value or "").strip() and not search.get(module.search_params.get(name, ""))
|
||||
]
|
||||
if before is not None and not search.get("continuation"):
|
||||
refused.append("before")
|
||||
if refused:
|
||||
@@ -479,18 +421,18 @@ def _run_navigate(kind: str, conn, owner, connector_id: UUID) -> dict:
|
||||
|
||||
@router.get("/api/v1/sources/{kind}/search")
|
||||
def search_source(
|
||||
request: Request,
|
||||
kind: str,
|
||||
q: str = "",
|
||||
connector_id: UUID | None = None,
|
||||
mailbox: str = "",
|
||||
before: int | None = None,
|
||||
filters: SearchFilters = Depends(),
|
||||
principal: Principal = Depends(get_principal),
|
||||
conn: Connection = Depends(get_conn),
|
||||
):
|
||||
if before is not None and before < 1:
|
||||
raise unprocessable("Die Seitenangabe ist ungültig.")
|
||||
return _run_search(kind, conn, principal.account_id, q.strip(), connector_id, mailbox.strip(), before, filters.values)
|
||||
return _run_search(kind, conn, principal.account_id, q.strip(), connector_id, mailbox.strip(), before, _filters(request))
|
||||
|
||||
|
||||
@router.get("/api/v1/sources/{kind}/navigate")
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
"""Documents of a source that are inserted into a container again.
|
||||
|
||||
The same document in different Vorgänge stays allowed. This module only finds where a document is
|
||||
already present around the target, so the dialog can warn and the user can decide. Levels (account
|
||||
preference `duplicate_check`):
|
||||
|
||||
- here: bound directly to the target.
|
||||
- inside: also what the target shows: Teilvorgänge of a Vorgang, the linked Vorgänge of an Akte.
|
||||
- around: also where the target lies: its Vorgang, the Akten it is linked to, the parent Akte.
|
||||
|
||||
Only elements the actor may read are reported. A hit outside the actor's rights does not exist here.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from app.access.policy import (
|
||||
Ctx,
|
||||
case_rights,
|
||||
element_rights,
|
||||
get_case,
|
||||
get_matter,
|
||||
matter_rights,
|
||||
)
|
||||
from app.core.errors import Problem
|
||||
|
||||
LEVELS = {"off": (), "here": ("here",), "inside": ("here", "inside"), "around": ("here", "inside", "around")}
|
||||
_ORDER = {"here": 0, "inside": 1, "around": 2}
|
||||
|
||||
|
||||
def _stored_connector(row: dict) -> str | None:
|
||||
if row["connector_id"] is not None:
|
||||
return str(row["connector_id"])
|
||||
stored = (row["detail"] or {}).get("connector_id")
|
||||
return str(stored) if stored else None
|
||||
|
||||
|
||||
def _candidates(conn, source: str, external_id: str, connector_id: str | None) -> list[dict]:
|
||||
"""Elements for the same document. `certain` is False when only one side knows the connection."""
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT e.id, e.title, e.owner_subject_id, er.connector_id, er.detail
|
||||
FROM external_references er
|
||||
JOIN elements e ON e.id = er.element_id
|
||||
WHERE er.source = %s AND er.external_id = %s AND e.deleted_at IS NULL
|
||||
""",
|
||||
(source, external_id.strip()[:200]),
|
||||
).fetchall()
|
||||
found = []
|
||||
for row in rows:
|
||||
stored = _stored_connector(row)
|
||||
if stored is not None and connector_id is not None and stored != connector_id:
|
||||
continue
|
||||
found.append(row | {"certain": stored == connector_id})
|
||||
return found
|
||||
|
||||
|
||||
def _children(conn, matter_id: UUID) -> list[UUID]:
|
||||
rows = conn.execute("SELECT id FROM matters WHERE parent_matter_id = %s AND deleted_at IS NULL", (matter_id,)).fetchall()
|
||||
return [row["id"] for row in rows]
|
||||
|
||||
|
||||
def _zones(conn, target: Ctx) -> dict[str, list[Ctx]]:
|
||||
inside: list[Ctx] = []
|
||||
around: list[Ctx] = []
|
||||
if target.kind == "matter":
|
||||
inside = [Ctx("matter", child) for child in _children(conn, target.id)]
|
||||
matter = get_matter(conn, target.id)
|
||||
anchors = [target.id]
|
||||
if matter is not None and matter["parent_matter_id"] is not None:
|
||||
around.append(Ctx("matter", matter["parent_matter_id"]))
|
||||
anchors.append(matter["parent_matter_id"])
|
||||
links = conn.execute("SELECT DISTINCT case_id FROM matter_case_links WHERE matter_id = ANY(%s)", (anchors,)).fetchall()
|
||||
around += [Ctx("case", row["case_id"]) for row in links]
|
||||
else:
|
||||
links = conn.execute("SELECT matter_id FROM matter_case_links WHERE case_id = %s", (target.id,)).fetchall()
|
||||
for row in links:
|
||||
inside.append(Ctx("matter", row["matter_id"]))
|
||||
inside += [Ctx("matter", child) for child in _children(conn, row["matter_id"])]
|
||||
case = get_case(conn, target.id)
|
||||
if case is not None and case["parent_case_id"] is not None:
|
||||
around.append(Ctx("case", case["parent_case_id"]))
|
||||
return {"here": [target], "inside": inside, "around": around}
|
||||
|
||||
|
||||
def _blocked(conn, case_id: UUID, element_id: UUID) -> bool:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT 1 FROM case_link_blocks b JOIN matter_case_links l ON l.id = b.link_id
|
||||
WHERE l.case_id = %s AND b.element_id = %s
|
||||
""",
|
||||
(case_id, element_id),
|
||||
).fetchone()
|
||||
return row is not None
|
||||
|
||||
|
||||
def _title_if_readable(conn, actor: UUID, container: Ctx) -> str | None:
|
||||
if container.kind == "case":
|
||||
case = get_case(conn, container.id)
|
||||
return case["title"] if case is not None and case_rights(conn, actor, case).read else None
|
||||
matter = get_matter(conn, container.id)
|
||||
return matter["title"] if matter is not None and matter_rights(conn, actor, matter, None).read else None
|
||||
|
||||
|
||||
def find_existing(
|
||||
conn,
|
||||
actor: UUID,
|
||||
source: str,
|
||||
external_id: str,
|
||||
connector_id: str | None,
|
||||
target: Ctx,
|
||||
rights_context: Ctx | None,
|
||||
level: str,
|
||||
) -> list[dict]:
|
||||
"""Matches of one document at the chosen level, readable by the actor, nearest zone first."""
|
||||
zones = LEVELS.get(level, ())
|
||||
if not zones:
|
||||
return []
|
||||
candidates = _candidates(conn, source, external_id, connector_id)
|
||||
if not candidates:
|
||||
return []
|
||||
layout = _zones(conn, target)
|
||||
by_element: dict[UUID, dict] = {}
|
||||
for candidate in candidates:
|
||||
bindings = conn.execute(
|
||||
"SELECT b.context_kind, b.context_id, c.title AS chapter FROM bindings b LEFT JOIN chapters c ON c.id = b.chapter_id WHERE b.element_id = %s",
|
||||
(candidate["id"],),
|
||||
).fetchall()
|
||||
for zone in zones:
|
||||
for container in layout[zone]:
|
||||
hit = next((b for b in bindings if b["context_kind"] == container.kind and b["context_id"] == container.id), None)
|
||||
if hit is None:
|
||||
continue
|
||||
if target.kind == "case" and zone == "inside" and _blocked(conn, target.id, candidate["id"]):
|
||||
continue
|
||||
seen_in = [container] if zone == "around" else [c for c in (target, rights_context) if c is not None]
|
||||
rights = [element_rights(conn, actor, candidate, ctx) for ctx in seen_in]
|
||||
if not any(r.read for r in rights):
|
||||
continue
|
||||
match = {
|
||||
"element_id": str(candidate["id"]),
|
||||
"title": candidate["title"],
|
||||
"where": zone,
|
||||
"certain": candidate["certain"],
|
||||
"chapter": hit["chapter"] if zone == "here" else None,
|
||||
"container": {
|
||||
"kind": container.kind,
|
||||
"title": _title_if_readable(conn, actor, container) if zone != "here" else None,
|
||||
},
|
||||
"can_bind": any(r.share for r in rights),
|
||||
}
|
||||
known = by_element.get(candidate["id"])
|
||||
if known is None or _ORDER[zone] < _ORDER[known["where"]]:
|
||||
by_element[candidate["id"]] = match
|
||||
return sorted(by_element.values(), key=lambda item: (_ORDER[item["where"]], item["title"]))
|
||||
|
||||
|
||||
def already_present(matches: list[dict]) -> Problem:
|
||||
where = {"here": "hier", "inside": "in diesem Bestand", "around": "im Umfeld"}[matches[0]["where"]]
|
||||
return Problem(
|
||||
409,
|
||||
"Bereits vorhanden",
|
||||
f"Dieses Dokument ist {where} schon vorhanden. Füge es nur dann erneut ein, wenn das gewollt ist.",
|
||||
"already_present",
|
||||
)
|
||||
@@ -13,7 +13,9 @@ from app.api.accounts import router as accounts_router
|
||||
from app.api.correspondence import router as correspondence_router
|
||||
from app.api.downloads import router as downloads_router
|
||||
from app.api.export import router as export_router
|
||||
from app.api.externals import router as externals_router
|
||||
from app.api.library import router as library_router
|
||||
from app.api.preferences import router as preferences_router
|
||||
from app.api.rooms import router as rooms_router
|
||||
from app.api.roomshare import router as roomshare_router
|
||||
from app.api.team import router as team_router
|
||||
@@ -130,6 +132,8 @@ def create_app() -> FastAPI:
|
||||
app.include_router(sources_router)
|
||||
app.include_router(export_router)
|
||||
app.include_router(downloads_router)
|
||||
app.include_router(preferences_router)
|
||||
app.include_router(externals_router)
|
||||
app.include_router(correspondence_router)
|
||||
return app
|
||||
|
||||
|
||||
+55
-1
@@ -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:
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
"""Account preferences with inheritance.
|
||||
|
||||
An employee inherits the value of the main user who sponsors the account and may change it,
|
||||
unless the main user made the preference binding for the team. A binding value overrides an own
|
||||
value without deleting it: it applies again once the binding is lifted.
|
||||
Allowed values, default and lockability of each key are fixed here.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
|
||||
from psycopg.types.json import Json
|
||||
|
||||
from app.core.errors import forbidden, not_found, unprocessable
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Preference:
|
||||
default: str
|
||||
allowed: tuple[str, ...]
|
||||
team_lockable: bool = True
|
||||
|
||||
|
||||
PREFERENCES: dict[str, Preference] = {
|
||||
# Where an element that is inserted again counts as already present.
|
||||
"duplicate_check": Preference("here", ("off", "here", "inside", "around")),
|
||||
}
|
||||
|
||||
|
||||
def spec(key: str) -> Preference:
|
||||
found = PREFERENCES.get(key)
|
||||
if found is None:
|
||||
raise not_found()
|
||||
return found
|
||||
|
||||
|
||||
def _valid(wanted: Preference, value) -> str | None:
|
||||
return value if isinstance(value, str) and value in wanted.allowed else None
|
||||
|
||||
|
||||
def preference_state(conn, account_id: UUID, key: str) -> dict:
|
||||
"""The effective value and where it comes from: locked, own, team or default."""
|
||||
wanted = spec(key)
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT a.account_kind,
|
||||
own.value AS own_value, own.team_mode AS own_mode,
|
||||
sp.value AS team_value, sp.team_mode AS team_mode
|
||||
FROM auth_accounts a
|
||||
LEFT JOIN account_preferences own ON own.account_id = a.id AND own.key = %s
|
||||
LEFT JOIN account_preferences sp ON sp.account_id = a.sponsor_id AND sp.key = %s
|
||||
WHERE a.id = %s
|
||||
""",
|
||||
(key, key, account_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise not_found()
|
||||
employee = row["account_kind"] == "employee"
|
||||
own = _valid(wanted, row["own_value"])
|
||||
team = _valid(wanted, row["team_value"]) if employee else None
|
||||
locked = employee and wanted.team_lockable and row["team_mode"] == "locked"
|
||||
inherited = team or wanted.default
|
||||
if locked:
|
||||
value, source = inherited, "locked"
|
||||
elif own is not None:
|
||||
value, source = own, "own"
|
||||
elif team is not None:
|
||||
value, source = team, "team"
|
||||
else:
|
||||
value, source = wanted.default, "default"
|
||||
return {
|
||||
"key": key,
|
||||
"value": value,
|
||||
"source": source,
|
||||
"own": own,
|
||||
"inherited": inherited,
|
||||
"locked": locked,
|
||||
"allowed": list(wanted.allowed),
|
||||
"default": wanted.default,
|
||||
"can_lock": row["account_kind"] == "main" and wanted.team_lockable,
|
||||
"team_mode": row["own_mode"] if row["own_mode"] and not employee else "free",
|
||||
}
|
||||
|
||||
|
||||
def effective_preference(conn, account_id: UUID, key: str) -> str:
|
||||
return preference_state(conn, account_id, key)["value"]
|
||||
|
||||
|
||||
def store_preference(conn, account_id: UUID, key: str, value: str | None, team_mode: str | None) -> dict:
|
||||
"""Own value (None: inherit) and, for a main user, the binding for the team."""
|
||||
wanted = spec(key)
|
||||
state = preference_state(conn, account_id, key)
|
||||
if value is not None and value not in wanted.allowed:
|
||||
raise unprocessable("Dieser Wert ist für die Einstellung nicht vorgesehen.")
|
||||
if team_mode is not None and not state["can_lock"]:
|
||||
raise forbidden("Nur der Hauptnutzer kann eine Einstellung für das Team verbindlich setzen.")
|
||||
if state["locked"]:
|
||||
raise forbidden("Der Hauptnutzer hat diese Einstellung für das Team vorgegeben.")
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO account_preferences (account_id, key, value, team_mode)
|
||||
VALUES (%s, %s, %s, COALESCE(%s, 'free'))
|
||||
ON CONFLICT (account_id, key) DO UPDATE
|
||||
SET value = EXCLUDED.value,
|
||||
team_mode = COALESCE(%s, account_preferences.team_mode),
|
||||
updated_at = now()
|
||||
""",
|
||||
(account_id, key, Json(value) if value is not None else None, team_mode, team_mode),
|
||||
)
|
||||
return preference_state(conn, account_id, key)
|
||||
@@ -1082,4 +1082,6 @@ LABELS = {
|
||||
"room_default_changed": "Voreinstellung des Datenraums geändert",
|
||||
"room_download_policy_changed": "Download-Regel des Datenraums geändert",
|
||||
"download_defaults_changed": "Standard für Downloads geändert",
|
||||
"preference_changed": "Einstellung des Kontos geändert",
|
||||
"external_ref_bound": "Vorhandene Referenz zusätzlich eingebunden",
|
||||
}
|
||||
@@ -1,93 +1,15 @@
|
||||
"""Declared source and editor capabilities.
|
||||
"""Declared editor capabilities and the payloads that carry them.
|
||||
|
||||
A common contract does not invent abilities the adapter does not have.
|
||||
A filter is offered only when that module implements it.
|
||||
The capabilities of a source belong to its module. A filter is offered only when that module implements it.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
PAPERLESS_CAPABILITIES = {
|
||||
"kind": "paperless",
|
||||
"navigate": False,
|
||||
"read_original": True,
|
||||
"read_preview": True,
|
||||
"read_attachment": False,
|
||||
"edit_content": False,
|
||||
"edit_metadata": False,
|
||||
"search": {
|
||||
"query": True,
|
||||
"mode": True,
|
||||
"title": True,
|
||||
"correspondent": True,
|
||||
"document_type": True,
|
||||
"storage_path": True,
|
||||
"tags": True,
|
||||
"tag_mode": True,
|
||||
"document_range": True,
|
||||
"added_range": True,
|
||||
"asn": True,
|
||||
"custom_fields": True,
|
||||
"sort": True,
|
||||
"continuation": True,
|
||||
"result_preview": True,
|
||||
},
|
||||
}
|
||||
|
||||
IMAP_CAPABILITIES = {
|
||||
"kind": "imap",
|
||||
"navigate": True,
|
||||
"read_original": True,
|
||||
"read_preview": True,
|
||||
"read_attachment": True,
|
||||
"edit_content": False,
|
||||
"edit_metadata": False,
|
||||
"search": {
|
||||
"query": True,
|
||||
"mode": False,
|
||||
"title": False,
|
||||
"correspondent": False,
|
||||
"document_type": False,
|
||||
"storage_path": False,
|
||||
"tags": False,
|
||||
"tag_mode": False,
|
||||
"document_range": False,
|
||||
"added_range": False,
|
||||
"asn": False,
|
||||
"custom_fields": False,
|
||||
"sort": False,
|
||||
"continuation": True,
|
||||
"result_preview": False,
|
||||
},
|
||||
}
|
||||
|
||||
CAPABILITIES = {
|
||||
"paperless": PAPERLESS_CAPABILITIES,
|
||||
"imap": IMAP_CAPABILITIES,
|
||||
}
|
||||
|
||||
EDITORS = {
|
||||
"note": True,
|
||||
"markdown_file": False,
|
||||
}
|
||||
|
||||
UNSUPPORTED_SEARCH = (
|
||||
"mode",
|
||||
"title",
|
||||
"correspondent",
|
||||
"document_type",
|
||||
"storage_path",
|
||||
"tag",
|
||||
"tag_mode",
|
||||
"document_from",
|
||||
"document_to",
|
||||
"added_from",
|
||||
"added_to",
|
||||
"asn",
|
||||
"custom",
|
||||
"sort",
|
||||
"cursor",
|
||||
)
|
||||
|
||||
|
||||
def capability_payload() -> dict:
|
||||
from app.sourceregistry import registered_sources
|
||||
@@ -112,10 +34,6 @@ def capability_payload() -> dict:
|
||||
}
|
||||
|
||||
|
||||
def unsupported_filters(values: dict[str, str]) -> list[str]:
|
||||
return [name for name in UNSUPPORTED_SEARCH if str(values.get(name) or "").strip()]
|
||||
|
||||
|
||||
def with_capabilities(kind: str, connector_id: str, payload: dict) -> dict:
|
||||
from app.sourceregistry import require_source
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Callable
|
||||
|
||||
from app.core.errors import unprocessable
|
||||
@@ -65,6 +65,11 @@ class SourceModule:
|
||||
navigate: Callable | None = None
|
||||
preview: Callable | None = None
|
||||
facets: Callable | None = None
|
||||
# Query parameter of a search, mapped to the capability flag that must be true for it.
|
||||
# The module owns its filter names. Anything not declared here is refused.
|
||||
search_params: dict[str, str] = field(default_factory=dict)
|
||||
# thumbnail(conn, owner_id, connector_id, item) -> (bytes, media type), when the module declares it.
|
||||
thumbnail: Callable | None = None
|
||||
|
||||
|
||||
_MODULES: dict[str, SourceModule] = {}
|
||||
|
||||
+143
-11
@@ -48,7 +48,6 @@ from app.sourceaccess import (
|
||||
connector_owner as _connector_owner,
|
||||
release_for_external,
|
||||
)
|
||||
from app.sourceport import CAPABILITIES
|
||||
from app.sourceregistry import ConfigField, SourceModule, get_source, parse_fields, register_source, require_source
|
||||
|
||||
|
||||
@@ -418,7 +417,14 @@ def paperless_documents(conn, owner_id: UUID, query: str, connector_id: UUID | N
|
||||
release_for_external(conn)
|
||||
names = _paperless_lookup(row["id"], base, row["secret"])
|
||||
params = _paperless_params(query, options, names["custom_fields"])
|
||||
payload = _paperless_get(base, row["secret"], "/api/documents/", params)
|
||||
# Observed on a real instance: all-tags together with a custom field makes Paperless answer 500.
|
||||
try:
|
||||
payload = _paperless_get(base, row["secret"], "/api/documents/", params)
|
||||
except Problem as problem:
|
||||
clash = "," in params.get("tags__id__all", "") and "custom_field_query" in params
|
||||
if clash and "HTTP 5" in problem.detail:
|
||||
raise unprocessable(f"{problem.detail} Das tritt auf, wenn „Alle gewählten Tags“ mit einem Zusatzfeld zusammentrifft. Wähle „Mindestens eines“ oder lass das Zusatzfeld weg.") from problem
|
||||
raise
|
||||
assert_still_owned(conn, row["id"], owner_id)
|
||||
terms = _search_terms(query, (options or {}).get("title"))
|
||||
items = []
|
||||
@@ -438,6 +444,7 @@ def paperless_documents(conn, owner_id: UUID, query: str, connector_id: UUID | N
|
||||
"document_type": _named(doc.get("document_type"), names["document_types"]),
|
||||
"storage_path": _named(doc.get("storage_path"), names["storage_paths"]),
|
||||
"tags": [name for name in (_named(tag, names["tags"]) for tag in doc.get("tags") or []) if name][:12],
|
||||
"custom_fields": _custom_display(doc.get("custom_fields"), names["custom_fields"]),
|
||||
"pages": pages if isinstance(pages, int) and pages > 0 else None,
|
||||
"asn": asn if isinstance(asn, int) else None,
|
||||
"snippet": _snippet(doc, terms),
|
||||
@@ -478,6 +485,27 @@ def paperless_facets(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
}
|
||||
|
||||
|
||||
_THUMBNAIL_MEDIA = frozenset({"image/png", "image/jpeg", "image/webp", "image/gif"})
|
||||
|
||||
PAPERLESS_SEARCH_PARAMS = {
|
||||
"mode": "mode",
|
||||
"title": "title",
|
||||
"correspondent": "correspondent",
|
||||
"document_type": "document_type",
|
||||
"storage_path": "storage_path",
|
||||
"tag": "tags",
|
||||
"tag_mode": "tag_mode",
|
||||
"document_from": "document_range",
|
||||
"document_to": "document_range",
|
||||
"added_from": "added_range",
|
||||
"added_to": "added_range",
|
||||
"asn": "asn",
|
||||
"custom": "custom_fields",
|
||||
"sort": "sort",
|
||||
"cursor": "continuation",
|
||||
}
|
||||
|
||||
|
||||
def paperless_thumbnail(conn, owner_id: UUID, document_id: str, connector_id: UUID) -> tuple[bytes, str]:
|
||||
if not document_id.isdigit() or len(document_id) > 12:
|
||||
raise unprocessable("Die Paperless-Nummer ist ungültig.")
|
||||
@@ -488,10 +516,19 @@ def paperless_thumbnail(conn, owner_id: UUID, document_id: str, connector_id: UU
|
||||
assert_still_owned(conn, row["id"], owner_id)
|
||||
if fetched is None:
|
||||
raise not_found()
|
||||
data, media, _name = fetched
|
||||
if not media.startswith("image/"):
|
||||
raise unprocessable("Paperless hat kein Vorschaubild geliefert.")
|
||||
return data, media
|
||||
data, header, _name = fetched
|
||||
return data, _thumbnail_media(data, header)
|
||||
|
||||
|
||||
def _thumbnail_media(data: bytes, header: str) -> str:
|
||||
"""Raster pictures only, told by their bytes. A vector file could carry markup, a header can be wrong."""
|
||||
try:
|
||||
media = sniff(data)
|
||||
except Problem:
|
||||
media = "image/gif" if data[:4] == b"GIF8" else ""
|
||||
if media not in _THUMBNAIL_MEDIA:
|
||||
raise unprocessable(f"Paperless hat kein Vorschaubild geliefert (Antworttyp {header or 'unbekannt'}).")
|
||||
return media
|
||||
|
||||
|
||||
_LOOKUP_TTL = 60.0
|
||||
@@ -555,6 +592,33 @@ def _paperless_names_or_none(base: str, token: str, path: str) -> list[dict]:
|
||||
return []
|
||||
|
||||
|
||||
_CUSTOM_SHOWN = 8
|
||||
|
||||
|
||||
def _custom_display(values, fields: list[dict]) -> list[dict]:
|
||||
"""The custom fields of a hit as name, type and readable value. Empty values and unknown fields are left out."""
|
||||
if not isinstance(values, list):
|
||||
return []
|
||||
known = {field["id"]: field for field in fields}
|
||||
shown = []
|
||||
for entry in values:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
field = known.get(str(entry.get("field")))
|
||||
value = entry.get("value")
|
||||
if field is None or value is None or value == "" or isinstance(value, list):
|
||||
continue
|
||||
if field["type"] == "boolean":
|
||||
text = "Ja" if value else "Nein"
|
||||
elif field["type"] == "select":
|
||||
text = next((option["name"] for option in field["options"] if option["id"] == str(value)), "")
|
||||
else:
|
||||
text = str(value)
|
||||
if text:
|
||||
shown.append({"name": field["name"], "type": field["type"], "value": text[:200]})
|
||||
return shown[:_CUSTOM_SHOWN]
|
||||
|
||||
|
||||
def _named(value, entries: list[dict]) -> str | None:
|
||||
if value is None or isinstance(value, bool):
|
||||
return None
|
||||
@@ -957,11 +1021,22 @@ def _paperless_get(base: str, token: str, path: str, params: dict) -> dict:
|
||||
except HTTPError as exc:
|
||||
if exc.code in {401, 403}:
|
||||
raise unprocessable("Paperless hat den Schlüssel abgelehnt.") from exc
|
||||
raise unprocessable("Paperless hat die Anfrage nicht beantwortet.") from exc
|
||||
raise unprocessable(f"Paperless hat die Anfrage nicht beantwortet ({_refusal(exc, path)}).") from exc
|
||||
except (URLError, TimeoutError, json.JSONDecodeError) as exc:
|
||||
raise unprocessable("Paperless ist nicht erreichbar.") from exc
|
||||
|
||||
|
||||
def _refusal(exc: HTTPError, path: str) -> str:
|
||||
"""Status and, when Paperless names one, its reason. The body is the owner's own instance, cut short."""
|
||||
reason = ""
|
||||
try:
|
||||
body = json.loads(exc.read(2000).decode("utf-8", "replace"))
|
||||
reason = json.dumps(body, ensure_ascii=False)[:240] if isinstance(body, (dict, list)) else ""
|
||||
except (ValueError, OSError):
|
||||
reason = ""
|
||||
return f"HTTP {exc.code} auf {path}" + (f": {reason}" if reason else "")
|
||||
|
||||
|
||||
def _correspondent(doc: dict, names: dict | None = None) -> str | None:
|
||||
value = doc.get("correspondent_name") or doc.get("correspondent")
|
||||
if isinstance(value, str) and value.strip():
|
||||
@@ -2028,11 +2103,66 @@ def _imap_bytes(conn, actor_id: UUID, element: dict, context: Ctx | None, ref) -
|
||||
return raw, "message/rfc822", "nachricht.eml"
|
||||
|
||||
|
||||
PAPERLESS_CAPABILITIES = {
|
||||
"kind": "paperless",
|
||||
"navigate": False,
|
||||
"read_original": True,
|
||||
"read_preview": True,
|
||||
"read_attachment": False,
|
||||
"edit_content": False,
|
||||
"edit_metadata": False,
|
||||
"search": {
|
||||
"query": True,
|
||||
"mode": True,
|
||||
"title": True,
|
||||
"correspondent": True,
|
||||
"document_type": True,
|
||||
"storage_path": True,
|
||||
"tags": True,
|
||||
"tag_mode": True,
|
||||
"document_range": True,
|
||||
"added_range": True,
|
||||
"asn": True,
|
||||
"custom_fields": True,
|
||||
"sort": True,
|
||||
"continuation": True,
|
||||
"result_preview": True,
|
||||
},
|
||||
}
|
||||
|
||||
IMAP_CAPABILITIES = {
|
||||
"kind": "imap",
|
||||
"navigate": True,
|
||||
"read_original": True,
|
||||
"read_preview": True,
|
||||
"read_attachment": True,
|
||||
"edit_content": False,
|
||||
"edit_metadata": False,
|
||||
"search": {
|
||||
"query": True,
|
||||
"mode": False,
|
||||
"title": False,
|
||||
"correspondent": False,
|
||||
"document_type": False,
|
||||
"storage_path": False,
|
||||
"tags": False,
|
||||
"tag_mode": False,
|
||||
"document_range": False,
|
||||
"added_range": False,
|
||||
"asn": False,
|
||||
"custom_fields": False,
|
||||
"sort": False,
|
||||
"continuation": True,
|
||||
"result_preview": False,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class _PaperlessAdapter:
|
||||
kind = "paperless"
|
||||
|
||||
def capabilities(self) -> dict:
|
||||
return CAPABILITIES[self.kind]
|
||||
return PAPERLESS_CAPABILITIES
|
||||
|
||||
def probe(self, row) -> dict:
|
||||
count = _paperless_count(row["settings"]["base_url"], row["secret"])
|
||||
@@ -2053,7 +2183,7 @@ class _ImapAdapter:
|
||||
kind = "imap"
|
||||
|
||||
def capabilities(self) -> dict:
|
||||
return CAPABILITIES[self.kind]
|
||||
return IMAP_CAPABILITIES
|
||||
|
||||
def probe(self, row) -> dict:
|
||||
_imap_login(row["settings"], row["secret"])
|
||||
@@ -2076,7 +2206,7 @@ def _install_sources() -> None:
|
||||
kind="paperless",
|
||||
label="Paperless",
|
||||
representation="pdf",
|
||||
capabilities=CAPABILITIES["paperless"],
|
||||
capabilities=PAPERLESS_CAPABILITIES,
|
||||
config_fields=PAPERLESS_FIELDS,
|
||||
normalize_config=_paperless_normalize,
|
||||
probe=paperless.probe,
|
||||
@@ -2086,12 +2216,14 @@ def _install_sources() -> None:
|
||||
navigate=None,
|
||||
preview=None,
|
||||
facets=paperless.facets,
|
||||
search_params=PAPERLESS_SEARCH_PARAMS,
|
||||
thumbnail=lambda conn, owner, connector_id, item: paperless_thumbnail(conn, owner, str(item), connector_id),
|
||||
))
|
||||
register_source(SourceModule(
|
||||
kind="imap",
|
||||
label="E-Mail",
|
||||
representation="email",
|
||||
capabilities=CAPABILITIES["imap"],
|
||||
capabilities=IMAP_CAPABILITIES,
|
||||
config_fields=IMAP_FIELDS,
|
||||
normalize_config=_imap_normalize,
|
||||
probe=imap.probe,
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Einstellungen je Konto. Ein Mitarbeiter erbt den Wert seines Hauptnutzers und kann ihn ändern,
|
||||
-- solange der Hauptnutzer die Einstellung nicht für das Team verbindlich gesetzt hat (team_mode).
|
||||
-- Erlaubte Werte, Standard und Verbindlichkeit je Schlüssel stehen im Code (app/preferences.py).
|
||||
-- value NULL: kein eigener Wert, es gilt die Erbung bzw. der Standard.
|
||||
|
||||
CREATE TABLE account_preferences (
|
||||
account_id uuid NOT NULL REFERENCES auth_accounts (id) ON DELETE CASCADE,
|
||||
key text NOT NULL,
|
||||
value jsonb,
|
||||
team_mode text NOT NULL DEFAULT 'free',
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (account_id, key),
|
||||
CONSTRAINT account_preferences_key_len CHECK (char_length(key) BETWEEN 1 AND 60),
|
||||
CONSTRAINT account_preferences_team_mode CHECK (team_mode IN ('free', 'locked'))
|
||||
);
|
||||
|
||||
-- Dieselbe Quelle und Kennung: Suche nach bereits eingefügten Dokumenten (app/duplicates.py).
|
||||
CREATE INDEX external_references_identity ON external_references (source, external_id);
|
||||
@@ -10,6 +10,9 @@ import io
|
||||
import zipfile
|
||||
from email.parser import BytesParser
|
||||
|
||||
import pytest
|
||||
|
||||
from app.core.errors import unprocessable
|
||||
from app.media.port import MemoryAdapter
|
||||
from app.sourceaccess import borrow_original, complete_borrow
|
||||
from app.sourceregistry import SourceModule, unregister_source, register_source
|
||||
@@ -472,6 +475,14 @@ def test_paperless_default_search_reads_title_and_content_and_shows_a_preview(cl
|
||||
"tags": [2, 5, 99],
|
||||
"archive_serial_number": 1042,
|
||||
"page_count": 3,
|
||||
"custom_fields": [
|
||||
{"field": 1, "value": "EUR100.50"},
|
||||
{"field": 2, "value": "2024-03-09"},
|
||||
{"field": 3, "value": "b"},
|
||||
{"field": 4, "value": True},
|
||||
{"field": 5, "value": None},
|
||||
{"field": 99, "value": "fremd"},
|
||||
],
|
||||
"content": body,
|
||||
}
|
||||
],
|
||||
@@ -482,6 +493,16 @@ def test_paperless_default_search_reads_title_and_content_and_shows_a_preview(cl
|
||||
return {"results": [{"id": 2, "name": "Miete"}, {"id": 5, "name": "Wohnung"}]}
|
||||
if path == "/api/correspondents/":
|
||||
return {"results": [{"id": 4, "name": "Hausverwaltung"}]}
|
||||
if path == "/api/custom_fields/":
|
||||
return {
|
||||
"results": [
|
||||
{"id": 1, "name": "Rechnungsbetrag", "data_type": "monetary"},
|
||||
{"id": 2, "name": "bezahlt_am", "data_type": "date"},
|
||||
{"id": 3, "name": "Status", "data_type": "select", "extra_data": {"select_options": [{"id": "b", "label": "bezahlt"}]}},
|
||||
{"id": 4, "name": "Geprüft", "data_type": "boolean"},
|
||||
{"id": 5, "name": "Notiz", "data_type": "string"},
|
||||
]
|
||||
}
|
||||
return {"results": [{"id": 8, "name": "Brief"}]}
|
||||
|
||||
monkeypatch.setattr(sources, "_paperless_get", fake_get)
|
||||
@@ -515,17 +536,31 @@ def test_paperless_default_search_reads_title_and_content_and_shows_a_preview(cl
|
||||
assert "Kündigung des Mietvertrags" in item["snippet"]
|
||||
assert len(item["snippet"]) <= 262
|
||||
assert item["snippet"].startswith("…")
|
||||
assert item["custom_fields"] == [
|
||||
{"name": "Rechnungsbetrag", "type": "monetary", "value": "EUR100.50"},
|
||||
{"name": "bezahlt_am", "type": "date", "value": "2024-03-09"},
|
||||
{"name": "Status", "type": "select", "value": "bezahlt"},
|
||||
{"name": "Geprüft", "type": "boolean", "value": "Ja"},
|
||||
]
|
||||
assert found["total"] == 1
|
||||
assert found["previous_page"] is None
|
||||
facets = ada.request("GET", "/api/v1/sources/paperless/facets", params={"connector_id": first["id"]})
|
||||
assert facets["storage_paths"] == []
|
||||
assert facets["tag_modes"] == ["all", "any", "none"]
|
||||
# A filter of another source is refused, not ignored.
|
||||
ada.request(
|
||||
"GET",
|
||||
"/api/v1/sources/imap/messages",
|
||||
params={"connector_id": first["id"], "mailbox": "INBOX", "mode": "title"},
|
||||
status=422,
|
||||
)
|
||||
for params in (
|
||||
{"mode": "regex"},
|
||||
{"tag_mode": "x"},
|
||||
{"asn": "x1"},
|
||||
{"storage_path": "a"},
|
||||
{"q": "miete", "mode": "title", "title": "vertrag"},
|
||||
{"bogus": "1"},
|
||||
):
|
||||
ada.request(
|
||||
"GET",
|
||||
@@ -559,9 +594,12 @@ def test_paperless_custom_field_filters_use_the_instance_fields(client, db, monk
|
||||
]
|
||||
}
|
||||
if path == "/api/documents/":
|
||||
if failing:
|
||||
raise unprocessable(failing[0])
|
||||
return {"count": 0, "results": []}
|
||||
return {"results": []}
|
||||
|
||||
failing = []
|
||||
monkeypatch.setattr(sources, "_paperless_get", fake_get)
|
||||
ada = Actor(client, "ada-custom@example.test", "Ada Custom")
|
||||
register(db, ada.email, ada.name)
|
||||
@@ -618,6 +656,50 @@ def test_paperless_custom_field_filters_use_the_instance_fields(client, db, monk
|
||||
params={"connector_id": first["id"], "custom": "kein json"},
|
||||
status=422,
|
||||
)
|
||||
# Paperless answers 500 for all-tags together with a custom field: the message names that pairing.
|
||||
failing.append("Paperless hat die Anfrage nicht beantwortet (HTTP 500 auf /api/documents/).")
|
||||
row = json.dumps([{"field": "2", "op": "exists", "value": True}])
|
||||
|
||||
def refused(extra):
|
||||
return ada.request(
|
||||
"GET",
|
||||
"/api/v1/sources/paperless/documents",
|
||||
params={"connector_id": first["id"], "custom": row, **extra},
|
||||
status=422,
|
||||
)
|
||||
|
||||
assert "Alle gewählten Tags" in str(refused({"tag": "1,2"}))
|
||||
assert "Alle gewählten Tags" not in str(refused({"tag": "1,2", "tag_mode": "any"}))
|
||||
assert "Alle gewählten Tags" not in str(refused({"tag": "1"}))
|
||||
|
||||
|
||||
def test_paperless_refusal_names_the_status_and_reason(monkeypatch):
|
||||
import io
|
||||
from urllib.error import HTTPError
|
||||
|
||||
import app.sources as sources
|
||||
from app.core.errors import Problem
|
||||
|
||||
def refuse(code, body):
|
||||
def fake(request, timeout=0):
|
||||
raise HTTPError(request.full_url, code, "x", {}, io.BytesIO(body))
|
||||
|
||||
return fake
|
||||
|
||||
monkeypatch.setattr(sources, "urlopen", refuse(400, '{"custom_field_query": ["Ungültig"]}'.encode()))
|
||||
with pytest.raises(Problem) as caught:
|
||||
sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {"page_size": "20"})
|
||||
assert "HTTP 400 auf /api/documents/" in caught.value.detail
|
||||
assert "custom_field_query" in caught.value.detail
|
||||
assert "token-alpha" not in caught.value.detail
|
||||
monkeypatch.setattr(sources, "urlopen", refuse(500, b"<html>Fehler</html>"))
|
||||
with pytest.raises(Problem) as caught:
|
||||
sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {})
|
||||
assert caught.value.detail.endswith("(HTTP 500 auf /api/documents/).")
|
||||
monkeypatch.setattr(sources, "urlopen", refuse(403, b"{}"))
|
||||
with pytest.raises(Problem) as caught:
|
||||
sources._paperless_get("https://paper.example.test", "token-alpha", "/api/documents/", {})
|
||||
assert "Schlüssel abgelehnt" in caught.value.detail
|
||||
|
||||
|
||||
def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeypatch):
|
||||
@@ -631,6 +713,10 @@ def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeyp
|
||||
return b"RIFFxxxxWEBP", "image/webp", ""
|
||||
if path.endswith("/8/thumb/"):
|
||||
return b"<p>", "text/html", ""
|
||||
if path.endswith("/6/thumb/"):
|
||||
return b"\x89PNG\r\n\x1a\nxxxx", "application/octet-stream", ""
|
||||
if path.endswith("/7/thumb/"):
|
||||
return b"<svg xmlns='http://www.w3.org/2000/svg'/>", "image/svg+xml", ""
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(sources, "_paperless_file", fake_file)
|
||||
@@ -643,21 +729,29 @@ def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeyp
|
||||
json={"kind": "paperless", "name": "Archiv", "base_url": "https://paper.example.test", "token": "token-alpha"},
|
||||
status=201,
|
||||
)
|
||||
url = "/api/v1/sources/paperless/documents/{}/thumb"
|
||||
ok = ada.client.get(url.format(9), params={"connector_id": first["id"]}, headers=_headers(ada))
|
||||
url = "/api/v1/sources/paperless/thumbnail"
|
||||
|
||||
def get(actor, item, kind_url=url):
|
||||
return actor.client.get(kind_url, params={"connector_id": first["id"], "item": item}, headers=_headers(actor))
|
||||
|
||||
ok = get(ada, 9)
|
||||
assert ok.status_code == 200
|
||||
assert ok.headers["content-type"] == "image/webp"
|
||||
assert ok.headers["cache-control"] == "private, no-store"
|
||||
assert seen[-1] == ("token-alpha", "/api/documents/9/thumb/")
|
||||
assert ada.client.get(url.format(8), params={"connector_id": first["id"]}, headers=_headers(ada)).status_code == 422
|
||||
assert ada.client.get(url.format(5), params={"connector_id": first["id"]}, headers=_headers(ada)).status_code == 404
|
||||
assert ada.client.get(url.format("x1"), params={"connector_id": first["id"]}, headers=_headers(ada)).status_code == 422
|
||||
typed = get(ada, 6)
|
||||
assert typed.status_code == 200 and typed.headers["content-type"] == "image/png"
|
||||
assert get(ada, 8).status_code == 422
|
||||
assert get(ada, 7).status_code == 422
|
||||
assert get(ada, 5).status_code == 404
|
||||
assert get(ada, "x1").status_code == 422
|
||||
# IMAP declares no thumbnails, and the module must declare the capability.
|
||||
assert get(ada, 9, "/api/v1/sources/imap/thumbnail").status_code == 422
|
||||
ben = Actor(client, "ben-thumb@example.test", "Ben Thumb")
|
||||
register(db, ben.email, ben.name)
|
||||
ben.login()
|
||||
stranger = ben.client.get(url.format(9), params={"connector_id": first["id"]}, headers=_headers(ben))
|
||||
assert stranger.status_code == 404
|
||||
assert len(seen) == 3
|
||||
assert get(ben, 9).status_code == 404
|
||||
assert len(seen) == 5
|
||||
|
||||
|
||||
def test_revoked_grant_hides_bytes_fetched_during_the_call(client, db, monkeypatch):
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Bereits eingefügte Quelldokumente: Vorabfrage, Warnung (409), erneutes Einfügen und zusätzliches Einbinden."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.support import matter_of
|
||||
from tests.test_team import _main
|
||||
|
||||
PREF = "/api/v1/auth/preferences/duplicate_check"
|
||||
EXISTING = "/api/v1/external-refs/existing"
|
||||
CONNECTOR = str(uuid4())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ada(client, db):
|
||||
return _main(client, db, "ada", main=True)
|
||||
|
||||
|
||||
def _ref(actor, kind, container, external_id="41", connector=CONNECTOR, status=201, **extra):
|
||||
body = {
|
||||
"title": f"Dokument {external_id}",
|
||||
"source": "paperless",
|
||||
"external_id": external_id,
|
||||
"context": {"kind": kind, "id": container},
|
||||
"detail": {"connector_id": connector},
|
||||
**extra,
|
||||
}
|
||||
return actor.request("POST", "/api/v1/external-refs", json=body, status=status)
|
||||
|
||||
|
||||
def _ask(actor, kind, container, external_id="41", connector=CONNECTOR):
|
||||
body = {
|
||||
"context": {"kind": kind, "id": container},
|
||||
"items": [{"source": "paperless", "external_id": external_id, "connector_id": connector}],
|
||||
}
|
||||
return actor.request("POST", EXISTING, json=body)["items"][0]["matches"]
|
||||
|
||||
|
||||
def _level(actor, value):
|
||||
actor.request("PUT", PREF, json={"value": value})
|
||||
|
||||
|
||||
def test_a_document_in_the_same_container_warns_and_can_be_inserted_anyway(ada):
|
||||
matter = matter_of(ada, "Mietminderung")["id"]
|
||||
first = _ref(ada, "matter", matter)
|
||||
found = _ask(ada, "matter", matter)
|
||||
assert [(m["element_id"], m["where"], m["certain"]) for m in found] == [(first["id"], "here", True)]
|
||||
|
||||
refused = _ref(ada, "matter", matter, status=409)
|
||||
assert refused["code"] == "already_present"
|
||||
# A deliberate second insert goes through. Another document or another connection is no duplicate.
|
||||
_ref(ada, "matter", matter, duplicate="insert")
|
||||
_ref(ada, "matter", matter, external_id="42")
|
||||
_ref(ada, "matter", matter, connector=str(uuid4()))
|
||||
assert _ask(ada, "matter", matter, external_id="42")[0]["where"] == "here"
|
||||
|
||||
|
||||
def test_the_same_document_in_another_vorgang_is_no_duplicate(ada):
|
||||
one = matter_of(ada, "Eins")["id"]
|
||||
two = matter_of(ada, "Zwei")["id"]
|
||||
_ref(ada, "matter", one)
|
||||
assert _ask(ada, "matter", two) == []
|
||||
_ref(ada, "matter", two)
|
||||
|
||||
|
||||
def test_the_check_can_be_switched_off(ada):
|
||||
matter = matter_of(ada, "Mietminderung")["id"]
|
||||
_ref(ada, "matter", matter)
|
||||
_level(ada, "off")
|
||||
assert _ask(ada, "matter", matter) == []
|
||||
_ref(ada, "matter", matter)
|
||||
|
||||
|
||||
def test_levels_reach_into_teilvorgang_vorgang_and_akte(ada):
|
||||
parent = matter_of(ada, "Hauptvorgang")["id"]
|
||||
child = ada.request("POST", "/api/v1/matters", json={"title": "Teil", "parent_matter_id": parent}, status=201)["id"]
|
||||
case = ada.request("POST", "/api/v1/cases", json={"title": "Akte"}, status=201)["id"]
|
||||
ada.request("POST", f"/api/v1/matters/{parent}/case-links", json={"case_id": case}, status=201)
|
||||
_ref(ada, "matter", child, external_id="1")
|
||||
_ref(ada, "matter", parent, external_id="2")
|
||||
_ref(ada, "case", case, external_id="3")
|
||||
|
||||
def where(kind, container, external_id):
|
||||
return [m["where"] for m in _ask(ada, kind, container, external_id)]
|
||||
|
||||
# Standard: only what sits directly in the target.
|
||||
assert where("matter", parent, "1") == []
|
||||
_level(ada, "inside")
|
||||
assert where("matter", parent, "1") == ["inside"]
|
||||
assert where("matter", child, "2") == []
|
||||
assert where("case", case, "2") == ["inside"]
|
||||
assert where("case", case, "3") == ["here"]
|
||||
_level(ada, "around")
|
||||
assert where("matter", child, "2") == ["around"]
|
||||
assert where("matter", parent, "3") == ["around"]
|
||||
assert where("matter", child, "3") == ["around"]
|
||||
# The server warns at the same level.
|
||||
_ref(ada, "matter", child, external_id="3", status=409)
|
||||
|
||||
|
||||
def test_an_existing_element_can_be_bound_instead_of_a_new_one(ada):
|
||||
parent = matter_of(ada, "Hauptvorgang")["id"]
|
||||
child = ada.request("POST", "/api/v1/matters", json={"title": "Teil", "parent_matter_id": parent}, status=201)["id"]
|
||||
original = _ref(ada, "matter", child)
|
||||
_level(ada, "inside")
|
||||
assert _ask(ada, "matter", parent)[0]["can_bind"] is True
|
||||
|
||||
bound = _ref(ada, "matter", parent, bind_element_id=original["id"])
|
||||
assert bound["id"] == original["id"]
|
||||
assert [m["where"] for m in _ask(ada, "matter", parent)] == ["here"]
|
||||
refused = _ref(ada, "matter", parent, bind_element_id=original["id"], status=422)
|
||||
assert "bereits" in refused["detail"]
|
||||
|
||||
|
||||
def test_a_stranger_neither_sees_nor_binds_my_element(client, db, ada):
|
||||
mine = matter_of(ada, "Mein Vorgang")["id"]
|
||||
element = _ref(ada, "matter", mine)
|
||||
ben = _main(client, db, "ben")
|
||||
theirs = matter_of(ben, "Ihr Vorgang")["id"]
|
||||
_level(ben, "around")
|
||||
assert _ask(ben, "matter", theirs) == []
|
||||
_ref(ben, "matter", theirs)
|
||||
_ref(ben, "matter", theirs, bind_element_id=element["id"], status=404)
|
||||
@@ -0,0 +1,79 @@
|
||||
"""Einstellungen je Konto: Erbung vom Hauptnutzer, eigener Wert, Verbindlichkeit für das Team."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.test_team import _invite, _main
|
||||
|
||||
PATH = "/api/v1/auth/preferences/duplicate_check"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def world(client, db):
|
||||
ada = _main(client, db, "ada", main=True)
|
||||
room = ada.request("POST", "/api/v1/rooms", json={"name": "Kanzlei"}, status=201)["id"]
|
||||
ada.use_room(room)
|
||||
emp = _invite(ada, room, "emp-pref@example.test", name="Eva")
|
||||
return {"ada": ada, "emp": emp}
|
||||
|
||||
|
||||
def test_the_default_applies_without_any_choice(world):
|
||||
for person in (world["ada"], world["emp"]):
|
||||
state = person.request("GET", PATH)
|
||||
assert (state["value"], state["source"]) == ("here", "default")
|
||||
assert world["ada"].request("GET", PATH)["can_lock"] is True
|
||||
assert world["emp"].request("GET", PATH)["can_lock"] is False
|
||||
|
||||
|
||||
def test_an_employee_inherits_the_main_user_and_may_override(world):
|
||||
ada, emp = world["ada"], world["emp"]
|
||||
ada.request("PUT", PATH, json={"value": "inside"})
|
||||
inherited = emp.request("GET", PATH)
|
||||
assert (inherited["value"], inherited["source"]) == ("inside", "team")
|
||||
|
||||
own = emp.request("PUT", PATH, json={"value": "off"})
|
||||
assert (own["value"], own["source"]) == ("off", "own")
|
||||
# The main user's change does not touch an own choice.
|
||||
ada.request("PUT", PATH, json={"value": "around"})
|
||||
assert emp.request("GET", PATH)["value"] == "off"
|
||||
assert ada.request("GET", PATH)["value"] == "around"
|
||||
|
||||
# Inheriting again.
|
||||
back = emp.request("PUT", PATH, json={"value": None})
|
||||
assert (back["value"], back["source"]) == ("around", "team")
|
||||
|
||||
|
||||
def test_a_binding_overrides_an_own_value_without_deleting_it(world):
|
||||
ada, emp = world["ada"], world["emp"]
|
||||
emp.request("PUT", PATH, json={"value": "off"})
|
||||
ada.request("PUT", PATH, json={"value": "inside", "team_mode": "locked"})
|
||||
|
||||
locked = emp.request("GET", PATH)
|
||||
assert (locked["value"], locked["source"], locked["locked"]) == ("inside", "locked", True)
|
||||
refused = emp.request("PUT", PATH, json={"value": "here"}, status=403)
|
||||
assert "vorgegeben" in refused["detail"]
|
||||
|
||||
# The main user is not bound by the own binding.
|
||||
assert ada.request("PUT", PATH, json={"value": "here"})["value"] == "here"
|
||||
assert ada.request("GET", PATH)["team_mode"] == "locked"
|
||||
assert emp.request("GET", PATH)["value"] == "here"
|
||||
|
||||
ada.request("PUT", PATH, json={"value": "here", "team_mode": "free"})
|
||||
again = emp.request("GET", PATH)
|
||||
assert (again["value"], again["source"]) == ("off", "own")
|
||||
|
||||
|
||||
def test_only_the_main_user_binds_and_values_are_checked(world):
|
||||
ada, emp = world["ada"], world["emp"]
|
||||
emp.request("PUT", PATH, json={"value": "off", "team_mode": "locked"}, status=403)
|
||||
assert emp.request("GET", PATH)["source"] == "default"
|
||||
ada.request("PUT", PATH, json={"value": "everywhere"}, status=422)
|
||||
ada.request("GET", "/api/v1/auth/preferences/unknown", status=404)
|
||||
ada.request("PUT", "/api/v1/auth/preferences/unknown", json={"value": "off"}, status=404)
|
||||
|
||||
|
||||
def test_a_binding_of_a_stranger_does_not_reach_my_employee(client, db, world):
|
||||
ben = _main(client, db, "ben")
|
||||
ben.request("PUT", PATH, json={"value": "off", "team_mode": "locked"})
|
||||
assert world["emp"].request("GET", PATH)["value"] == "here"
|
||||
+3
-3
@@ -30,7 +30,7 @@ Unter `/home/lars/docker/` legt der Lauf nur diese zwei Verzeichnisse an:
|
||||
|----------|---------|--------|
|
||||
| `deploy-dev.yml` | Push `develop` | Deploy nach `/home/lars/docker/tsumugu-dev`, Health `localhost:8094` und `localhost:3094` |
|
||||
| `deploy-prod.yml` | Push `main` | Deploy nach `/home/lars/docker/tsumugu`, Health `localhost:8007` und `localhost:3007` |
|
||||
| `test.yml` | Push `develop`, nach erfolgreichem Dev-Deploy, manuell | Pytest gegen `tsumugu_test` auf der Dev-Postgres, Smoke der laufenden Dev-App, Frontend-Build |
|
||||
| `test.yml` | Push auf `develop` (wartet, bis der Dev-Checkout den Commit enthält), manuell | Pytest gegen `tsumugu_test` auf der Dev-Postgres, Smoke der laufenden Dev-App, Frontend-Build |
|
||||
|
||||
Gearbeitet wird auf `develop`. Production entsteht nur durch einen Merge-Commit `develop` → `main` (`git merge --no-ff develop` auf `main`) und den anschließenden Push von `main`. Kein Fast-Forward, kein direkter Commit auf `main`, kein manuelles `git pull` auf dem Prod-Checkout. Gitea (`deploy-prod.yml`) ist der einzige Prod-Schreibpfad für Code.
|
||||
|
||||
@@ -50,11 +50,11 @@ Diese Punkte sind im [Code-Review](review/06_BEFUNDE.md) beschrieben. Sie stehen
|
||||
|---|---|---|---|
|
||||
| Betreiberkonto | Auf einer leeren Datenbank legt die öffentliche Anmeldeseite das Betreiberkonto an. Keine MFA | Nach jedem Neuaufsetzen einer Umgebung sofort selbst anmelden, bevor die Adresse öffentlich erreichbar ist. Alternativ vorher `docker compose exec backend python -m app.cli create-account --email … --name … --password …` ausführen | R-04 |
|
||||
| Sicherung | Es gibt kein Backup von Datenbank und Volume `tsumugu-media`/`dev-tsumugu-media` und keinen geprobten Restore | Laut technischer Spezifikation 8.2 nur synthetische Daten in Produktion, bis Paket P7 umgesetzt ist | R-21 |
|
||||
| Release | `deploy-prod.yml` baut bei jedem Push auf `main` neu. Tests laufen nur auf `develop` nach dem Dev-Deploy | Vor dem Merge nach `main` den grünen `test.yml`-Lauf zum selben Stand prüfen | R-21, R-27 |
|
||||
| Release | `deploy-prod.yml` baut bei jedem Push auf `main` neu. Tests laufen nur auf `develop`, sobald der Dev-Deploy den Commit ausgecheckt hat | Vor dem Merge nach `main` den grünen `test.yml`-Lauf zum selben Stand prüfen | R-21, R-27 |
|
||||
| Datenbankzugang | Das Backend migriert beim Start mit dem Owner-Zugang. Die Runtime-Rolle `tsumugu_runtime` erhält dasselbe Passwort wie `DB_USER` | Ein Wechsel von `DB_PASSWORD` ändert beim nächsten Start beide Rollen | R-09 |
|
||||
| Quellen-Geheimnisse | Paperless-Token und IMAP-Passwörter stehen unverschlüsselt in der Tabelle `connectors` | Datenbank-Dumps wie Zugangsdaten schützen | R-05 |
|
||||
| Sessions | 14 Tage gültig, keine Leerlaufabmeldung, kein Passwortwechsel in der Anwendung | Passwort nur über die Datenbank oder ein neues Konto änderbar | R-08 |
|
||||
| Testläufe | `test.yml` startet je Push zweimal auf derselben Datenbank `tsumugu_test` | Ein roter Lauf kann vom parallelen Lauf verursacht sein; Ergebnis des Einzellaufs prüfen | R-27 |
|
||||
| Testläufe | `test.yml` startet je Push einmal, parallel zum Deploy, und wartet auf dessen Checkout (`workflow_run` startete auf dieser Gitea nicht verlässlich); alle Läufe teilen die Datenbank `tsumugu_test`, und das Image bleibt das Laufzeitimage | Zwei manuelle Läufe gleichzeitig vermeiden; sie würden sich die Datenbank wegnehmen | R-27 |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ Legende Spalte „Auth“: **–** öffentlich, **S** gültige Session, **S+R**
|
||||
| POST | `/api/v1/auth/login` | – | E-Mail, Passwort → Session-Sicht und Cookie. 8 Fehlversuche je Bucket (Hash aus IP und E-Mail) in 10 min, gezählt in `auth_login_attempts` |
|
||||
| POST | `/api/v1/auth/logout` | S | Session widerrufen, Cookie löschen, 204 |
|
||||
| GET / PUT | `/api/v1/auth/download-defaults` | S+R, Hauptnutzer | Voreinstellung je Rolle und `direct` (direkt freigegebene Personen); neue Räume übernehmen die drei Rollenwerte. Audit `download_defaults_changed` |
|
||||
| GET / PUT | `/api/v1/auth/preferences/{key}` | S+R | Einstellung des Kontos mit Erbung. GET liefert `value` (wirksam), `source` (`locked`/`own`/`team`/`default`), `own`, `inherited`, `locked`, `allowed`, `can_lock`, `team_mode`. PUT setzt den eigenen Wert (`value`, `null` = erben) und beim Hauptnutzer die Verbindlichkeit für das Team (`team_mode` `free`/`locked`). Ein Mitarbeiter erhält bei verbindlicher Vorgabe 403. Schlüssel: `duplicate_check` (`off`/`here`/`inside`/`around`, Standard `here`). Audit `preference_changed` |
|
||||
| GET | `/api/v1/auth/session` | S | Konto, bestätigter Datenraum, Räume, CSRF-Token, Epoche |
|
||||
| POST | `/api/v1/auth/session/scope` | S | Datenraum wechseln (`kind`, `id`); erhöht die Epoche |
|
||||
| GET | `/api/v1/auth/bootstrap` | – | `{available: true}`, solange kein Betreiber existiert, sonst 404 |
|
||||
@@ -145,7 +146,8 @@ Legende Spalte „Auth“: **–** öffentlich, **S** gültige Session, **S+R**
|
||||
| PATCH | `/api/v1/notes/{id}` | gemeinsamen Text ändern; Antwort mit `visible_bindings` und `notice` |
|
||||
| POST | `/api/v1/files` | Datei hochladen (Multipart: `title`, `context_kind`, `context_id`, `upload`, optional Platzierung, Kapitel, Daten, Richtung); PDF, PNG, JPEG, WebP bis 20 MiB |
|
||||
| GET | `/api/v1/files/{id}/content` | Datei als `attachment` mit Dateiname (UTF-8, `filename*`), `no-store`. Mit `save=true` zusätzlich das Download-Recht, sonst 403 |
|
||||
| POST | `/api/v1/external-refs` | externe Referenz (`paperless`, `imap`, `url`) mit `detail` |
|
||||
| POST | `/api/v1/external-refs` | externe Referenz (`paperless`, `imap`, `url`) mit `detail`. `duplicate` (`ask` Standard, `insert`): bei `ask` antwortet ein bereits vorhandenes Dokument mit 409 `already_present`, wenn nur sichere Treffer auf der Prüfstufe des Kontos zählen (Einstellung `duplicate_check`). `bind_element_id` bindet stattdessen das vorhandene Element zusätzlich in den Zielcontainer (eine Referenz, eine weitere Einbindung; Audit `external_ref_bound`). Nur ein sicherer, lesbarer Treffer im Umfeld des Ziels, mit Weitergaberecht (sonst 403 bzw. 404) |
|
||||
| POST | `/api/v1/external-refs/existing` | nur lesend: `context`, optional `placement`, `items` (`source`, `external_id`, `connector_id`) → je Dokument die Treffer (`element_id`, `title`, `where` `here`/`inside`/`around`, `certain`, `chapter`, `container`, `can_bind`) auf der Prüfstufe des Kontos. Nur Lesbares, ein Treffer außerhalb der Rechte gilt als nicht vorhanden |
|
||||
| POST | `/api/v1/sources/link` | wie oben, nur `paperless`/`imap` (doppelter Weg, siehe Connectoren-Befund) |
|
||||
| GET | `/api/v1/elements` | eigene, nicht eingebundene Elemente (max. 100) |
|
||||
| POST | `/api/v1/elements/{id}/delete` | überall löschen; nur Element-Owner |
|
||||
@@ -193,13 +195,13 @@ Legende Spalte „Auth“: **–** öffentlich, **S** gültige Session, **S+R**
|
||||
| DELETE | `/api/v1/connectors/{id}` | Zugang löschen; bestehende Referenzen bleiben und werden nicht umgehängt |
|
||||
| POST | `/api/v1/connectors/{id}/probe` | gespeicherten Zugang prüfen; Ergebnis (auch ein Fehler) wird als Prüfstand festgehalten. Es gibt keine Prüfung mit ungespeicherten Entwürfen |
|
||||
| GET | `/api/v1/sources/capabilities` | erklärte Such-, Lese- und Editorfähigkeiten; nicht Vorhandenes bleibt falsch |
|
||||
| GET | `/api/v1/sources/{kind}/search` | Suche der registrierten Quelle im erklärten Umfang; unbekannte Art und nicht erklärte Filter antworten `422` |
|
||||
| GET | `/api/v1/sources/{kind}/search` | Suche der registrierten Quelle im erklärten Umfang. Außer `q`, `connector_id`, `mailbox`, `before` und `uidvalidity` ist jeder Abfrageparameter ein Filter, den das Modul in `search_params` deklariert; unbekannte Art, nicht deklarierte oder nicht erklärte Filter und unbekannte Parameter antworten `422` |
|
||||
| GET | `/api/v1/sources/{kind}/navigate?connector_id=` | Fachnavigation, wenn die Quelle sie erklärt |
|
||||
| GET | `/api/v1/sources/{kind}/preview` | Vorschau vor dem Übernehmen, wenn die Quelle sie erklärt |
|
||||
| GET | `/api/v1/sources/paperless/documents?q=&connector_id=` | Paperless-Suche, 20 Treffer je Seite, `cursor` ist die Seitennummer. `mode`: `title_content` (Standard, Titel und Inhalt ohne Index), `title`, `content`, `index` (Volltextindex von Paperless). Filter: `title`, `correspondent`, `document_type`, `storage_path` (jeweils Nummer oder `none` für „ohne“), `tag` (Nummern, durch Komma) mit `tag_mode` `all` (Standard), `any` oder `none`, `document_from`/`document_to` (Dokumentdatum), `added_from`/`added_to` (Hinzugefügt), `asn`, `custom` (JSON-Liste `[{field, op, value}]`, höchstens 5, alle zugleich; `field` ist die Nummer aus `facets.custom_fields`, `op` je Feldtyp `exact`, `icontains`, `istartswith`, `iendswith`, `gt`, `gte`, `lt`, `lte`, `range` oder `exists`; Dokumentverweis-Felder sind nicht filterbar; wird als `custom_field_query` an Paperless gegeben), `sort` (`created`, `added`, `title`, jeweils mit `-`; `-score` nur bei `mode=index` mit Suchtext). Ein ungültiger Wert antwortet `422`. Je Treffer: `title`, `document_on`, `added_on`, `correspondent`, `document_type`, `storage_path`, `tags` (Namen), `pages`, `asn`, `snippet` (Textauszug, reiner Text) und `open_url`; dazu `total`, `next_page`, `previous_page` |
|
||||
| GET | `/api/v1/sources/{kind}/facets?connector_id=` | Auswahllisten der Instanz (Paperless: Korrespondenten, Dokumenttypen, Speicherpfade, Tags, Zusatzfelder mit Typ und Auswahlwerten). Namen und Listen werden je Instanz und Schlüssel eine Minute lang vorgehalten |
|
||||
| GET | `/api/v1/sources/paperless/documents/{id}/preview?connector_id=` | Datei zum Ansehen vor dem Übernehmen |
|
||||
| GET | `/api/v1/sources/paperless/documents/{id}/thumb?connector_id=` | Vorschaubild der Trefferliste. Läuft über den Server der eigenen Instanz, `private, no-store`; fremde Instanz `404`, Antwort ohne Bild `422` |
|
||||
| GET | `/api/v1/sources/{kind}/thumbnail?connector_id=&item=` | Vorschaubild eines Treffers, nur wenn das Modul `search.result_preview` erklärt und ein `thumbnail` bereitstellt, sonst `422`. Läuft über den Server der eigenen Instanz, `private, no-store`, nur Rastergrafiken (PNG, JPEG, WebP, GIF); fremde Instanz `404`, eine Antwort ohne Bild `422` |
|
||||
| GET | `/api/v1/sources/imap/mailboxes?connector_id=` | IMAP-Fächer |
|
||||
| GET | `/api/v1/sources/imap/messages?connector_id=&mailbox=&q=&before=` | Nachrichtenliste, 30 je Seite, Fortsetzung über `before` |
|
||||
| GET | `/api/v1/sources/imap/message?connector_id=&mailbox=&uid=` | Vorschau vor dem Übernehmen |
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Tsumugu – Datenmodell (Ist-Stand)
|
||||
|
||||
**Stand:** 19.10.2026, nach den Migrationen in `backend/migrations/` (letzte: `20261022T090000Z_account_kinds.sql`). Entstanden im Code-Review vom 18.10.2026 und für die Connector-Grundlage nachgezogen. Die Arten `connectors.kind` und `external_references.source` prüft die Registrierung. Die früheren Prüflisten in der Datenbank sind entfernt. `url` bleibt eine Referenz ohne Connector.
|
||||
**Stand:** 19.10.2026, nach den Migrationen in `backend/migrations/` (letzte: `20261025T090000Z_account_preferences.sql`). Entstanden im Code-Review vom 18.10.2026 und für die Connector-Grundlage nachgezogen. Die Arten `connectors.kind` und `external_references.source` prüft die Registrierung. Die früheren Prüflisten in der Datenbank sind entfernt. `url` bleibt eine Referenz ohne Connector.
|
||||
|
||||
Die Sollfassung steht in der [technischen Spezifikation, Abschnitt 4](TSUMUGU_TECHNICAL_SPEC.md). Diese Datei beschreibt, was tatsächlich angelegt ist. Bei jeder neuen Migration nachziehen.
|
||||
|
||||
@@ -22,6 +22,7 @@ Die Sollfassung steht in der [technischen Spezifikation, Abschnitt 4](TSUMUGU_TE
|
||||
| `data_rooms` | `id`, `kind` (`personal`/`organization`), `name`, `status` (`active`/`archived`), `created_by` | persönlicher Raum: `id = account_id` |
|
||||
| `room_memberships` | PK (`room_id`, `subject_id`), `role` (`member`/`manager`/`reader`), `status` (`active`/`removed`) | – |
|
||||
| `auth_accounts` (Spalte seit 23.10.2026) | `download_defaults` (jsonb: `manager`, `member`, `reader`, `direct`; Voreinstellung für das Herunterladen von Originalen, Standard überall erlaubt) | Neue Räume übernehmen die drei Rollenwerte |
|
||||
| `account_preferences` (seit 25.10.2026) | PK (`account_id`, `key`), `value` (jsonb, `NULL` = erben), `team_mode` (`free`/`locked`, nur beim Hauptnutzer ausgewertet), `updated_at` | Ein Mitarbeiter erbt den Wert seines Hauptnutzers (`sponsor_id`) und kann ihn ändern. Ist die Einstellung für das Team verbindlich, gilt der Wert des Hauptnutzers; ein eigener Wert bleibt gespeichert und gilt wieder, sobald die Bindung endet. Erlaubte Werte und Standard je Schlüssel stehen in `app/preferences.py` |
|
||||
| `data_rooms` (Spalte seit 23.10.2026) | `download_roles` (jsonb: `manager`, `member`, `reader`) | Regel je Raum, nur der Eigentümer ändert sie |
|
||||
| `data_rooms` (Spalten seit 21.10.2026) | `owner_id` (Eigentümer; für persönliche Räume das Konto), `default_visibility` (`private`/`room`, Voreinstellung für neue Akten und Vorgänge) | Bestehende Räume `private`, Eigentümer aus `created_by` |
|
||||
| `room_shares` | `resource_kind` (`case`/`matter`), `resource_id`, `room_id`, `access` (`read`/`edit`), `created_by` | Freigabe einer Akte oder eines Vorgangs an einen Datenraum (Team); eindeutig je Eintrag und Raum. Endet, wenn der Freigebende den Raum verlässt oder Leser wird |
|
||||
|
||||
@@ -99,3 +99,20 @@ Die Konfiguration einer Instanz gehört dem Modul. Ein Modul deklariert `config_
|
||||
Neuer Connector: Modul mit `config_fields` in `register_source` eintragen, UI-Modul mit `registerSourceModule` registrieren. Kein Eingriff in `ConnectorIn`, `Sources.jsx` oder die Fachseiten.
|
||||
|
||||
Nachweis und Grenzen: `backend/tests/test_connector_config.py` (Manifest, Geheimnis, Instanzerhalt, Fremdzugriff, Prüfstand) und `frontend/src/sources/config-core.test.mjs` (6 Node-Tests bestanden), Frontend-Build erfolgreich. Die Backend-Tests wurden in dieser Umgebung **nicht ausgeführt** (kein Python, keine PostgreSQL-Instanz). Keine Bedienprüfung im Browser. Zugangsdaten bleiben im Klartext (R-05); Archivmodus und DataPolicy sind nicht Teil dieses Schnitts.
|
||||
|
||||
## Paperless-Suche, Erweiterung am 24.10.2026
|
||||
|
||||
Auf Wunsch des Nutzers erweitert über den Umfang von Abschnitt C des Nacharbeitsauftrags hinaus: Suchart (Titel und Inhalt als Standard ohne Index, nur Titel, nur Inhalt, Volltextindex), Speicherpfad, Tag-Verknüpfung (alle, mindestens eines, keines), „ohne“ bei Korrespondent, Typ und Pfad, Hinzugefügt-Datum, Archivnummer und Zusatzfelder. Dazu eine Trefferkarte mit Vorschaubild, Textauszug, Tags, Typ und Seitenzahl sowie ein großer Dialog mit Filtern links und Treffern rechts. Zusatzfeld-Abfragen waren im Auftrag ausdrücklich kein Pflichtumfang und sind hier auf Nutzerwunsch dazugekommen.
|
||||
|
||||
**Verantwortung im Modul:** Die Filternamen eines Moduls deklariert `SourceModule.search_params` (Parametername auf Fähigkeitsflag). Die gemeinsame Suchroute kennt keinen Filternamen mehr. Sie reicht alle übrigen Abfrageparameter weiter und weist jeden ab, den das Modul nicht deklariert oder dessen Fähigkeit nicht wahr ist. Das gilt auch für unbekannte Parameter. Das Vorschaubild ist eine optionale Modulfähigkeit (`SourceModule.thumbnail`, Route `/api/v1/sources/{kind}/thumbnail`, Flag `search.result_preview`). Ein Modul ohne Deklaration lehnt es mit `422` ab.
|
||||
|
||||
**Verbleibende Quellkenntnis im allgemeinen Code (zulässig):** Die Kompatibilitätsrouten `/api/v1/sources/paperless/documents` und `.../preview` in `backend/app/api/sources.py` delegieren nur und tragen keine eigene Protokoll- oder Rechtelogik. Die Fähigkeiten von Paperless und IMAP stehen jetzt in `backend/app/sources.py` beim jeweiligen Modul; `backend/app/sourceport.py` enthält nur noch die Editorfähigkeiten und die Nutzlasten. Die unbenutzte Liste `UNSUPPORTED_SEARCH` und `unsupported_filters` sind entfernt. In der Oberfläche liegt der Paperless-Auswahldialog in `frontend/src/pages/Sources.jsx` und wird über `registerSourceModule` (`large: true`) eingehängt; Fachseiten und Modal kennen nur das allgemeine Flag.
|
||||
|
||||
| Nachweis | Ergebnis |
|
||||
|---|---|
|
||||
| Backend-Suite gegen `tsumugu_local_test` (eingebettete PostgreSQL, Wegwerf-Umgebung) | **164 Tests bestanden** |
|
||||
| `test_connector_followup.py`: Parameterübertragung je Filter, Standardsuche, Textauszug, Namensauflösung, Zusatzfelder samt Ablehnung ungültiger Eingaben, fremder Filter und unbekannter Parameter `422`, Vorschaubild nur für Raster, fremde Instanz `404` | bestanden, gegen Attrappe |
|
||||
| Bedienung im Browser (Chrome headless, Desktop 1440×900 und 390×844) gegen die echte Oberfläche, die echte API und einen nachgebildeten Paperless-HTTP-Server: Dialoggröße, Suche, Vorschaubilder, Textauszug, Ansicht in der Karte, unvollständige Zusatzfeld-Zeile meldet statt zu senden, Zusatzfelder gehen als `custom_field_query` hinaus, Blättern erhält die Auswahl, Einfügen, keine Konsolenfehler | 38 von 38 Prüfungen bestanden. Dabei ist ein Überlappen von Filtern und Treffern auf 390 px gefunden und behoben worden. |
|
||||
|
||||
**Grenze:** Keine der Prüfungen lief gegen die echte Paperless-Instanz. Die Parameternamen (`title_content`, `tags__id__none`, `storage_path__id`, `added__date__gte`, `archive_serial_number`, `custom_field_query`) stammen aus der Kenntnis der Paperless-ngx-API und stehen am Stellvertreter, nicht an der Nutzerinstanz belegt. Die Flags `custom_fields` und `result_preview` sind damit nur durch Code und Attrappe ausführbar belegt. Offen sind insbesondere der Wert eines Betragsfelds (reine Zahl ohne Währung), der Textauszug im Volltextindex und die Mindestversion für Zusatzfeld-Abfragen. Eine Prüfung an der echten Instanz braucht eine ausdrückliche Freigabe.
|
||||
|
||||
@@ -4,6 +4,7 @@ import { SessionContext } from "../session.js";
|
||||
import { ErrorNote } from "../pages/widgets.jsx";
|
||||
import { formatWhen } from "./admin-core.js";
|
||||
import { DownloadPolicy } from "./DownloadPolicy.jsx";
|
||||
import { DuplicatePreference } from "./DuplicatePreference.jsx";
|
||||
import { deviceClass, readImageChoice, writeImageChoice } from "../mail-images.js";
|
||||
import { isEmployee } from "./admin-core.js";
|
||||
import { PasswordForm } from "./widgets.jsx";
|
||||
@@ -82,6 +83,7 @@ export function Account() {
|
||||
<ErrorNote error={error} />
|
||||
{notice ? <p className="notice" role="status">{notice}</p> : null}
|
||||
<MailImages />
|
||||
<DuplicatePreference />
|
||||
{isEmployee(session) ? null : (
|
||||
<section className="card stack" aria-label="Herunterladen">
|
||||
<h2>Herunterladen von Originalen</h2>
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { api } from "../api.js";
|
||||
import { ErrorNote } from "../pages/widgets.jsx";
|
||||
|
||||
const PATH = "/api/v1/auth/preferences/duplicate_check";
|
||||
|
||||
export const DUPLICATE_LEVELS = {
|
||||
off: "Keine Prüfung",
|
||||
here: "Nur hier (direkt in diesem Vorgang oder dieser Akte)",
|
||||
inside: "Hier und was darin angezeigt wird (Teilvorgänge, zugeordnete Vorgänge)",
|
||||
around: "Zusätzlich dort, wo es liegt (übergeordneter Vorgang, zugeordnete Akten)",
|
||||
};
|
||||
|
||||
// Where a document that is inserted again counts as already present. Only a warning, never a block.
|
||||
// An employee inherits the value of the main user and may change it unless the main user made it binding.
|
||||
export function DuplicatePreference() {
|
||||
const [state, setState] = useState(null);
|
||||
const [error, setError] = useState("");
|
||||
const [notice, setNotice] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
api(PATH).then((data) => alive && setState(data)).catch((err) => alive && setError(err.message));
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
async function save(body) {
|
||||
setError("");
|
||||
setNotice("");
|
||||
try {
|
||||
setState(await api(PATH, { method: "PUT", body }));
|
||||
setNotice("Die Einstellung ist gespeichert.");
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
if (!state) return error ? <ErrorNote error={error} /> : null;
|
||||
const ownChoice = state.can_lock ? state.own || state.default : state.own || "";
|
||||
return (
|
||||
<section className="card stack" aria-label="Doppelte Dokumente">
|
||||
<h2>Doppelte Dokumente</h2>
|
||||
<p className="muted">
|
||||
Beim Einfügen eines Dokuments aus einer Quelle warnt Tsumugu, wenn es an der gewählten Stelle schon vorhanden ist. Du kannst es trotzdem
|
||||
einfügen oder das vorhandene Element zusätzlich einbinden. Dasselbe Dokument in verschiedenen Vorgängen bleibt immer erlaubt.
|
||||
</p>
|
||||
<label>
|
||||
Prüfen in
|
||||
<select
|
||||
value={ownChoice}
|
||||
disabled={state.locked}
|
||||
onChange={(event) => save({ value: event.target.value || null })}
|
||||
>
|
||||
{state.can_lock ? null : <option value="">Wie beim Hauptnutzer ({DUPLICATE_LEVELS[state.inherited]})</option>}
|
||||
{state.allowed.map((level) => (
|
||||
<option key={level} value={level}>{DUPLICATE_LEVELS[level]}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{state.locked ? <p className="muted">Der Hauptnutzer hat diese Einstellung für das Team vorgegeben: {DUPLICATE_LEVELS[state.value]}.</p> : null}
|
||||
{state.can_lock ? (
|
||||
<label className="check">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={state.team_mode === "locked"}
|
||||
onChange={(event) => save({ value: state.own, team_mode: event.target.checked ? "locked" : "free" })}
|
||||
/>
|
||||
Für Mitarbeiter verbindlich
|
||||
</label>
|
||||
) : null}
|
||||
<ErrorNote error={error} />
|
||||
{notice ? <p className="notice" role="status">{notice}</p> : null}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import { Workspace, rememberScroll, restoreScroll, useWorkspaceLayout } from "./
|
||||
import { useLibraryFocus } from "../library/LibraryLayout.jsx";
|
||||
import { outlineSignature } from "../library/library-core.js";
|
||||
import { NavIcon } from "./icons.jsx";
|
||||
import { useExternalPlacement } from "../sources/duplicates.jsx";
|
||||
|
||||
function matterOrigin(group) {
|
||||
const kind = group.kind_label === "Teilvorgang" ? "Teilvorgang" : "Vorgang";
|
||||
@@ -905,6 +906,15 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "",
|
||||
const [sentOn, setSentOn] = useState("");
|
||||
const [receivedOn, setReceivedOn] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const { place, panel } = useExternalPlacement({
|
||||
context,
|
||||
placement,
|
||||
onPlaced: () => {
|
||||
setTitle("");
|
||||
setExternalId("");
|
||||
onChange();
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => { setChapterId(initialChapter || ""); }, [initialChapter]);
|
||||
|
||||
@@ -912,25 +922,19 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "",
|
||||
event.preventDefault();
|
||||
setError("");
|
||||
try {
|
||||
await api("/api/v1/external-refs", {
|
||||
method: "POST",
|
||||
body: {
|
||||
title,
|
||||
source,
|
||||
external_id: externalId,
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
dates: {
|
||||
document_on: documentOn || null,
|
||||
sent_on: sentOn || null,
|
||||
received_on: receivedOn || null,
|
||||
},
|
||||
await place({
|
||||
title,
|
||||
source,
|
||||
external_id: externalId,
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
dates: {
|
||||
document_on: documentOn || null,
|
||||
sent_on: sentOn || null,
|
||||
received_on: receivedOn || null,
|
||||
},
|
||||
});
|
||||
setTitle("");
|
||||
setExternalId("");
|
||||
onChange();
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
}
|
||||
@@ -951,6 +955,7 @@ function ExternalForm({ context, placement, chapters = [], initialChapter = "",
|
||||
{chapterSelect(chapters, chapterId, setChapterId)}
|
||||
<button type="submit">Referenz erfassen</button>
|
||||
<ErrorNote error={error} />
|
||||
{panel}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
+210
-64
@@ -5,6 +5,7 @@ import { api, fetchBlob } from "../api.js";
|
||||
import { MailBody } from "../mail-view.jsx";
|
||||
import { NewConnector, useSourceModules } from "../sources/connector-config.jsx";
|
||||
import { SourceFrame, registerSourceModule } from "../sources/registry.jsx";
|
||||
import { PresentNote, bindable, describeMatch, lookupPresent, presentKey, useExternalPlacement } from "../sources/duplicates.jsx";
|
||||
|
||||
const SEARCH_LIMITS = {
|
||||
paperless: "Die Suche läuft in Paperless selbst, über Titel und Inhalt, den Titel allein, den Inhalt allein oder den Volltextindex. Filter nach Korrespondent, Dokumenttyp, Speicherpfad, Tags, Dokumentdatum, Hinzugefügt-Datum und Archivnummer grenzen sie ein.",
|
||||
@@ -155,7 +156,20 @@ function customKind(field) {
|
||||
return CUSTOM_KINDS[field?.type] || "text";
|
||||
}
|
||||
|
||||
// Only rows that are complete go to the server. An empty row is a row not filled in yet.
|
||||
// A row that is not complete is reported, not left out of the search.
|
||||
function customProblem(rows) {
|
||||
for (const row of rows) {
|
||||
const name = row.field ? "Eine Zusatzfeld-Bedingung" : "Eine Zusatzfeld-Bedingung ohne Feld";
|
||||
if (!row.field) return `${name} ist unvollständig. Wähle ein Feld oder entferne die Zeile.`;
|
||||
const presence = row.op === "set" || row.op === "unset";
|
||||
if (presence || row.kind === "boolean") continue;
|
||||
if (row.op === "range" ? !(row.value && row.value2) : !String(row.value).trim()) {
|
||||
return `${name} ist unvollständig. Gib den Wert an oder entferne die Zeile.`;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function customPayload(rows) {
|
||||
const payload = [];
|
||||
for (const row of rows) {
|
||||
@@ -252,21 +266,21 @@ function CustomFieldFilters({ fields, rows, onChange }) {
|
||||
// The thumbnail comes through the server like every other Paperless call, so the key stays there.
|
||||
function PaperlessThumb({ connectorId, id }) {
|
||||
const [url, setUrl] = useState("");
|
||||
const [failed, setFailed] = useState(false);
|
||||
const [failed, setFailed] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
let made = "";
|
||||
setUrl("");
|
||||
setFailed(false);
|
||||
fetchBlob(`/api/v1/sources/paperless/documents/${id}/thumb?connector_id=${connectorId}`)
|
||||
setFailed("");
|
||||
fetchBlob(`/api/v1/sources/paperless/thumbnail?connector_id=${connectorId}&item=${id}`)
|
||||
.then((blob) => {
|
||||
if (!alive) return;
|
||||
made = URL.createObjectURL(blob);
|
||||
setUrl(made);
|
||||
})
|
||||
.catch(() => {
|
||||
if (alive) setFailed(true);
|
||||
.catch((err) => {
|
||||
if (alive) setFailed(err.message || "Das Vorschaubild ist nicht verfügbar.");
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
@@ -275,7 +289,7 @@ function PaperlessThumb({ connectorId, id }) {
|
||||
}, [connectorId, id]);
|
||||
|
||||
if (url) return <img src={url} alt="" className="paperless-thumb__image" />;
|
||||
return <span className="paperless-thumb__empty" aria-hidden="true">{failed ? "Kein Bild" : ""}</span>;
|
||||
return <span className="paperless-thumb__empty" title={failed || undefined}>{failed ? "Kein Bild" : ""}</span>;
|
||||
}
|
||||
|
||||
export function PaperlessPicker({ context, placement, chapterId = "", chapterTitle = "", targetLabel = "", onDone, onInserted }) {
|
||||
@@ -290,10 +304,16 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
const [total, setTotal] = useState(null);
|
||||
const [searched, setSearched] = useState(false);
|
||||
const [picked, setPicked] = useState(() => new Map());
|
||||
// Documents that are already present around the target, by presentKey. `review` holds the choices while the user decides.
|
||||
const [present, setPresent] = useState(() => new Map());
|
||||
const [review, setReview] = useState(null);
|
||||
const [previewKey, setPreviewKey] = useState("");
|
||||
const [previewUrl, setPreviewUrl] = useState("");
|
||||
const [pending, setPending] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
// The filters sit beside the hits on a wide screen. On a phone they start folded away.
|
||||
const [tagSearch, setTagSearch] = useState("");
|
||||
const [moreOpen, setMoreOpen] = useState(() => typeof window === "undefined" || !window.matchMedia || window.matchMedia("(min-width: 900px)").matches);
|
||||
const searchRef = useRef(0);
|
||||
const previewRef = useRef(0);
|
||||
const facetRef = useRef(0);
|
||||
@@ -338,8 +358,7 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
if (connectorId) params.set("connector_id", connectorId);
|
||||
if (text) params.set("q", text);
|
||||
if (text && filters.mode !== "title_content") params.set("mode", filters.mode);
|
||||
// The search text already is the title in this mode.
|
||||
if (filters.title.trim() && !(text && filters.mode === "title")) params.set("title", filters.title.trim());
|
||||
if (filters.title.trim()) params.set("title", filters.title.trim());
|
||||
if (filters.correspondent) params.set("correspondent", filters.correspondent);
|
||||
if (filters.documentType) params.set("document_type", filters.documentType);
|
||||
if (filters.storagePath) params.set("storage_path", filters.storagePath);
|
||||
@@ -383,6 +402,11 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
setError("Wähle zuerst den Paperless-Zugang.");
|
||||
return;
|
||||
}
|
||||
const problem = customProblem(filters.custom);
|
||||
if (problem) {
|
||||
setError(problem);
|
||||
return;
|
||||
}
|
||||
const token = searchRef.current + 1;
|
||||
searchRef.current = token;
|
||||
const asked = connectorId;
|
||||
@@ -397,12 +421,33 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
setTotal(typeof data.total === "number" ? data.total : null);
|
||||
setSearched(true);
|
||||
if (!asked && data.connector_id) setConnectorId(data.connector_id);
|
||||
refreshPresent(data.items || [], asked || data.connector_id || "");
|
||||
} catch (err) {
|
||||
if (token !== searchRef.current) return;
|
||||
setError(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
function presentEntry(item, bound = connectorId) {
|
||||
return { source: "paperless", external_id: String(item.id), connector_id: item.connector_id || bound };
|
||||
}
|
||||
|
||||
function matchesOf(item) {
|
||||
const entry = presentEntry(item);
|
||||
return present.get(presentKey(entry.source, entry.connector_id, entry.external_id)) || [];
|
||||
}
|
||||
|
||||
async function refreshPresent(list, bound) {
|
||||
const entries = list.map((item) => presentEntry(item, bound));
|
||||
const found = await lookupPresent(context, placement, entries);
|
||||
setPresent((current) => {
|
||||
const next = new Map(current);
|
||||
entries.forEach((entry) => next.delete(presentKey(entry.source, entry.connector_id, entry.external_id)));
|
||||
found.forEach((value, key) => next.set(key, value));
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function resetFilters() {
|
||||
invalidate();
|
||||
closePreview();
|
||||
@@ -449,52 +494,82 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
}
|
||||
}
|
||||
|
||||
async function linkMany() {
|
||||
async function linkMany(choices = null) {
|
||||
const chosen = [...picked.values()];
|
||||
if (!chosen.length) return;
|
||||
setError("");
|
||||
const keyOf = (item) => `${item.connector_id || connectorId}:${item.id}`;
|
||||
if (!choices) {
|
||||
const known = chosen.filter((item) => matchesOf(item).length);
|
||||
if (known.length) {
|
||||
setReview(new Map(known.map((item) => [keyOf(item), "skip"])));
|
||||
return;
|
||||
}
|
||||
}
|
||||
setReview(null);
|
||||
setPending(true);
|
||||
const inserted = [];
|
||||
const skipped = [];
|
||||
const conflicts = [];
|
||||
try {
|
||||
for (const item of chosen) {
|
||||
await api("/api/v1/external-refs", {
|
||||
method: "POST",
|
||||
body: {
|
||||
title: item.title,
|
||||
source: "paperless",
|
||||
external_id: String(item.id),
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
...(item.document_on || item.correspondent || item.open_url || connectorId
|
||||
? {
|
||||
dates: { document_on: item.document_on || null, sent_on: null, received_on: null },
|
||||
detail: {
|
||||
...(item.connector_id || connectorId ? { connector_id: item.connector_id || connectorId } : {}),
|
||||
...(item.correspondent ? { correspondent: item.correspondent } : {}),
|
||||
...(item.open_url ? { open_url: item.open_url } : {}),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
inserted.push(`${item.connector_id || connectorId}:${item.id}`);
|
||||
const key = keyOf(item);
|
||||
const choice = choices?.get(key);
|
||||
if (choice === "skip") {
|
||||
skipped.push(key);
|
||||
continue;
|
||||
}
|
||||
const existing = choice === "bind" ? bindable(matchesOf(item)) : null;
|
||||
try {
|
||||
await api("/api/v1/external-refs", {
|
||||
method: "POST",
|
||||
body: {
|
||||
title: item.title,
|
||||
source: "paperless",
|
||||
external_id: String(item.id),
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
...(choice === "insert" ? { duplicate: "insert" } : {}),
|
||||
...(existing ? { bind_element_id: existing.element_id } : {}),
|
||||
...(item.document_on || item.correspondent || item.open_url || connectorId
|
||||
? {
|
||||
dates: { document_on: item.document_on || null, sent_on: null, received_on: null },
|
||||
detail: {
|
||||
...(item.connector_id || connectorId ? { connector_id: item.connector_id || connectorId } : {}),
|
||||
...(item.correspondent ? { correspondent: item.correspondent } : {}),
|
||||
...(item.open_url ? { open_url: item.open_url } : {}),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
inserted.push(key);
|
||||
} catch (err) {
|
||||
// Inserted by someone else in the meantime: ask again instead of failing.
|
||||
if (err.code !== "already_present") throw err;
|
||||
conflicts.push(item);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
setPending(false);
|
||||
}
|
||||
if (conflicts.length) {
|
||||
await refreshPresent(conflicts, connectorId);
|
||||
setReview(new Map(conflicts.map((item) => [keyOf(item), "skip"])));
|
||||
}
|
||||
if (inserted.length) {
|
||||
setItems((current) => current.filter((item) => !inserted.includes(`${connectorId}:${item.id}`)));
|
||||
setPicked((current) => {
|
||||
const next = new Map(current);
|
||||
inserted.forEach((id) => next.delete(id));
|
||||
return next;
|
||||
});
|
||||
onInserted?.();
|
||||
}
|
||||
if (inserted.length === chosen.length) onDone();
|
||||
setPicked((current) => {
|
||||
const next = new Map(current);
|
||||
[...inserted, ...skipped].forEach((id) => next.delete(id));
|
||||
return next;
|
||||
});
|
||||
if (inserted.length && inserted.length + skipped.length === chosen.length) onDone();
|
||||
}
|
||||
|
||||
function setFilter(name, value) {
|
||||
@@ -505,6 +580,16 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
setFilters((current) => ({ ...current, mode, sort: current.sort === "-score" && mode !== "index" ? "-created" : current.sort }));
|
||||
}
|
||||
|
||||
const visibleTags = facets.tags.filter((item) => item.name.toLocaleLowerCase().includes(tagSearch.trim().toLocaleLowerCase()));
|
||||
|
||||
function tagName(id) {
|
||||
return facets.tags.find((item) => item.id === id)?.name || id;
|
||||
}
|
||||
|
||||
function selectTags(ids) {
|
||||
setFilters((current) => ({ ...current, tags: [...new Set([...current.tags, ...ids])] }));
|
||||
}
|
||||
|
||||
function toggleTag(id) {
|
||||
setFilters((current) => {
|
||||
const tags = current.tags.includes(id) ? current.tags.filter((item) => item !== id) : [...current.tags, id];
|
||||
@@ -568,15 +653,12 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
<button type="submit">Suchen</button>
|
||||
</div>
|
||||
<p className="muted">{PAPERLESS_MODE_HINTS[filters.mode]}</p>
|
||||
<details className="paperless-more" open>
|
||||
<details className="paperless-more" open={moreOpen} onToggle={(event) => setMoreOpen(event.currentTarget.open)}>
|
||||
<summary>{active ? `Weitere Filter (${active} aktiv)` : "Weitere Filter"}</summary>
|
||||
<div className="source-filters">
|
||||
<label>
|
||||
Titel enthält
|
||||
<input value={filters.title} onChange={(event) => setFilter("title", event.target.value)} />
|
||||
{filters.mode === "title" && filters.query.trim() && filters.title.trim() ? (
|
||||
<span className="muted">Bei „Nur Titel“ gilt der Suchtext als Titel. Dieses Feld bleibt unberücksichtigt.</span>
|
||||
) : null}
|
||||
</label>
|
||||
<label>
|
||||
Korrespondent
|
||||
@@ -600,13 +682,39 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
<option value="none">Keines der gewählten Tags</option>
|
||||
</select>
|
||||
</label>
|
||||
{filters.tags.length ? (
|
||||
<ul className="paperless-chips" aria-label="Gewählte Tags">
|
||||
{filters.tags.map((id) => (
|
||||
<li key={id}>
|
||||
<button type="button" className="paperless-chip" onClick={() => toggleTag(id)} aria-label={`Tag ${tagName(id)} entfernen`}>
|
||||
{tagName(id)} <span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
{facets.tags.length ? (
|
||||
<>
|
||||
<input type="search" value={tagSearch} onChange={(event) => setTagSearch(event.target.value)} placeholder="Tags suchen" aria-label="Tags suchen" />
|
||||
<div className="paperless-tags__actions">
|
||||
<button type="button" className="quiet" onClick={() => selectTags(visibleTags.map((item) => item.id))} disabled={!visibleTags.length}>
|
||||
{tagSearch.trim() ? "Treffer wählen" : "Alle wählen"}
|
||||
</button>
|
||||
<button type="button" className="quiet" onClick={() => setFilter("tags", [])} disabled={!filters.tags.length}>
|
||||
Alle abwählen
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
<div className="paperless-tags">
|
||||
{facets.tags.length ? facets.tags.map((item) => (
|
||||
{visibleTags.map((item) => (
|
||||
<label key={item.id} className="paperless-pick">
|
||||
<input type="checkbox" checked={filters.tags.includes(item.id)} onChange={() => toggleTag(item.id)} />
|
||||
<span>{item.name}</span>
|
||||
</label>
|
||||
)) : <p className="muted">Keine Tags an diesem Zugang.</p>}
|
||||
))}
|
||||
{!facets.tags.length ? <p className="muted">Keine Tags an diesem Zugang.</p> : null}
|
||||
{facets.tags.length && !visibleTags.length ? <p className="muted">Kein Tag passt zur Suche.</p> : null}
|
||||
</div>
|
||||
</fieldset>
|
||||
<label>
|
||||
@@ -653,8 +761,37 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
<ErrorNote error={error} />
|
||||
<p className="muted">{SEARCH_LIMITS.paperless}</p>
|
||||
<p className="muted">Die Verbindung richtest du unter <Link to="/sources">Quellen</Link> ein. Ausgewählte Belege bleiben erhalten, wenn du weiterblätterst oder neu suchst.</p>
|
||||
{review ? (
|
||||
<div className="card stack duplicate-panel" role="alert">
|
||||
<strong>Diese Belege sind schon vorhanden. Was soll geschehen?</strong>
|
||||
<ul className="duplicate-list">
|
||||
{[...picked.values()].filter((item) => review.has(`${item.connector_id || connectorId}:${item.id}`)).map((item) => {
|
||||
const key = `${item.connector_id || connectorId}:${item.id}`;
|
||||
const matches = matchesOf(item);
|
||||
return (
|
||||
<li key={key}>
|
||||
<span>{item.title}: {matches.length ? describeMatch(matches[0]) : "schon vorhanden"}</span>
|
||||
<select
|
||||
value={review.get(key)}
|
||||
aria-label={`Entscheidung für ${item.title}`}
|
||||
onChange={(event) => setReview((current) => new Map(current).set(key, event.target.value))}
|
||||
>
|
||||
<option value="skip">Nicht einfügen</option>
|
||||
<option value="insert">Trotzdem erneut einfügen</option>
|
||||
{bindable(matches) ? <option value="bind">Vorhandenes zusätzlich einbinden</option> : null}
|
||||
</select>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<div className="row">
|
||||
<button type="button" className="quiet" disabled={pending} onClick={() => setReview(null)}>Zurück zur Auswahl</button>
|
||||
<button type="button" disabled={pending} onClick={() => linkMany(review)}>Entscheidung ausführen</button>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
{items.length || picked.size ? (
|
||||
<button type="button" disabled={pending || picked.size === 0} onClick={linkMany}>
|
||||
<button type="button" disabled={pending || picked.size === 0 || Boolean(review)} onClick={() => linkMany()}>
|
||||
{picked.size ? `${picked.size} Belege einfügen` : "Belege auswählen"}
|
||||
</button>
|
||||
) : null}
|
||||
@@ -692,12 +829,23 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
</button>
|
||||
<div className="paperless-card__body">
|
||||
<strong>{item.title}</strong>
|
||||
<PresentNote matches={matchesOf(item)} />
|
||||
{facts.length ? <span className="paperless-card__facts">{facts.join(" · ")}</span> : null}
|
||||
{item.tags?.length ? (
|
||||
<ul className="paperless-card__tags" aria-label="Tags">
|
||||
{item.tags.map((tag) => <li key={tag}>{tag}</li>)}
|
||||
</ul>
|
||||
) : null}
|
||||
{item.custom_fields?.length ? (
|
||||
<dl className="paperless-card__fields" aria-label="Zusatzfelder">
|
||||
{item.custom_fields.map((field) => (
|
||||
<div key={field.name}>
|
||||
<dt>{field.name}</dt>
|
||||
<dd>{field.type === "date" && formatDate(field.value) !== "Ohne Datum" ? formatDate(field.value) : field.value}</dd>
|
||||
</div>
|
||||
))}
|
||||
</dl>
|
||||
) : null}
|
||||
{item.snippet ? <p className="paperless-card__snippet">{item.snippet}</p> : null}
|
||||
<div className="row">
|
||||
<button type="button" className="quiet" onClick={() => preview(item)} aria-expanded={open}>
|
||||
@@ -838,6 +986,7 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle =
|
||||
const [preview, setPreview] = useState(null);
|
||||
const [previewError, setPreviewError] = useState("");
|
||||
const [adopting, setAdopting] = useState(false);
|
||||
const { place, panel } = useExternalPlacement({ context, placement, onPlaced: () => onDone() });
|
||||
const [loading, setLoading] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const requestRef = useRef(0);
|
||||
@@ -1020,26 +1169,22 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle =
|
||||
if (cc) detail.message_cc = cc;
|
||||
if (attachments.length) detail.attachments = attachments;
|
||||
try {
|
||||
await api("/api/v1/external-refs", {
|
||||
method: "POST",
|
||||
body: {
|
||||
title: item.title,
|
||||
source: "imap",
|
||||
external_id: item.id,
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
...(item.document_on ? {
|
||||
dates: {
|
||||
document_on: item.document_on,
|
||||
sent_on: direction === "out" ? item.document_on : null,
|
||||
received_on: direction === "in" ? item.document_on : null,
|
||||
},
|
||||
} : {}),
|
||||
detail,
|
||||
},
|
||||
await place({
|
||||
title: item.title,
|
||||
source: "imap",
|
||||
external_id: item.id,
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
...(item.document_on ? {
|
||||
dates: {
|
||||
document_on: item.document_on,
|
||||
sent_on: direction === "out" ? item.document_on : null,
|
||||
received_on: direction === "in" ? item.document_on : null,
|
||||
},
|
||||
} : {}),
|
||||
detail,
|
||||
});
|
||||
onDone();
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
@@ -1120,6 +1265,7 @@ export function MailPicker({ context, placement, chapterId = "", chapterTitle =
|
||||
{selected && !preview && !previewError ? <p className="muted">Vorschau wird geladen …</p> : null}
|
||||
{preview ? <MailBody html={preview.html} text={preview.text} truncated={preview.text_truncated} notices={preview.notices} /> : null}
|
||||
<ErrorNote error={error} />
|
||||
{panel}
|
||||
<button type="button" onClick={() => link(selected)} disabled={!selected.uid || adopting}>{adopting ? "Wird übernommen …" : "Übernehmen"}</button>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
import { useState } from "react";
|
||||
import { api } from "../api.js";
|
||||
|
||||
// A document that is already present around the target. The server decides; this only asks and explains.
|
||||
export function presentKey(source, connectorId, externalId) {
|
||||
return `${source}:${connectorId || ""}:${externalId}`;
|
||||
}
|
||||
|
||||
export function describeMatch(match) {
|
||||
const title = match.container?.title;
|
||||
const maybe = match.certain ? "" : "möglicherweise ";
|
||||
if (match.where === "here") return `${maybe}schon hier${match.chapter ? ` (Kapitel „${match.chapter}“)` : ""}`;
|
||||
if (match.where === "inside") return `${maybe}schon ${title ? `in „${title}“` : "in einem zugehörigen Teil"}`;
|
||||
const kind = match.container?.kind === "case" ? "in der Akte" : "im Vorgang";
|
||||
return `${maybe}schon ${kind}${title ? ` „${title}“` : ""}`;
|
||||
}
|
||||
|
||||
export function bindable(matches) {
|
||||
return (matches || []).find((item) => item.certain && item.can_bind && item.where !== "here") || null;
|
||||
}
|
||||
|
||||
// entries: [{ source, external_id, connector_id }]. Answers a Map of presentKey -> matches, only for documents that are present.
|
||||
// A failed lookup answers nothing: the warning is a help, the server still protects on insert.
|
||||
export async function lookupPresent(context, placement, entries) {
|
||||
const found = new Map();
|
||||
if (!entries.length) return found;
|
||||
try {
|
||||
const data = await api("/api/v1/external-refs/existing", {
|
||||
method: "POST",
|
||||
body: {
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
items: entries.map((item) => ({ source: item.source, external_id: item.external_id, ...(item.connector_id ? { connector_id: item.connector_id } : {}) })),
|
||||
},
|
||||
});
|
||||
for (const entry of data.items || []) {
|
||||
if (entry.matches?.length) found.set(presentKey(entry.source, entry.connector_id, entry.external_id), entry.matches);
|
||||
}
|
||||
} catch {
|
||||
return found;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
export function PresentNote({ matches }) {
|
||||
if (!matches?.length) return null;
|
||||
return <p className="duplicate-note" role="status">Schon vorhanden: {matches.map(describeMatch).join(", ")}.</p>;
|
||||
}
|
||||
|
||||
// Choices when a document is already present: do not insert, insert again, or bind the existing element as well.
|
||||
export function DuplicatePanel({ matches, busy, onCancel, onInsert, onBind }) {
|
||||
const existing = bindable(matches);
|
||||
return (
|
||||
<div className="card stack duplicate-panel" role="alert">
|
||||
<strong>Dieses Dokument ist schon vorhanden.</strong>
|
||||
<ul>
|
||||
{matches.map((item) => <li key={item.element_id}>{item.title}: {describeMatch(item)}</li>)}
|
||||
</ul>
|
||||
<div className="row">
|
||||
<button type="button" className="quiet" disabled={busy} onClick={onCancel}>Nicht einfügen</button>
|
||||
<button type="button" className="quiet" disabled={busy} onClick={onInsert}>Trotzdem erneut einfügen</button>
|
||||
{existing ? <button type="button" disabled={busy} onClick={() => onBind(existing.element_id)}>Vorhandenes zusätzlich einbinden</button> : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// One document at a time. `place(body)` posts it. If the server finds it already present, `panel` offers the choices
|
||||
// and `onPlaced` runs once the document is in, whichever way.
|
||||
export function useExternalPlacement({ context, placement, onPlaced }) {
|
||||
const [conflict, setConflict] = useState(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
async function send(body) {
|
||||
await api("/api/v1/external-refs", { method: "POST", body });
|
||||
setConflict(null);
|
||||
await onPlaced?.();
|
||||
}
|
||||
|
||||
async function place(body) {
|
||||
setError("");
|
||||
try {
|
||||
await send(body);
|
||||
} catch (err) {
|
||||
if (err.code !== "already_present") throw err;
|
||||
const entry = { source: body.source, external_id: body.external_id, connector_id: body.detail?.connector_id };
|
||||
const found = await lookupPresent(context, placement, [entry]);
|
||||
const matches = found.get(presentKey(entry.source, entry.connector_id, entry.external_id)) || [];
|
||||
if (!matches.length) throw err;
|
||||
setConflict({ body, matches });
|
||||
}
|
||||
}
|
||||
|
||||
async function resolve(extra) {
|
||||
if (!conflict) return;
|
||||
setBusy(true);
|
||||
setError("");
|
||||
try {
|
||||
await send({ ...conflict.body, ...extra });
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const panel = conflict ? (
|
||||
<>
|
||||
<DuplicatePanel
|
||||
matches={conflict.matches}
|
||||
busy={busy}
|
||||
onCancel={() => setConflict(null)}
|
||||
onInsert={() => resolve({ duplicate: "insert" })}
|
||||
onBind={(elementId) => resolve({ bind_element_id: elementId })}
|
||||
/>
|
||||
{error ? <p className="error" role="alert">{error}</p> : null}
|
||||
</>
|
||||
) : null;
|
||||
return { place, panel };
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
|
||||
import { api } from "../api.js";
|
||||
import { TargetLine } from "../pages/content.jsx";
|
||||
import { ErrorNote } from "../pages/widgets.jsx";
|
||||
import { useExternalPlacement } from "./duplicates.jsx";
|
||||
import { defineSourceModules, moduleFor, registerSourceModule, sourceModules, unregisterSourceModule } from "./registry-core.js";
|
||||
|
||||
export { defineSourceModules, moduleFor, registerSourceModule, sourceModules, unregisterSourceModule };
|
||||
@@ -71,6 +72,7 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId
|
||||
const [error, setError] = useState("");
|
||||
const [pending, setPending] = useState(false);
|
||||
const searchRef = useRef(0);
|
||||
const { place, panel } = useExternalPlacement({ context, placement, onPlaced: () => { onInserted?.(); onDone?.(); } });
|
||||
const hint = module?.searchHint || "Suche im heute unterstützten Umfang dieser Quelle.";
|
||||
|
||||
useEffect(() => {
|
||||
@@ -111,20 +113,15 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId
|
||||
setPending(true);
|
||||
setError("");
|
||||
try {
|
||||
await api("/api/v1/external-refs", {
|
||||
method: "POST",
|
||||
body: {
|
||||
title: item.title,
|
||||
source: sourceKind,
|
||||
external_id: String(item.id),
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
...(connectorId ? { detail: { connector_id: connectorId } } : {}),
|
||||
},
|
||||
await place({
|
||||
title: item.title,
|
||||
source: sourceKind,
|
||||
external_id: String(item.id),
|
||||
context,
|
||||
...(placement ? { placement } : {}),
|
||||
...(chapterId ? { chapter_id: chapterId } : {}),
|
||||
...(connectorId ? { detail: { connector_id: connectorId } } : {}),
|
||||
});
|
||||
onInserted?.();
|
||||
onDone?.();
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} finally {
|
||||
@@ -149,6 +146,7 @@ export function StandardSourcePicker({ sourceKind, context, placement, chapterId
|
||||
<button type="submit">Suchen</button>
|
||||
</form>
|
||||
<ErrorNote error={error} />
|
||||
{panel}
|
||||
<p className="muted">{hint}</p>
|
||||
<div className="stack">
|
||||
{items.map((item) => (
|
||||
|
||||
+19
-3
@@ -1740,6 +1740,9 @@ button.pick-tree__twist {
|
||||
.paperless-search { display: grid; grid-template-columns: minmax(9rem, 12rem) minmax(0, 1fr) auto; gap: 8px; align-items: end; }
|
||||
.paperless-more > summary { cursor: pointer; font-weight: 600; min-height: 32px; }
|
||||
.paperless-tags { display: grid; gap: 2px; max-height: 12rem; overflow: auto; }
|
||||
.paperless-tags__actions { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||
.paperless-chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 0; padding: 0; list-style: none; }
|
||||
.paperless-chip { padding: 2px 10px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface2); color: var(--text); font-size: 13px; cursor: pointer; }
|
||||
.paperless-card {
|
||||
display: grid;
|
||||
grid-template-columns: auto 5rem minmax(0, 1fr);
|
||||
@@ -1766,6 +1769,11 @@ button.pick-tree__twist {
|
||||
}
|
||||
.paperless-card__tags { display: flex; flex-wrap: wrap; gap: 4px; margin: 0; padding: 0; list-style: none; }
|
||||
.paperless-card__tags li { padding: 0 8px; border-radius: 999px; background: var(--surface2); color: var(--text2); font-size: 12px; }
|
||||
.paperless-card__fields { display: flex; flex-wrap: wrap; gap: 2px 14px; margin: 0; font-size: 13px; }
|
||||
.paperless-card__fields div { display: flex; gap: 4px; min-width: 0; }
|
||||
.paperless-card__fields dt { color: var(--text3); }
|
||||
.paperless-card__fields dt::after { content: ":"; }
|
||||
.paperless-card__fields dd { margin: 0; color: var(--text1); font-weight: 600; overflow-wrap: anywhere; }
|
||||
.paperless-card__view { grid-column: 1 / -1; }
|
||||
.paperless-thumb {
|
||||
display: grid;
|
||||
@@ -1782,9 +1790,10 @@ button.pick-tree__twist {
|
||||
.paperless-thumb__image { display: block; width: 100%; height: 100%; object-fit: cover; object-position: top; }
|
||||
.paperless-thumb__empty { color: var(--text3); font-size: 11px; }
|
||||
@media (max-width: 899px) {
|
||||
.modal--large .paperless-layout { grid-template-columns: minmax(0, 1fr); overflow: auto; }
|
||||
.modal--large .paperless-layout__filters, .modal--large .paperless-layout__results { overflow: visible; border-right: 0; padding-right: 0; }
|
||||
.modal.modal--large { overflow: auto; }
|
||||
.modal.modal--large { overflow: auto; display: block; height: min(96vh, 1400px); }
|
||||
.modal.modal--large > .stack { display: grid; flex: none; overflow: visible; }
|
||||
.modal--large .paperless-layout { flex: none; grid-template-columns: minmax(0, 1fr); grid-auto-rows: max-content; overflow: visible; }
|
||||
.modal--large .paperless-layout__filters, .modal--large .paperless-layout__results { min-height: auto; overflow: visible; border-right: 0; padding-right: 0; }
|
||||
}
|
||||
@media (max-width: 600px) {
|
||||
.paperless-search { grid-template-columns: minmax(0, 1fr); }
|
||||
@@ -1828,3 +1837,10 @@ button.pick-tree__twist {
|
||||
.mini-icon--link { color: var(--kind-room); }
|
||||
.structure__links { margin-top: 12px; padding-top: 8px; border-top: 1px solid var(--border); }
|
||||
.structure__links h3 { font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text2); margin: 0 8px 4px; }
|
||||
|
||||
.duplicate-note { margin: 0; color: var(--text); font-size: 13px; font-weight: 600; }
|
||||
.duplicate-panel { border: 1px solid var(--border); background: var(--surface2); }
|
||||
.duplicate-panel ul { margin: 0; padding-left: 18px; }
|
||||
.duplicate-list { list-style: none; padding-left: 0 !important; display: grid; gap: 8px; }
|
||||
.duplicate-list li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between; }
|
||||
.duplicate-list select { max-width: 100%; }
|
||||
Reference in new issue
Block a user