Zeige die Zusatzfelder in der Trefferliste und erkenne Vorschaubilder am Inhalt statt am Antwort-Header.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2f7de33049
commit
38406d3293
4 files changed
+89
-10
No files matched your search
+40
-4
@@ -437,6 +437,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),
|
||||
@@ -508,11 +509,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
|
||||
# Raster pictures only. A vector file could carry markup.
|
||||
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("Paperless hat kein Vorschaubild geliefert.")
|
||||
return data, media
|
||||
raise unprocessable(f"Paperless hat kein Vorschaubild geliefert (Antworttyp {header or 'unbekannt'}).")
|
||||
return media
|
||||
|
||||
|
||||
_LOOKUP_TTL = 60.0
|
||||
@@ -576,6 +585,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
|
||||
|
||||
@@ -472,6 +472,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 +490,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,6 +533,12 @@ 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"]})
|
||||
@@ -639,6 +663,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("/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
|
||||
@@ -663,6 +689,8 @@ def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeyp
|
||||
assert ok.headers["content-type"] == "image/webp"
|
||||
assert ok.headers["cache-control"] == "private, no-store"
|
||||
assert seen[-1] == ("token-alpha", "/api/documents/9/thumb/")
|
||||
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
|
||||
@@ -673,7 +701,7 @@ def test_paperless_thumbnail_is_bound_to_the_owners_instance(client, db, monkeyp
|
||||
register(db, ben.email, ben.name)
|
||||
ben.login()
|
||||
assert get(ben, 9).status_code == 404
|
||||
assert len(seen) == 4
|
||||
assert len(seen) == 5
|
||||
|
||||
|
||||
def test_revoked_grant_hides_bytes_fetched_during_the_call(client, db, monkeypatch):
|
||||
|
||||
@@ -265,21 +265,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);
|
||||
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;
|
||||
@@ -288,7 +288,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 }) {
|
||||
@@ -714,6 +714,16 @@ export function PaperlessPicker({ context, placement, chapterId = "", chapterTit
|
||||
{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}>
|
||||
|
||||
@@ -1766,6 +1766,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;
|
||||
|
||||
Reference in new issue
Block a user