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
pickup_slots.py · 3.1 KB · 74 lines
1"""Pre-write hook for pickup_slots: a window cannot already hold more than
2it can.
3
4One gate, the one the schema has no way to say: a comparison between two
5fields. `capacity` and `orders_taken` are integers, and the schema declares
6each `validation.min` 0 -- so neither can be negative, for every writer,
7which is what this hook used to check by hand. (Zero capacity is legal and
8means something useful -- "this window exists and is full", a different
9sentence from is_open=false's "we are not open then".)
10
11**orders_taken may not exceed capacity, and the refusal carries both
12numbers.** A gate that only says "no" leaves whoever is fixing the slot
13guessing which of the two figures is wrong -- exactly the objection
14hook_shipment_lines' three-number refusal answers on the shipping side.
15
16**The race this does NOT stop, and must not be read as stopping.** Two
17shoppers can pass action_checkout's capacity gate for the last place in
18one window; the second increment then pushes orders_taken past capacity
19for real. Checkout writes through object_records directly, which is a
20trusted server-side path and bypasses hooks by design
21(docs/validation-and-logic.md), so that write lands. THAT IS DELIBERATE
22AND IT IS THE POINT: the oversell is a fact about what happened, and the
23shop needs to see it on the row it happened to so somebody can ring the
24customer. What this hook refuses is a NUMBER SOMEBODY TYPED -- an
25operator editing a slot in a form, or an API client posting one -- and a
26typo is not a fact. The same split hook_shipment_lines and
27action_create_shipment already carry, named in both files so neither is
28removed on the assumption the other covers it.
29"""
30
31
32def _text(value):
33 return str(value if value is not None else "").strip()
34
35
36def _int(value, default=None):
37 """The integer a field holds, or `default` when it holds nothing and
38 None when it holds something that is not one.
39
40 None means "schema validation owns this error": a hook that invented
41 its own message for a non-integer would produce two different
42 refusals for one mistake depending on which gate saw it first.
43 """
44 text = _text(value)
45 if not text:
46 return default
47 try:
48 return int(text)
49 except ValueError:
50 return None
51
52
53def BEFORE_WRITE(request):
54 action = _text(request.get("action"))
55 if action not in ("create", "update"):
56 return None
57 record = request.get("record") or {}
58
59 capacity = _int(record.get("capacity"), 0)
60 taken = _int(record.get("orders_taken"), 0)
61 if capacity is None or taken is None:
62 return None # schema validation owns the type error
63
64 if taken > capacity:
65 starts_at = _text(record.get("starts_at"))
66 when = f" starting {starts_at}" if starts_at else ""
67 return {"error": (f"That slot{when} would hold more orders than it "
68 f"can: capacity {capacity}, orders taken {taken}. "
69 f"Raise the capacity if the shop really can serve "
70 f"{taken} in that window -- do not raise the count, "
71 f"which is a record of orders that exist."),
72 "status": 409}
73
74 return None