askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
scans.py · 3.0 KB · 74 lines
| 1 | """Pre-write hook for scans: one document, one scan; evidence stays put. |
| 2 | |
| 3 | Two gates, both about the same thing -- a scan is a record of what |
| 4 | ARRIVED, and neither a person nor a retry may quietly change that. |
| 5 | |
| 6 | **The same bytes are the same scan.** People photograph a receipt twice |
| 7 | because the first one was blurry, and forward the email version of |
| 8 | something a colleague already snapped. Treating those as separate |
| 9 | documents is how one dinner becomes two expenses. The content hash is the |
| 10 | identity, per owner -- two businesses receiving the same invoice PDF are |
| 11 | two genuinely separate facts. |
| 12 | |
| 13 | **Confirmed evidence is closed.** Once a human has turned a scan into an |
| 14 | expense, the thing to correct is the expense; editing what the machine |
| 15 | read afterwards would leave the ledger explained by evidence that no |
| 16 | longer says what it said when somebody agreed to it (docs/logic-decisions.md |
| 17 | #8). |
| 18 | """ |
| 19 | |
| 20 | import os |
| 21 | |
| 22 | import object_records |
| 23 | |
| 24 | # What may still move after a scan is confirmed. Nothing that changes what |
| 25 | # the document said or what it became. |
| 26 | _ALLOWED_AFTER_CONFIRM = {"notes", "status"} |
| 27 | |
| 28 | |
| 29 | def _base_dir(): |
| 30 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 31 | |
| 32 | |
| 33 | def _text(value): |
| 34 | return str(value if value is not None else "").strip() |
| 35 | |
| 36 | |
| 37 | def BEFORE_WRITE(request): |
| 38 | action = request.get("action") |
| 39 | record = dict(request.get("record") or {}) |
| 40 | existing = request.get("existing") or {} |
| 41 | changes = request.get("changes") or {} |
| 42 | |
| 43 | if action == "create": |
| 44 | digest = _text(record.get("content_sha256")) |
| 45 | if not digest: |
| 46 | return None # text-only intake without bytes |
| 47 | owner = _text(record.get("owner_id")) |
| 48 | try: |
| 49 | rows = object_records.read_collection_records("scans", base_dir=_base_dir()) |
| 50 | except Exception: |
| 51 | return {"error": ("Scan history unreadable; refusing to record " |
| 52 | "against an unknown history."), "status": 503} |
| 53 | for row in rows: |
| 54 | if (_text(row.get("content_sha256")) == digest |
| 55 | and _text(row.get("owner_id")) == owner): |
| 56 | return {"error": (f"This document is already here as scan " |
| 57 | f"{row['id']}. Photographing the same receipt " |
| 58 | f"twice must not become two expenses."), |
| 59 | "status": 409} |
| 60 | return None |
| 61 | |
| 62 | if action == "update" and _text(existing.get("status")) == "confirmed": |
| 63 | touched = [field for field in changes if field not in _ALLOWED_AFTER_CONFIRM] |
| 64 | if touched: |
| 65 | return {"error": ( |
| 66 | f"This scan has been confirmed as " |
| 67 | f"{_text(existing.get('confirmed_record')) or 'a record'}. " |
| 68 | f"Correct that record, not the evidence it came from " |
| 69 | f"({', '.join(sorted(touched))})."), "status": 409} |
| 70 | if _text(record.get("status", "confirmed")) != "confirmed": |
| 71 | return {"error": ("A confirmed scan stays confirmed. Reverse the " |
| 72 | "record it produced instead."), "status": 409} |
| 73 | |
| 74 | return None |