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

9 downloads

Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 49 minutes ago History
loss_events.py · 3.6 KB · 80 lines
1"""hook_loss_events -- who reported a loss, who approved it, and that they differ.
2
3Two layers, because the two questions are different:
4
5BEFORE_WRITE (the record API, MCP): the only place that knows who is
6writing. It stamps `reported_by` from the signed-in user when a loss is
7recorded, and `approved_by` / `approved_at` when it is approved, so neither
8is ever typed; it refuses an edit to either stamp; and it refuses the
9reporter's own approval. Same shape as hook_expenses: approving your own
10loss is not an approval -- the person who reports a theft is the person a
11second pair of eyes exists for.
12
13LOCKED_CHECK (loss_events.approval, every writer, inside the write lock):
14an approved row names an approver and a reporter and they are not the same
15person. The hook cannot hold this for a storage-level writer (an import, a
16script, action_apply_count's own loss events) and a check that only the
17HTTP path ran would be a rule with a back door. The check never asks who
18is writing -- it states a fact about the row.
19"""
20
21from datetime import datetime, timezone
22
23
24def _text(value):
25 return str(value if value is not None else "").strip()
26
27
28def BEFORE_WRITE(request):
29 action = request.get("action")
30 if action not in ("create", "update"):
31 return None # deleting an approved loss is the schema's locked_when (refused)
32 record = dict(request.get("record") or {})
33 existing = request.get("existing") or {}
34 actor = _text((request.get("subject") or {}).get("user_id"))
35
36 if action == "create":
37 if not actor:
38 return {"error": "A loss is reported by a signed-in person.", "status": 403}
39 if _text(record.get("status")) not in ("", "reported"):
40 return {"error": ("A loss is reported first, then approved by somebody other "
41 "than the person who reported it."), "status": 400}
42 record["reported_by"] = actor
43 record["approved_by"] = ""
44 record["approved_at"] = ""
45 return {"record": record}
46
47 for stamp in ("reported_by", "approved_by", "approved_at"):
48 if _text(record.get(stamp)) != _text(existing.get(stamp)) and not (
49 stamp != "reported_by" and _text(record.get("status")) == "approved"
50 and _text(existing.get("status")) != "approved"):
51 return {"error": f"{stamp} is stamped by the server, never typed.", "status": 400}
52
53 if _text(record.get("status")) == "approved" and _text(existing.get("status")) != "approved":
54 reporter = _text(existing.get("reported_by"))
55 if not actor:
56 return {"error": "A loss is approved by a signed-in person.", "status": 403}
57 if actor == reporter:
58 return {"error": ("Losses are approved by somebody other than the person who "
59 "reported them. Approving your own report is not an approval."),
60 "status": 403}
61 record["approved_by"] = actor
62 record["approved_at"] = datetime.now(timezone.utc).isoformat()
63 return {"record": record}
64 return None
65
66
67def LOCKED_CHECK(request):
68 if request.get("action") not in ("create", "update"):
69 return None
70 record = request.get("record") or {}
71 if _text(record.get("status")) != "approved":
72 return None
73 reporter, approver = _text(record.get("reported_by")), _text(record.get("approved_by"))
74 if not reporter or not approver:
75 return {"error": "An approved loss names who reported it and who approved it.",
76 "status": 409}
77 if reporter == approver:
78 return {"error": "A loss is approved by somebody other than the person who reported it.",
79 "status": 409}
80 return None