Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3Two gates, both about the same thing -- a scan is a record of what
4ARRIVED, 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
7because the first one was blurry, and forward the email version of
8something a colleague already snapped. Treating those as separate
9documents is how one dinner becomes two expenses. The content hash is the
10identity, per owner -- two businesses receiving the same invoice PDF are
11two genuinely separate facts.
12
13**Confirmed evidence is closed.** Once a human has turned a scan into an
14expense, the thing to correct is the expense; editing what the machine
15read afterwards would leave the ledger explained by evidence that no
16longer says what it said when somebody agreed to it (docs/logic-decisions.md
17#8).
18"""
19
20import os
21
22import 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
29def _base_dir():
30 return os.environ.get("DBBASIC_DATA_DIR", "data")
31
32
33def _text(value):
34 return str(value if value is not None else "").strip()
35
36
37def 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