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

0 downloads

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.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
pickup_slots.py · 4.1 KB · 95 lines
1"""Pre-write hook for pickup_slots: a window cannot hold minus one order,
2and it cannot already hold more than it can.
3
4Two gates, both of them things the schema has no way to say. `capacity`
5and `orders_taken` are ordinary integers as far as validation is
6concerned, and integers are perfectly happy to be -3 or to be four in a
7window that holds two.
8
9**Capacity may not be negative.** Zero is legal and means something
10useful -- "this window exists and is full", which is a different sentence
11from is_open=false's "we are not open then". Minus one is not a smaller
12version of that; it is a number no surface can act on, and the picker
13that reads it would quietly decide the window has places left.
14
15**orders_taken may not exceed capacity, and the refusal carries both
16numbers.** A gate that only says "no" leaves whoever is fixing the slot
17guessing which of the two figures is wrong -- exactly the objection
18hook_shipment_lines' three-number refusal answers on the shipping side.
19
20**The race this does NOT stop, and must not be read as stopping.** Two
21shoppers can pass action_checkout's capacity gate for the last place in
22one window; the second increment then pushes orders_taken past capacity
23for real. Checkout writes through object_records directly, which is a
24trusted server-side path and bypasses hooks by design
25(docs/validation-and-logic.md), so that write lands. THAT IS DELIBERATE
26AND IT IS THE POINT: the oversell is a fact about what happened, and the
27shop needs to see it on the row it happened to so somebody can ring the
28customer. What this hook refuses is a NUMBER SOMEBODY TYPED -- an
29operator editing a slot in a form, or an API client posting one -- and a
30typo is not a fact. The same split hook_shipment_lines and
31action_create_shipment already carry, named in both files so neither is
32removed on the assumption the other covers it.
33"""
34
35
36def _text(value):
37 return str(value if value is not None else "").strip()
38
39
40def _int(value, default=None):
41 """The integer a field holds, or `default` when it holds nothing and
42 None when it holds something that is not one.
43
44 None means "schema validation owns this error": a hook that invented
45 its own message for a non-integer would produce two different
46 refusals for one mistake depending on which gate saw it first.
47 """
48 text = _text(value)
49 if not text:
50 return default
51 try:
52 return int(text)
53 except ValueError:
54 return None
55
56
57def BEFORE_WRITE(request):
58 action = _text(request.get("action"))
59 if action not in ("create", "update"):
60 return None
61 record = request.get("record") or {}
62
63 capacity = _int(record.get("capacity"), 0)
64 if capacity is None:
65 return None
66 if capacity < 0:
67 return {"error": (f"A slot's capacity cannot be negative (got "
68 f"{capacity}). Zero is the way to say this window "
69 f"is full, and is_open=false is the way to say the "
70 f"shop is shut then -- a negative capacity is "
71 f"neither, and every surface that reads it would "
72 f"have to guess which one was meant."),
73 "status": 400}
74
75 taken = _int(record.get("orders_taken"), 0)
76 if taken is None:
77 return None
78 if taken < 0:
79 return {"error": (f"A slot cannot have taken a negative number of "
80 f"orders (got {taken}). Nothing booked is what zero "
81 f"means; a correction is a new number, never a "
82 f"negative one."),
83 "status": 400}
84
85 if taken > capacity:
86 starts_at = _text(record.get("starts_at"))
87 when = f" starting {starts_at}" if starts_at else ""
88 return {"error": (f"That slot{when} would hold more orders than it "
89 f"can: capacity {capacity}, orders taken {taken}. "
90 f"Raise the capacity if the shop really can serve "
91 f"{taken} in that window -- do not raise the count, "
92 f"which is a record of orders that exist."),
93 "status": 409}
94
95 return None