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
confirm_count.py · 7.0 KB · 150 lines
1"""action_confirm_count -- the named witness confirms a count, from their own sign-in.
2
3GET the counts awaiting the caller's confirmation
4POST {count_id, counted_minor} confirm (same figure) or dispute (another one)
5
6action_record_count lets the person counting a till name a second person
7as witness. Naming somebody is a claim, not an attestation: a closer who
8pockets cash and types a colleague's name would otherwise produce a
9"witnessed" shortage that implicates the colleague. So the count stays
10`witness_status: named` -- rated as self-certified, and an account that
11requires a second attestor reports that requirement unmet -- until the
12person named answers here:
13
14- Only the named witness, signed in as themselves, can answer. Not the
15 counter, not a third user, not an admin acting for them: an approval
16 somebody else can give on your behalf is not yours. The admin token is
17 nobody's sign-in and is refused.
18- They state the figure they counted. It must equal the recorded one to
19 confirm. A different figure is recorded as a refusal to confirm
20 (`disputed`, with the figure they gave in witness_stated_minor), and
21 the answer does not tell them the recorded figure.
22- They answer once. A confirmed or disputed count is not answered again;
23 a disagreement is settled by counting again, which is a new record.
24
25The GET listing is how the witness finds what is waiting for them: their
26own pending counts, with the account, date and who counted -- but not the
27figure, which is theirs to state, not to copy.
28
29Confirming changes only the count row. The variance journal was booked
30when the count was recorded and is not touched.
31"""
32
33import os
34from datetime import datetime, timezone
35
36import object_money
37import object_records
38
39ACTOR = "action_confirm_count"
40
41NOT_YOURS = "No count is waiting for your confirmation under that id."
42
43
44def _base_dir():
45 return os.environ.get("DBBASIC_DATA_DIR", "data")
46
47
48def _signed_in_self(request):
49 """(user_id, None) for a person signed in as themselves, else (None, refusal)."""
50 identity = request.get("_identity") or {}
51 user_id = str(identity.get("user_id") or "").strip()
52 if not user_id:
53 return None, {"status": 403,
54 "error": "Sign in as yourself to confirm a count you witnessed."}
55 if identity.get("auth_method") == "admin_token":
56 return None, {"status": 403,
57 "error": ("A count is confirmed by the named witness from their "
58 "own sign-in. The admin token is nobody's sign-in.")}
59 return user_id, None
60
61
62def GET(request):
63 user_id, refused = _signed_in_self(request)
64 if refused:
65 return refused
66 base = _base_dir()
67 # access: only rows naming the caller as their pending witness are kept, keyed by identity
68 pending = [r for r in object_records.read_collection_records("value_account_counts",
69 base_dir=base)
70 if r.get("witnessed_by") == user_id and r.get("witness_status") == "named"]
71 names = {}
72 if pending:
73 wanted = {r.get("value_account_id") for r in pending}
74 # access: the names of only the accounts the caller was named to witness a count of
75 for account in object_records.read_collection_records("value_accounts", base_dir=base):
76 if account.get("id") in wanted:
77 names[account["id"]] = account.get("name") or account["id"]
78 return {
79 "status": 200,
80 "counts": [{"count_id": r.get("id"),
81 "value_account_id": r.get("value_account_id"),
82 "account_name": names.get(r.get("value_account_id"), ""),
83 "counted_on": r.get("counted_on"),
84 "counted_by": r.get("counted_by"),
85 "recorded_at": r.get("created_at", "")}
86 for r in pending],
87 "how": ("POST count_id and counted_minor, the figure you counted, to confirm. "
88 "A different figure is recorded as your refusal to confirm."),
89 }
90
91
92def POST(request):
93 user_id, refused = _signed_in_self(request)
94 if refused:
95 return refused
96 count_id = str(request.get("count_id") or "").strip()
97 if not count_id:
98 return {"status": 400, "error": "count_id is required."}
99 try:
100 stated = int(request.get("counted_minor"))
101 except (TypeError, ValueError):
102 return {"status": 400,
103 "error": ("State the figure you counted as counted_minor, a whole "
104 "number of the denomination's smallest unit.")}
105
106 base = _base_dir()
107 try:
108 # access: answered only to the user the row names as its witness, checked below
109 row = object_records.get_collection_record("value_account_counts", count_id,
110 base_dir=base)
111 except (LookupError, ValueError):
112 row = None
113 if row is not None and row.get("counted_by") == user_id:
114 return {"status": 403,
115 "error": ("You recorded this count. It is confirmed by the witness you "
116 "named, from their own sign-in, not by the person counting.")}
117 if row is None or not row.get("witnessed_by") or row.get("witnessed_by") != user_id:
118 return {"status": 404, "error": NOT_YOURS}
119 status = row.get("witness_status") or ""
120 if status != "named":
121 return {"status": 409,
122 "error": f"You have already answered for this count ({status or 'not named'})."}
123
124 try:
125 recorded = int(row.get("counted_minor"))
126 except (TypeError, ValueError):
127 recorded = None
128 agrees = recorded is not None and stated == recorded
129 answer = "confirmed" if agrees else "disputed"
130 try:
131 # access: system write, the caller is the row's named witness (checked above)
132 object_records.update_collection_record(
133 "value_account_counts", count_id,
134 {"witness_status": answer, "witness_stated_minor": str(stated),
135 "witness_answered_at": datetime.now(timezone.utc).isoformat()},
136 base_dir=base, actor=f"{ACTOR}:{user_id}", preserve_read_only=True,
137 expected_rev=object_records.compute_record_rev(row))
138 except object_records.VersionConflictError:
139 return {"status": 409,
140 "error": "This count changed while you were answering. Look again and retry."}
141
142 if not agrees:
143 return {"status": 409, "count_id": count_id, "witness_status": "disputed",
144 "assurance": object_money.assurance_for("physical_count"),
145 "error": ("The figure you stated does not match the recorded count, so "
146 "you have NOT confirmed it. Your figure is recorded on the count "
147 "as a refusal to confirm; the account owner sees it. Count again "
148 "together to settle it.")}
149 return {"status": 200, "count_id": count_id, "witness_status": "confirmed",
150 "assurance": object_money.assurance_for("physical_count", witnessed=True)}