feat(picker): Wochenlauf --send/--report, Gmail nur fuer Rueckblick
Gmail-Postfach seit 05/2026 ungenutzt: nur noch bei since < 2026-06-01. CLI-Default since = vor 10 Tagen; Bericht an eigenes Postfach. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wd9aeH8MKgesfbsaS3kpnx
This commit is contained in:
parent
ec1409001c
commit
4dff8234f1
3 changed files with 65 additions and 7 deletions
|
|
@ -3,6 +3,7 @@ Invoice Picker: Rechnungen aus Link-/Portal-Mails ziehen und für den Versand an
|
||||||
Läuft unabhängig vom Alt-Scan (/emails/scan-all); Dedupe über Message-ID, Postfach wird nur gelesen (readonly).
|
Läuft unabhängig vom Alt-Scan (/emails/scan-all); Dedupe über Message-ID, Postfach wird nur gelesen (readonly).
|
||||||
|
|
||||||
CLI (Cron/Rückblick): python -m backend.grabber --since 2025-01-01
|
CLI (Cron/Rückblick): python -m backend.grabber --since 2025-01-01
|
||||||
|
Wochenlauf (Cron NucBox So 06:00): python -m backend.grabber --send --report
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
@ -115,11 +116,14 @@ def _domain(sender: str) -> str:
|
||||||
return m[1].lower() if m else ""
|
return m[1].lower() if m else ""
|
||||||
|
|
||||||
|
|
||||||
def _mailboxes(db: Session):
|
GMAIL_UNTIL = date(2026, 6, 1) # seit Mai 2026 läuft info@ nur noch über Strato
|
||||||
"""Strato (Settings/Env) + optional Gmail (info@ lief bis Ende Mai 2026 über Google)."""
|
|
||||||
|
|
||||||
|
def _mailboxes(db: Session, since: date):
|
||||||
|
"""Strato (Settings/Env) + Gmail nur für Rückblicke vor Juni 2026 (info@ lief bis Ende Mai über Google)."""
|
||||||
cfg = get_imap_config(db)
|
cfg = get_imap_config(db)
|
||||||
yield cfg, FOLDERS
|
yield cfg, FOLDERS
|
||||||
if os.getenv("GMAIL_IMAP_PASSWORD"):
|
if os.getenv("GMAIL_IMAP_PASSWORD") and since < GMAIL_UNTIL:
|
||||||
# ponytail: nur "Alle Nachrichten" (\All) statt jedes Label einzeln; Dedupe über Message-ID
|
# ponytail: nur "Alle Nachrichten" (\All) statt jedes Label einzeln; Dedupe über Message-ID
|
||||||
yield {**cfg, "host": "imap.gmail.com", "port": 993, "user": os.getenv("GMAIL_IMAP_USER", "info@c-stone-dev.com"),
|
yield {**cfg, "host": "imap.gmail.com", "port": 993, "user": os.getenv("GMAIL_IMAP_USER", "info@c-stone-dev.com"),
|
||||||
"password": os.environ["GMAIL_IMAP_PASSWORD"]}, None
|
"password": os.environ["GMAIL_IMAP_PASSWORD"]}, None
|
||||||
|
|
@ -137,7 +141,7 @@ def run(db: Session, since: date, state: Optional[dict] = None) -> dict:
|
||||||
state = state if state is not None else {}
|
state = state if state is not None else {}
|
||||||
state.setdefault("errors", [])
|
state.setdefault("errors", [])
|
||||||
counts = state.setdefault("counts", {})
|
counts = state.setdefault("counts", {})
|
||||||
for cfg, folders in _mailboxes(db):
|
for cfg, folders in _mailboxes(db, since):
|
||||||
try:
|
try:
|
||||||
_scan(db, cfg, folders, since, state, counts)
|
_scan(db, cfg, folders, since, state, counts)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -189,11 +193,20 @@ if __name__ == "__main__":
|
||||||
from backend.database import SessionLocal
|
from backend.database import SessionLocal
|
||||||
|
|
||||||
p = argparse.ArgumentParser()
|
p = argparse.ArgumentParser()
|
||||||
p.add_argument("--since", help="YYYY-MM-DD (Default: vor 3 Tagen)")
|
p.add_argument("--since", help="YYYY-MM-DD (Default: vor 10 Tagen, passt zum Wochenlauf)")
|
||||||
|
p.add_argument("--send", action="store_true", help="danach status=queued an Accountable senden")
|
||||||
|
p.add_argument("--report", action="store_true", help="Kurzbericht per Mail an IMAP_USER")
|
||||||
a = p.parse_args()
|
a = p.parse_args()
|
||||||
since = datetime.strptime(a.since, "%Y-%m-%d").date() if a.since else date.today() - timedelta(days=3)
|
since = datetime.strptime(a.since, "%Y-%m-%d").date() if a.since else date.today() - timedelta(days=10)
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
print(run(db, since))
|
state = run(db, since)
|
||||||
|
if a.send:
|
||||||
|
from backend import mailer
|
||||||
|
state["versand"] = mailer.send_queued(db)
|
||||||
|
if a.report:
|
||||||
|
from backend import mailer
|
||||||
|
mailer.send_report(db, state, since)
|
||||||
|
print(state)
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,31 @@ def send_queued(db: Session) -> dict:
|
||||||
failed += 1
|
failed += 1
|
||||||
db.commit()
|
db.commit()
|
||||||
return {"sent": sent, "failed": failed}
|
return {"sent": sent, "failed": failed}
|
||||||
|
|
||||||
|
|
||||||
|
def report_text(db: Session, state: dict, since) -> str:
|
||||||
|
from sqlalchemy import func
|
||||||
|
totals = dict(db.query(Outbound.status, func.count()).group_by(Outbound.status).all())
|
||||||
|
manual = (db.query(Outbound).filter(Outbound.status == "manual", Outbound.email_date >= since)
|
||||||
|
.order_by(Outbound.email_date).all())
|
||||||
|
lines = [f"Invoice Picker – Wochenlauf ab {since:%d.%m.%Y}", "",
|
||||||
|
f"Neu: {state.get('counts') or 'nichts'}", f"Versand: {state.get('versand', '-')}",
|
||||||
|
f"Gesamtstand: {totals}", ""]
|
||||||
|
if manual:
|
||||||
|
lines.append("Manuell abholen (kein Direktlink/Portal):")
|
||||||
|
lines += [f"- {r.email_date:%d.%m.} {r.email_from}: {r.email_subject}" for r in manual]
|
||||||
|
if state.get("errors"):
|
||||||
|
lines += ["", "Fehler:"] + [f"- {e}" for e in state["errors"][:20]]
|
||||||
|
return "\n".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def send_report(db: Session, state: dict, since):
|
||||||
|
"""Kurzbericht an das eigene Postfach (SMTP-User = Markus), nie an Accountable."""
|
||||||
|
c = _cfg()
|
||||||
|
msg = EmailMessage()
|
||||||
|
msg["From"] = msg["To"] = c["user"]
|
||||||
|
msg["Subject"] = f"Invoice Picker Wochenbericht {datetime.now():%d.%m.%Y}"
|
||||||
|
msg.set_content(report_text(db, state, since))
|
||||||
|
with smtplib.SMTP_SSL(c["host"], c["port"], timeout=60) as s:
|
||||||
|
s.login(c["user"], c["password"])
|
||||||
|
s.send_message(msg)
|
||||||
|
|
|
||||||
|
|
@ -59,3 +59,20 @@ def test_aws(tmp_path, monkeypatch):
|
||||||
assert grabber.process_message(db, email.message_from_string(mail), "INBOX", str(tmp_path)) is None
|
assert grabber.process_message(db, email.message_from_string(mail), "INBOX", str(tmp_path)) is None
|
||||||
fake_aws = mail.replace("aws.com", "notaws.com").replace("<aws1>", "<aws2>")
|
fake_aws = mail.replace("aws.com", "notaws.com").replace("<aws1>", "<aws2>")
|
||||||
assert grabber.process_message(db, email.message_from_string(fake_aws), "INBOX", str(tmp_path)) == "manual"
|
assert grabber.process_message(db, email.message_from_string(fake_aws), "INBOX", str(tmp_path)) == "manual"
|
||||||
|
|
||||||
|
|
||||||
|
def test_gmail_nur_rueckblick_und_bericht(monkeypatch):
|
||||||
|
from datetime import date, datetime
|
||||||
|
from backend import mailer
|
||||||
|
db = sessionmaker(bind=create_engine("sqlite://"))()
|
||||||
|
Base.metadata.create_all(db.get_bind())
|
||||||
|
monkeypatch.setattr(grabber, "get_imap_config", lambda db: {"host": "imap.strato.de", "storage_path": "/x"})
|
||||||
|
monkeypatch.setenv("GMAIL_IMAP_PASSWORD", "x")
|
||||||
|
hosts = lambda since: [c["host"] for c, _ in grabber._mailboxes(db, since)]
|
||||||
|
assert hosts(date(2025, 1, 1)) == ["imap.strato.de", "imap.gmail.com"]
|
||||||
|
assert hosts(date(2026, 11, 5)) == ["imap.strato.de"]
|
||||||
|
db.add(Outbound(message_id="m1", status="manual", email_date=datetime(2026, 11, 10),
|
||||||
|
email_from="Shop", email_subject="Rechnung verfügbar"))
|
||||||
|
db.commit()
|
||||||
|
txt = mailer.report_text(db, {"counts": {"manual": 1}, "errors": []}, date(2026, 11, 5))
|
||||||
|
assert "Manuell abholen" in txt and "Rechnung verfügbar" in txt
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue