Add scheduled and event-driven printing with a new web UI
CI / test (3.10) (push) Successful in 1m20s
CI / test (3.12) (push) Successful in 1m49s
CI / test (3.13) (push) Successful in 1m5s

The web server now runs a scheduler over schedules kept in SQLite
(~/.local/share/receipt-maker, outside the clone). A schedule is a
trigger, a receipt made of blocks, and options:

- Triggers: once, recurring (rrule-based, DST-safe), sun times,
  reminders before calendar (ICS) events, RSS/JSON watchers, webhooks
  with per-schedule secrets.
- Blocks, rendered fresh as one job: templated notes ({date},
  {days_until:..}, rotating {choice}, trigger vars), QR, saved images,
  random image, calendar agenda, feed headlines, dividers.
- Rules: quiet hours (hold/skip/ignore), daily cap, pause, catch-up
  window for missed runs (never replayed), retries on connect failure
  only; a mid-job drop (new PrinterDropped) is never resent.

receipt.py renders through composable Blocks (render_note/image/qr are
wrappers, output unchanged). Print history persists, with Print again.
The page moves to web/ with Print (note/image/QR + paper preview),
Schedules (presets, editor with live preview and next runs), History
and Settings tabs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 23:58:26 +01:00
co-authored by Claude Opus 5.5
parent fa99c3076c
commit ba92f1fbda
29 changed files with 5433 additions and 294 deletions
+102
View File
@@ -219,3 +219,105 @@ def test_serve_refuses_without_token(monkeypatch, capsys):
assert rc == 1
assert receipt_web.TOKEN_ENV in capsys.readouterr().err
SCHEDULE = {"name": "Chore", "trigger": {"type": "recurring", "times": ["08:00"]},
"blocks": [{"type": "note", "text": "Water the plants"}]}
def test_static_files_need_the_token(client):
assert client.get("/static/app.js").status_code == 401
res = client.get("/static/app.js", headers=HEADERS)
assert res.status_code == 200 and "javascript" in res.headers["content-type"]
assert client.get("/static/..%2Freceipt.py", headers=HEADERS).status_code == 404
def test_schedule_crud(client):
res = client.post("/api/schedules", headers=HEADERS, json=SCHEDULE)
assert res.status_code == 201, res.text
schedule = res.json()["schedule"]
assert schedule["summary"] == "Every day at 08:00"
assert schedule["next_run_at"]
edited = {**SCHEDULE, "trigger": {"type": "recurring", "times": ["09:15"],
"weekdays": [5, 6]}}
res = client.put(f"/api/schedules/{schedule['id']}", headers=HEADERS, json=edited)
assert res.json()["schedule"]["summary"] == "Weekends at 09:15"
res = client.post(f"/api/schedules/{schedule['id']}/duplicate", headers=HEADERS)
assert res.json()["schedule"]["enabled"] is False
assert len(client.get("/api/schedules", headers=HEADERS).json()["schedules"]) == 2
assert client.delete(f"/api/schedules/{schedule['id']}", headers=HEADERS).status_code == 200
assert client.get(f"/api/schedules/{schedule['id']}", headers=HEADERS).status_code == 404
@pytest.mark.parametrize("change,status", [
({"blocks": []}, 422),
({"trigger": {"type": "recurring", "times": []}}, 422),
({"trigger": {"type": "sun", "event": "sunset"}}, 400), # no location set
({"trigger": {"type": "calendar", "source_id": "missing"}}, 400),
({"trigger": {"type": "watch", "url": "file:///etc/passwd"}}, 422),
])
def test_schedule_validation(client, change, status):
assert client.post("/api/schedules", headers=HEADERS,
json={**SCHEDULE, **change}).status_code == status
def test_schedule_test_print(client, fake_printer):
schedule = client.post("/api/schedules", headers=HEADERS, json=SCHEDULE).json()["schedule"]
res = client.post(f"/api/schedules/{schedule['id']}/run", headers=HEADERS)
assert finished_job(client, res)["state"] == "done"
assert b"Water the plants" in fake_printer.payloads[0]
def test_preview(client):
res = client.post("/api/preview", headers=HEADERS, json={"blocks": [
{"type": "note", "text": "Hi {nope}"}, {"type": "qr", "content": "https://x.test"}]})
body = res.json()
assert body["parts"][0]["text"] == "Hi {nope}"
assert body["parts"][1]["image"].startswith("data:image/png;base64,")
assert body["warnings"] == ["Unknown variable {nope}"]
def test_webhook(client, fake_printer):
hook = {**SCHEDULE, "trigger": {"type": "webhook", "per_hour": 1},
"blocks": [{"type": "note", "text": "{payload.text}"}]}
schedule = client.post("/api/schedules", headers=HEADERS, json=hook).json()["schedule"]
path, secret = f"/{schedule['hook_path']}", schedule["secret"]
token = {"X-Receipt-Token": TOKEN}
assert client.post(path, content=b"hi").status_code == 401
assert client.post(path, headers=token, content=b"hi").status_code == 404
assert client.post(f"{path}?key=wrong", headers=token, content=b"hi").status_code == 404
res = client.post(path, headers={**token, "X-Hook-Secret": secret}, content=b"Buy milk")
assert finished_job(client, res)["state"] == "done"
assert b"Buy milk" in fake_printer.payloads[0]
res = client.post(f"{path}?key={secret}", headers=token, json={"text": "again"})
assert res.status_code == 429
big = b"x" * (receipt_web.MAX_HOOK_BODY + 1)
assert client.post(f"{path}?key={secret}", headers=token, content=big).status_code == 413
def test_history_and_reprint(client, fake_printer):
finished_job(client, client.post("/api/note", headers=HEADERS, json={"text": "again"}))
run = client.get("/api/runs", headers=HEADERS).json()["runs"][0]
assert (run["name"], run["state"], run["has_payload"]) == ("again", "done", 1)
res = client.post(f"/api/runs/{run['id']}/reprint", headers=HEADERS)
assert finished_job(client, res)["state"] == "done"
assert fake_printer.payloads[0] == fake_printer.payloads[1]
def test_source_in_use_cant_be_deleted(client):
source = client.post("/api/sources", headers=HEADERS, json={
"name": "Work", "kind": "ics", "url": "webcal://cal.test/x.ics"}).json()["source"]
assert source["url"] == "https://cal.test/x.ics"
client.post("/api/schedules", headers=HEADERS, json={
**SCHEDULE, "trigger": {"type": "calendar", "source_id": source["id"]}})
assert client.delete(f"/api/sources/{source['id']}", headers=HEADERS).status_code == 409