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.
counter_history.py · 4.8 KB · 107 lines
| 1 | """action_counter_history -- the change history of the shop's sales, for the |
| 2 | shift lead or manager who has to answer for it. |
| 3 | |
| 4 | POST {"collection": "order_lines", "record_id": "..."} one record's history |
| 5 | POST {"collection": "refunds", "limit": 50} the collection's latest |
| 6 | |
| 7 | **What was wrong (Corner Cup, a Haiku simulation of a counter cafe).** The |
| 8 | change log (object_record_changes) already said who changed what and when, |
| 9 | but it was readable only through /admin/... with the admin token, so a |
| 10 | manager asking "who turned this soup into a salad?" had to borrow the |
| 11 | owner's credentials, and the entries never said WHY. The reason now |
| 12 | travels with the write (X-DBBASIC-Change-Reason on an HTTP write; the |
| 13 | counter actions set their own: "void (customer_error): changed mind") and |
| 14 | is the entry's message. |
| 15 | |
| 16 | **What it shows.** Only these collections (the sale and its corrections): |
| 17 | SHOP_COLLECTIONS below. Only entries for records the permission engine lets |
| 18 | the caller READ, judged on the record as it stands now or, for a deleted |
| 19 | one, as it last stood -- the same rows the records API would show them, so |
| 20 | the history is not a side door to a record they could not open. Execute is |
| 21 | granted to shift leads, managers and the owner; a cashier reads the records |
| 22 | themselves but not their history. The admin routes are unchanged. |
| 23 | """ |
| 24 | |
| 25 | from __future__ import annotations |
| 26 | |
| 27 | import os |
| 28 | |
| 29 | import object_access |
| 30 | import object_corrections |
| 31 | import object_record_changes |
| 32 | import object_records |
| 33 | |
| 34 | SHOP_COLLECTIONS = ("orders", "order_lines", "invoices", "invoice_lines", "payments", |
| 35 | "refunds", "disputes", "goodwill", "remakes", "waste_logs", |
| 36 | "counter_approvals", "correction_reasons") |
| 37 | FIELDS = ("change_id", "timestamp", "record_id", "action", "actor", "message", |
| 38 | "changed_fields", "before", "after") |
| 39 | |
| 40 | |
| 41 | def _base_dir(): |
| 42 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 43 | |
| 44 | |
| 45 | def _readable_entry(access, collection, entry): |
| 46 | """The entry with only the fields the caller may read: a snapshot is cut |
| 47 | down by the same rule and schema redaction the records API applies, a |
| 48 | snapshot of a row the caller may not read at that moment is shown empty, |
| 49 | and a field they cannot read is not listed as changed (its name and the |
| 50 | fact it changed are as hidden as its value).""" |
| 51 | shown = {k: entry.get(k) for k in FIELDS} |
| 52 | try: |
| 53 | enforcing = object_access.enforcement_enabled() |
| 54 | except Exception: # noqa: BLE001 - mode unknown: redact |
| 55 | enforcing = True |
| 56 | if not enforcing: |
| 57 | return shown # the records API serves the caller whole rows too |
| 58 | visible = set() |
| 59 | for key in ("before", "after"): |
| 60 | snapshot = entry.get(key) |
| 61 | view = (access.readable(collection, snapshot) if isinstance(snapshot, dict) else None) or {} |
| 62 | shown[key] = view if isinstance(snapshot, dict) else snapshot |
| 63 | visible.update(view) |
| 64 | changed = entry.get("changed_fields") |
| 65 | shown["changed_fields"] = [f for f in changed if f in visible] if isinstance(changed, list) else [] |
| 66 | return shown |
| 67 | |
| 68 | |
| 69 | def POST(request): |
| 70 | text = object_corrections.text |
| 71 | base = _base_dir() |
| 72 | access = object_access.Access(request.get("_identity"), base_dir=base) |
| 73 | if not access.signed_in: |
| 74 | return {"status": 401, "error": "Sign in to read the history."} |
| 75 | staff = object_corrections.staff_refusal(access, "action_counter_history", doing="read the counter history") |
| 76 | if staff: |
| 77 | return staff |
| 78 | collection = text(request.get("collection")) |
| 79 | if collection not in SHOP_COLLECTIONS: |
| 80 | return {"status": 400, "error": ("collection is one of: " + ", ".join(SHOP_COLLECTIONS) + ".")} |
| 81 | record_id = text(request.get("record_id")) |
| 82 | try: |
| 83 | limit = max(1, min(500, int(text(request.get("limit")) or "100"))) |
| 84 | except ValueError: |
| 85 | limit = 100 |
| 86 | try: |
| 87 | page = object_record_changes.list_record_changes( |
| 88 | collection, record_id=record_id or None, base_dir=base, limit=limit) |
| 89 | except Exception as exc: # noqa: BLE001 |
| 90 | return {"status": 404, "error": f"No history for that: {str(exc)[:120]}"} |
| 91 | |
| 92 | current = {} |
| 93 | shown = [] |
| 94 | for entry in page.get("changes") or []: |
| 95 | rid = text(entry.get("record_id")) |
| 96 | if rid not in current: |
| 97 | # access: the row is only used to ask access.can below whether the caller may read it |
| 98 | try: |
| 99 | current[rid] = object_records.get_collection_record(collection, rid, base_dir=base) |
| 100 | except Exception: # noqa: BLE001 -- deleted: judge by its last snapshot |
| 101 | current[rid] = None |
| 102 | row = current[rid] or entry.get("after") or entry.get("before") or {} |
| 103 | if not access.can("read", collection, row): |
| 104 | continue |
| 105 | shown.append(_readable_entry(access, collection, entry)) |
| 106 | return {"status": 200, "ok": True, "collection": collection, "record_id": record_id, |
| 107 | "changes": shown} |