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
app_settings.py · 2.7 KB · 70 lines
| 1 | """Pre-write hook for app_settings: a key is configured once. |
| 2 | |
| 3 | app_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 |
| 5 | and returns the FIRST row whose key matches. That is a perfectly |
| 6 | reasonable reader. It is only dangerous because nothing stopped a second |
| 7 | row with the same key from existing -- and when one does, it does not |
| 8 | conflict, it does not warn, it simply loses. The newer value is invisible |
| 9 | and the operator has no way to see why their change did nothing. |
| 10 | |
| 11 | This is not hypothetical. It shipped a wrong journal on the live box: |
| 12 | `inventory.journal.inventory_account` had an old verification value, a |
| 13 | correct one was added beside it, and the very next sale credited the |
| 14 | stale account. The books balanced. The entry was wrong. |
| 15 | |
| 16 | So the same posture as the provenance rule in hook_wallet_entries |
| 17 | (docs/logic-decisions.md #7): stop honouring an invariant in every reader |
| 18 | and start ENFORCING it at the one write path they all share. A duplicate |
| 19 | key is refused with the id of the row that already owns it, because the |
| 20 | operator's next question is always "then where IS it set?". |
| 21 | |
| 22 | Updates are allowed to keep their own key -- that is just editing a |
| 23 | setting -- and only a COLLISION with a different row is refused. |
| 24 | """ |
| 25 | |
| 26 | import os |
| 27 | |
| 28 | import object_records |
| 29 | |
| 30 | COLLECTION = "app_settings" |
| 31 | |
| 32 | |
| 33 | def _base_dir(): |
| 34 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 35 | |
| 36 | |
| 37 | def 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 |