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
app_settings.py · 2.7 KB · 70 lines
1"""Pre-write hook for app_settings: a key is configured once.
2
3app_settings is read by forty-odd objects, each through its own small
4`_setting(base, key)` helper, and every one of them scans the collection
5and returns the FIRST row whose key matches. That is a perfectly
6reasonable reader. It is only dangerous because nothing stopped a second
7row with the same key from existing -- and when one does, it does not
8conflict, it does not warn, it simply loses. The newer value is invisible
9and the operator has no way to see why their change did nothing.
10
11This is not hypothetical. It shipped a wrong journal on the live box:
12`inventory.journal.inventory_account` had an old verification value, a
13correct one was added beside it, and the very next sale credited the
14stale account. The books balanced. The entry was wrong.
15
16So the same posture as the provenance rule in hook_wallet_entries
17(docs/logic-decisions.md #7): stop honouring an invariant in every reader
18and start ENFORCING it at the one write path they all share. A duplicate
19key is refused with the id of the row that already owns it, because the
20operator's next question is always "then where IS it set?".
21
22Updates are allowed to keep their own key -- that is just editing a
23setting -- and only a COLLISION with a different row is refused.
24"""
25
26import os
27
28import object_records
29
30COLLECTION = "app_settings"
31
32
33def _base_dir():
34 return os.environ.get("DBBASIC_DATA_DIR", "data")
35
36
37def BEFORE_WRITE(request):
38 action = request.get("action")
39 if action not in ("create", "update"):
40 return None
41 record = request.get("record") or {}
42 key = str(record.get("key") or "").strip()
43 if not key:
44 return None # schema validation owns the required-field error
45
46 record_id = str(record.get("id") or "")
47 try:
48 rows = object_records.read_collection_records(
49 COLLECTION, base_dir=_base_dir())
50 except Exception:
51 # An unreadable settings collection is not this hook's problem to
52 # report; the write path itself will fail properly if it matters.
53 return None
54
55 for row in rows:
56 if str(row.get("key") or "").strip() != key:
57 continue
58 if str(row.get("id") or "") == record_id:
59 continue # editing the row that already owns this key
60 return {
61 "error": (
62 f"Setting {key!r} is already configured (record "
63 f"{row.get('id')}, value {row.get('value')!r}). A key is "
64 f"configured once: readers take the first row that matches, "
65 f"so a second row would not override it -- it would be "
66 f"silently ignored. Edit that record instead."
67 ),
68 "status": 409,
69 }
70 return None