redesign filter page: rule test, match stats, warnings, toggle

- Rule tester (POST /api/filters/test): evaluates an unsaved rule
  read-only against the newest matching mails in the source folder,
  using an IMAP SEARCH on the first text condition to narrow candidates.
  Shows matches, near misses with the failing condition, and whether
  any candidate had an attachment at all (would have caught the
  Klarmobil and it-moench misconfigurations immediately).
- Per-rule match_count / last_match_at (new migration), incremented by
  the scheduler on every match and shown in the list.
- PATCH /api/filters/{id}/enabled toggles a rule without resetting its
  processed state (a full PUT re-evaluates every mail for that rule).
- New filter list: cards with enable switch, priority, condition and
  action chips, match stats, and warnings (display name in from/to,
  "am (exakt)" date, duplicates, empty values, invalid regex, move
  without folder). Quick filters for active/inactive/warnings/never
  matched; copy-as-template; remembered account selection.
- Edit dialog: wider layout, live warnings, integrated test panel,
  sensible defaults for new rules (from + date after Jan 1 + move).
- Light global refresh: nav with active item, page background, cards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 09:48:12 +02:00
co-authored by Claude Opus 5.5
parent ce7caf5dfe
commit 1b4f6ed34e
9 changed files with 849 additions and 377 deletions
+122
View File
@@ -0,0 +1,122 @@
"""Testet eine (noch nicht gespeicherte) Regel read-only gegen echte Mails im
Quellordner. Liefert Treffer und Beinahe-Treffer samt der Bedingung, an der
es scheitert — damit Fehlkonfigurationen (Anzeigename im Absender, fehlender
Anhang, falscher Betreff) schon beim Anlegen auffallen."""
import logging
import re
from datetime import datetime, timedelta
from app.models.db_models import Account, ConditionField, MatchType
from app.services.encryption import decrypt
from app.services.filter_engine import _parse_date_value, evaluate_conditions
from app.services.imap_client import IMAPClient
logger = logging.getLogger(__name__)
_SEARCH_KEYS = {
ConditionField.FROM: "FROM",
ConditionField.TO: "TO",
ConditionField.SUBJECT: "SUBJECT",
}
def _imap_date(d: datetime) -> str:
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
return f"{d.day:02d}-{months[d.month - 1]}-{d.year}"
def _search_term(value: str) -> str | None:
"""Suchbegriff für IMAP SEARCH: bei 'Name <adresse>' nur die Adresse.
Nicht-ASCII-Begriffe werden serverseitig nicht zuverlässig unterstützt."""
m = re.search(r"<([^>]+)>", value)
term = " ".join((m.group(1) if m else value).split())
if not term or not term.isascii():
return None
return term.replace("\\", "\\\\").replace('"', '\\"')
def _build_search(conditions) -> list[str]:
"""Grenzt die Kandidaten serverseitig ein, damit nicht 150k Mails geladen
werden müssen. Die eigentliche Prüfung macht danach evaluate_conditions."""
criteria: list[str] = []
for c in conditions:
if c.negate:
continue
if c.field in _SEARCH_KEYS and c.match_type in (MatchType.CONTAINS, MatchType.EXACT):
term = _search_term(c.value)
if term:
criteria += [_SEARCH_KEYS[c.field], f'"{term}"']
break
since = None
for c in conditions:
if c.field == ConditionField.DATE and not c.negate and c.match_type in (MatchType.AFTER, MatchType.ON_DATE):
since = _parse_date_value(c.value)
break
if not criteria and since is None:
# Ohne Anhaltspunkt: nur die letzten 60 Tage
since = datetime.utcnow() - timedelta(days=60)
if since is not None:
criteria += ["SINCE", _imap_date(since)]
return criteria
def test_rule(account: Account, source_folder: str, conditions, limit: int = 40) -> dict:
limit = max(1, min(limit, 100))
criteria = _build_search(conditions)
client = IMAPClient(
host=account.imap_host,
port=account.imap_port,
username=account.username,
password=decrypt(account.password),
use_ssl=account.use_ssl,
)
with client:
if not client.select_folder(source_folder, readonly=True):
raise ValueError(f"Ordner '{source_folder}' konnte nicht geöffnet werden")
status, data = client.conn.uid("SEARCH", None, *criteria)
uids = data[0].split() if status == "OK" and data and data[0] else []
candidates = len(uids)
# Neueste zuerst
uids = sorted(uids, key=int, reverse=True)[:limit]
matches, misses = [], []
failed_on: dict[str, int] = {}
attachments_seen = 0
for raw_uid in uids:
uid = raw_uid.decode() if isinstance(raw_uid, bytes) else str(raw_uid)
try:
mail = client.fetch_mail(uid)
except Exception as e:
misses.append({"uid": uid, "from": "", "subject": "", "date": None,
"has_attachment": False, "reason": f"Abruf fehlgeschlagen: {e}"})
continue
if not mail:
continue
if mail.has_attachment:
attachments_seen += 1
ok, details = evaluate_conditions(mail, conditions)
entry = {
"uid": uid,
"from": mail.from_addr,
"subject": mail.subject,
"date": mail.date.isoformat() if mail.date else None,
"has_attachment": mail.has_attachment,
}
if ok:
matches.append(entry)
else:
reason = details[-1] if details else ""
entry["reason"] = reason
misses.append(entry)
field = reason.split("(", 1)[0].replace("NOT ", "").strip() or "?"
failed_on[field] = failed_on.get(field, 0) + 1
return {
"search": " ".join(criteria) or "ALL",
"candidates": candidates,
"checked": len(matches) + len(misses),
"matches": matches,
"misses": misses,
"failed_on": failed_on,
"attachments_seen": attachments_seen,
}
+2
View File
@@ -201,6 +201,8 @@ def _poll_account_sync(account_id: int) -> None:
if matched:
total_matched += 1
rule.match_count = (rule.match_count or 0) + 1
rule.last_match_at = datetime.utcnow()
# Aktionen ausführen
for action in rule.actions:
success = execute_action(