Verlege die Paperless-Suchparameter und das Vorschaubild in das Modul und korrigiere das Handy-Layout der großen Auswahl.
Die gemeinsame Suchroute kennt keine Filternamen mehr; das Modul deklariert sie und unbekannte Parameter werden abgewiesen. Unvollständige Zusatzfeld-Zeilen werden gemeldet statt weggelassen. Vorschaubilder nur als Rastergrafik. Abschlussmatrix und API-Referenz nachgeführt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f580fb282d
commit
5e32ba547d
9 files changed
+127
-99
No files matched your search
+25
-69
@@ -4,7 +4,7 @@ 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
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
|
||||
@@ -434,29 +405,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 +435,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")
|
||||
|
||||
@@ -71,19 +71,12 @@ EDITORS = {
|
||||
}
|
||||
|
||||
UNSUPPORTED_SEARCH = (
|
||||
"mode",
|
||||
"title",
|
||||
"correspondent",
|
||||
"document_type",
|
||||
"storage_path",
|
||||
"tag",
|
||||
"tag_mode",
|
||||
"document_from",
|
||||
"document_to",
|
||||
"added_from",
|
||||
"added_to",
|
||||
"asn",
|
||||
"custom",
|
||||
"sort",
|
||||
"cursor",
|
||||
)
|
||||
|
||||
@@ -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] = {}
|
||||
|
||||
+25
-1
@@ -478,6 +478,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.")
|
||||
@@ -489,7 +510,8 @@ def paperless_thumbnail(conn, owner_id: UUID, document_id: str, connector_id: UU
|
||||
if fetched is None:
|
||||
raise not_found()
|
||||
data, media, _name = fetched
|
||||
if not media.startswith("image/"):
|
||||
# Raster pictures only. A vector file could carry markup.
|
||||
if media not in _THUMBNAIL_MEDIA:
|
||||
raise unprocessable("Paperless hat kein Vorschaubild geliefert.")
|
||||
return data, media
|
||||
|
||||
@@ -2086,6 +2108,8 @@ 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",
|
||||
|
||||
@@ -520,12 +520,20 @@ def test_paperless_default_search_reads_title_and_content_and_shows_a_preview(cl
|
||||
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",
|
||||
@@ -631,6 +639,8 @@ 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("/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 +653,27 @@ 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
|
||||
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) == 4
|
||||
|
||||
|
||||
def test_revoked_grant_hides_bytes_fetched_during_the_call(client, db, monkeypatch):
|
||||
|
||||
Reference in new issue
Block a user