Führe die Konfigurationsansicht der Connectoren ein, damit Paperless und IMAP ihre Felder selbst deklarieren.
Das Modul liefert ein Feldmanifest und validiert es; ein leeres Geheimnisfeld behält das gespeicherte. Die Oberfläche rendert die Felder generisch in einer Detailansicht je Zugang mit Prüfstand, Fähigkeiten und Verwendung. Die providerspezifischen Formulare in Sources.jsx entfallen, die flachen API-Felder bleiben als Kompatibilität. Das Standardfach eines IMAP-Kontos öffnet der Picker zuerst. Die Backend-Tests wurden in der Entwicklungsumgebung nicht ausgeführt; der Lauf auf develop steht aus. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c29bd29f34
commit
eb58dce4cd
20 files changed
+1071
-244
No files matched your search
+155
-34
@@ -44,15 +44,47 @@ from app.sourceaccess import (
|
||||
release_for_external,
|
||||
)
|
||||
from app.sourceport import CAPABILITIES
|
||||
from app.sourceregistry import SourceModule, get_source, register_source
|
||||
from app.sourceregistry import ConfigField, SourceModule, get_source, parse_fields, register_source, require_source
|
||||
|
||||
|
||||
_PUBLIC_COLUMNS = """
|
||||
id, kind, name, settings, created_at,
|
||||
secret <> '' AS has_secret,
|
||||
last_probe_at, last_probe_ok, last_probe_detail
|
||||
"""
|
||||
|
||||
|
||||
def parse_connector(
|
||||
kind: str,
|
||||
name: str,
|
||||
flat: dict,
|
||||
config: dict | None,
|
||||
current: dict | None = None,
|
||||
) -> tuple[str, dict, str]:
|
||||
"""Validate a connector body with the registered module. ``current`` is the stored row on change."""
|
||||
module = require_source(kind)
|
||||
name = " ".join(str(name or "").split())
|
||||
values = {key: value for key, value in flat.items() if value is not None}
|
||||
values.update(config or {})
|
||||
if module.config_fields:
|
||||
settings, secret = parse_fields(
|
||||
module.config_fields,
|
||||
values,
|
||||
current["settings"] if current else None,
|
||||
current["secret"] if current else "",
|
||||
)
|
||||
if module.normalize_config is not None:
|
||||
settings = module.normalize_config(settings, secret)
|
||||
return name, settings, secret
|
||||
return module.parse_config({**values, "kind": kind, "name": name, "config": config or {}})
|
||||
|
||||
|
||||
def save_connector(conn, owner_id: UUID, kind: str, name: str, settings: dict, secret: str) -> dict:
|
||||
row = conn.execute(
|
||||
"""
|
||||
f"""
|
||||
INSERT INTO connectors (owner_id, kind, name, settings, secret)
|
||||
VALUES (%s, %s, %s, %s, %s)
|
||||
RETURNING id, kind, name, settings, created_at
|
||||
RETURNING {_PUBLIC_COLUMNS}
|
||||
""",
|
||||
(owner_id, kind, name, Json(settings), secret),
|
||||
).fetchone()
|
||||
@@ -63,11 +95,12 @@ def update_connector(conn, owner_id: UUID, connector_id: UUID, name: str, settin
|
||||
"""Credential or name change on the same instance. The id does not move."""
|
||||
current = _owned(conn, owner_id, connector_id)
|
||||
row = conn.execute(
|
||||
"""
|
||||
f"""
|
||||
UPDATE connectors
|
||||
SET name = %s, settings = %s, secret = %s
|
||||
SET name = %s, settings = %s, secret = %s,
|
||||
last_probe_at = NULL, last_probe_ok = NULL, last_probe_detail = NULL
|
||||
WHERE id = %s AND owner_id = %s
|
||||
RETURNING id, kind, name, settings, created_at
|
||||
RETURNING {_PUBLIC_COLUMNS}
|
||||
""",
|
||||
(name, Json(settings), secret, current["id"], owner_id),
|
||||
).fetchone()
|
||||
@@ -76,8 +109,8 @@ def update_connector(conn, owner_id: UUID, connector_id: UUID, name: str, settin
|
||||
|
||||
def list_connectors(conn, owner_id: UUID) -> list[dict]:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, kind, name, settings, created_at
|
||||
f"""
|
||||
SELECT {_PUBLIC_COLUMNS}
|
||||
FROM connectors WHERE owner_id = %s
|
||||
ORDER BY kind, name
|
||||
""",
|
||||
@@ -86,6 +119,30 @@ def list_connectors(conn, owner_id: UUID) -> list[dict]:
|
||||
return [_public(row) for row in rows]
|
||||
|
||||
|
||||
def connector_detail(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
"""One own instance with its use. The secret itself is never part of it."""
|
||||
row = conn.execute(
|
||||
f"SELECT {_PUBLIC_COLUMNS} FROM connectors WHERE id = %s AND owner_id = %s",
|
||||
(connector_id, owner_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise not_found()
|
||||
used = conn.execute(
|
||||
"""
|
||||
SELECT count(*) AS total
|
||||
FROM external_references er
|
||||
JOIN elements e ON e.id = er.element_id
|
||||
WHERE (er.connector_id = %s OR er.detail ->> 'connector_id' = %s)
|
||||
AND e.deleted_at IS NULL AND e.owner_subject_id = %s
|
||||
""",
|
||||
(connector_id, str(connector_id), owner_id),
|
||||
).fetchone()
|
||||
body = _public(row)
|
||||
body["references"] = int(used["total"])
|
||||
body["protection_profile"] = "legacy-plaintext"
|
||||
return body
|
||||
|
||||
|
||||
def delete_connector(conn, owner_id: UUID, connector_id: UUID) -> None:
|
||||
row = conn.execute(
|
||||
"DELETE FROM connectors WHERE id = %s AND owner_id = %s RETURNING id",
|
||||
@@ -101,11 +158,30 @@ def probe(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
if module is None:
|
||||
raise unprocessable("Diese Quelle ist nicht registriert.")
|
||||
release_for_external(conn)
|
||||
result = module.probe(row)
|
||||
try:
|
||||
result = module.probe(row)
|
||||
except Problem as exc:
|
||||
assert_still_owned(conn, row["id"], owner_id)
|
||||
_record_probe(conn, row["id"], False, exc.detail)
|
||||
conn.commit()
|
||||
raise
|
||||
assert_still_owned(conn, row["id"], owner_id)
|
||||
_record_probe(conn, row["id"], bool(result.get("ok", True)), str(result.get("detail") or ""))
|
||||
return result
|
||||
|
||||
|
||||
def _record_probe(conn, connector_id: UUID, ok: bool, detail: str) -> None:
|
||||
"""Display only. The detail comes from the module and must not carry a secret."""
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE connectors
|
||||
SET last_probe_at = now(), last_probe_ok = %s, last_probe_detail = %s
|
||||
WHERE id = %s
|
||||
""",
|
||||
(ok, detail[:300] or None, connector_id),
|
||||
)
|
||||
|
||||
|
||||
def _paperless_choice(value, label: str) -> str | None:
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
@@ -325,6 +401,11 @@ def _owned(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
return row
|
||||
|
||||
|
||||
def owned_connector(conn, owner_id: UUID, connector_id: UUID) -> dict:
|
||||
"""The stored row of an own instance, secret included. For server-side use only."""
|
||||
return _owned(conn, owner_id, connector_id)
|
||||
|
||||
|
||||
def _public(row) -> dict:
|
||||
return {
|
||||
"id": str(row["id"]),
|
||||
@@ -333,6 +414,14 @@ def _public(row) -> dict:
|
||||
"settings": row["settings"],
|
||||
"created_at": row["created_at"].strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"capabilities": (get_source(row["kind"]).capabilities if get_source(row["kind"]) else None),
|
||||
"has_secret": bool(row["has_secret"]),
|
||||
"probe": {
|
||||
"at": row["last_probe_at"].strftime("%Y-%m-%dT%H:%M:%SZ"),
|
||||
"ok": bool(row["last_probe_ok"]),
|
||||
"detail": row["last_probe_detail"] or "",
|
||||
}
|
||||
if row["last_probe_at"]
|
||||
else None,
|
||||
}
|
||||
|
||||
|
||||
@@ -1456,32 +1545,62 @@ def _search_rfc822(client: imaplib.IMAP4, expected_id: str, max_bytes: int) -> l
|
||||
return found
|
||||
|
||||
|
||||
def _paperless_config(payload: dict) -> tuple[str, dict, str]:
|
||||
base_url = str(payload.get("base_url") or "").strip()
|
||||
token = str(payload.get("token") or "").strip()
|
||||
name = str(payload.get("name") or "").strip()
|
||||
if not base_url or len(token) < 8:
|
||||
raise unprocessable("Paperless braucht eine Adresse und einen API-Schlüssel.")
|
||||
return name, {"base_url": clean_base_url(base_url)}, token
|
||||
PAPERLESS_FIELDS = (
|
||||
ConfigField(
|
||||
"base_url",
|
||||
"Adresse",
|
||||
type="url",
|
||||
required=True,
|
||||
placeholder="https://paperless.example",
|
||||
help="Adresse deiner Paperless-Instanz ohne Pfad.",
|
||||
max_length=300,
|
||||
),
|
||||
ConfigField(
|
||||
"token",
|
||||
"API-Schlüssel",
|
||||
type="password",
|
||||
required=True,
|
||||
secret=True,
|
||||
help="Den Schlüssel erzeugst du in Paperless unter deinem Profil.",
|
||||
max_length=200,
|
||||
),
|
||||
)
|
||||
|
||||
IMAP_FIELDS = (
|
||||
ConfigField("host", "Server", required=True, placeholder="imap.example", max_length=200),
|
||||
ConfigField("port", "Port", type="number", required=True, default=993, minimum=1, maximum=65535),
|
||||
ConfigField(
|
||||
"security",
|
||||
"Verbindung",
|
||||
type="select",
|
||||
required=True,
|
||||
default="ssl",
|
||||
options=(("ssl", "SSL"), ("starttls", "STARTTLS")),
|
||||
),
|
||||
ConfigField("username", "Benutzername", required=True, max_length=200),
|
||||
ConfigField("password", "Passwort", type="password", required=True, secret=True, max_length=500),
|
||||
ConfigField(
|
||||
"mailbox",
|
||||
"Standardfach",
|
||||
default="INBOX",
|
||||
help="Dieses Fach öffnet die Auswahl zuerst.",
|
||||
max_length=200,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _imap_config(payload: dict) -> tuple[str, dict, str]:
|
||||
host = str(payload.get("host") or "").strip()
|
||||
username = str(payload.get("username") or "").strip()
|
||||
password = str(payload.get("password") or "")
|
||||
name = str(payload.get("name") or "").strip()
|
||||
if not host or not username or not password:
|
||||
raise unprocessable("Ein E-Mail-Konto braucht Server, Benutzername und Passwort.")
|
||||
if " " in host or len(host) > 200:
|
||||
def _paperless_normalize(settings: dict, secret: str) -> dict:
|
||||
if len(secret.strip()) < 8:
|
||||
raise unprocessable("Der API-Schlüssel ist zu kurz.")
|
||||
return {**settings, "base_url": clean_base_url(settings["base_url"])}
|
||||
|
||||
|
||||
def _imap_normalize(settings: dict, secret: str) -> dict:
|
||||
del secret
|
||||
if " " in settings["host"]:
|
||||
raise unprocessable("Der Mailserver ist ungültig.")
|
||||
port = payload.get("port") or 993
|
||||
return name, {
|
||||
"host": host,
|
||||
"port": int(port),
|
||||
"username": username,
|
||||
"mailbox": str(payload.get("mailbox") or "INBOX").strip() or "INBOX",
|
||||
"security": payload.get("security") or "ssl",
|
||||
}, password
|
||||
_check_mailbox(settings.get("mailbox") or "INBOX")
|
||||
return settings
|
||||
|
||||
|
||||
def _paperless_find(conn, owner_id: UUID, connector_id: UUID | None, query: str, options: dict) -> dict:
|
||||
@@ -1624,7 +1743,8 @@ def _install_sources() -> None:
|
||||
label="Paperless",
|
||||
representation="pdf",
|
||||
capabilities=CAPABILITIES["paperless"],
|
||||
parse_config=_paperless_config,
|
||||
config_fields=PAPERLESS_FIELDS,
|
||||
normalize_config=_paperless_normalize,
|
||||
probe=paperless.probe,
|
||||
find=paperless.find,
|
||||
open_original=read_linked_paperless,
|
||||
@@ -1638,7 +1758,8 @@ def _install_sources() -> None:
|
||||
label="E-Mail",
|
||||
representation="email",
|
||||
capabilities=CAPABILITIES["imap"],
|
||||
parse_config=_imap_config,
|
||||
config_fields=IMAP_FIELDS,
|
||||
normalize_config=_imap_normalize,
|
||||
probe=imap.probe,
|
||||
find=imap.find,
|
||||
open_original=_imap_bytes,
|
||||
|
||||
Reference in new issue
Block a user