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
packing_slip.py · 8.5 KB · 205 lines
1"""site_packing_slip -- what is in this box, printed. GET
2/shipments/{shipment_id}/slip.
3
4NO PRICES. Not "prices hidden behind a gift flag", not "prices unless the
5buyer ticked something": none, ever. A packing slip is not an invoice --
6it exists so the person opening the parcel can check the contents against
7what they ordered, and the money conversation already happened somewhere
8else with somebody who may not be the person holding the box. Making the
9slip priceless by construction is what makes EVERY shipment gift-safe
10without a flag to remember, and a flag somebody forgets to tick is
11precisely how a birthday present arrives with the amount paid stapled to
12it. The invoice has its own door (the payment portal) for anyone who
13wants the numbers.
14
15**Since the document layer landed, that rule is a PARAMETER, not a
16paragraph.** `object_documents.KINDS["packing_slip"]["show_money"]` is
17False, there is no per-call override offered, and build_model does not
18hide the money -- it never puts it in the model. This file cannot print a
19price even by mistake, because it never holds one: it passes `money=None`
20to the fold, and the fold never asks for a formatter it was told not to
21need. The old version achieved the same outcome by simply not writing the
22columns, which worked exactly as long as everybody who edited the file had
23read the docstring first.
24
25Everything else this page draws -- header, identity, ship-to, the lines
26table, page breaks, the repeated table heading, @page size -- now comes
27from the shared renderer instead of from a private `@media print` block
28that agreed with the other six printables about nothing. The one rule
29those six blocks all missed was `thead { display: table-header-group }`,
30which is why a shipment with fifty lines used to print its column
31headings once.
32
33Printable rather than pretty, and the nav stays: this is an operator page
34(the shipment id is an internal record id, so it sits behind the ordinary
35registered permission rather than a capability token like the invoice
36portal's), and an operator must not arrive somewhere that looks like a
37different product.
38
39customer_note and gift_message are printed WHEN THEY EXIST, read with
40.get. Checkout now collects both and stamps them on the order the moment
41the orders schema declares the columns (action_checkout's `_has_field`
42gate); until it does, the shop carries them on the cart and says so, and
43older orders simply have neither. So .get stays: a slip that crashed on
44an order predating the fields would be a page breaking itself over a
45column somebody added last week. The packer is the one who needs both --
46special instructions are useless to anyone else, and a gift message
47unprinted is a gift message that never happened.
48
49The gift message needs no gift flag beside it. This page shows no prices
50at all, by construction, so every parcel is already gift-safe: the
51message is about warmth, not secrecy.
52
53An unknown shipment is a friendly 404 in the same shell, never a
54traceback: a mistyped link is somebody who is still at their desk trying
55to send a parcel.
56"""
57
58import html
59import os
60
61import object_access
62import object_documents
63import object_records
64
65ACTOR = "site_packing_slip"
66KIND = "packing_slip"
67
68
69def _base_dir():
70 return os.environ.get("DBBASIC_DATA_DIR", "data")
71
72
73def _text(value):
74 return str(value if value is not None else "").strip()
75
76
77def _esc(value):
78 return html.escape(_text(value))
79
80
81def _settings(base):
82 """Every app_settings row as one mapping -- what object_documents wants,
83 and one scan instead of four. Duplicated on purpose like every other
84 package that reads app_settings (docs/logic-decisions.md #4)."""
85 values = {}
86 try:
87 # access: box configuration
88 for row in object_records.read_collection_records("app_settings",
89 base_dir=base):
90 key, value = _text(row.get("key")), _text(row.get("value"))
91 if key and value:
92 values[key] = value
93 except Exception:
94 pass
95 return values
96
97
98def _not_found():
99 """A friendly 404 in the same shell -- an empty document with the message
100 where the document would have been, rather than a traceback at somebody
101 who is still trying to send a parcel."""
102 return object_documents.render_page(
103 object_documents.build_model(KIND, {}, {}, require_business=False),
104 title="Packing slip -- not found",
105 before='<h1>Not found</h1>'
106 '<p class="doc-hint">There is no shipment with that id. It may '
107 'have been mistyped, or the shipment may have been deleted. '
108 '<a href="/pick-list">Back to the pick list</a>.</p>',
109 setup_nudge=False,
110 status=404)
111
112
113def _notes_for(order):
114 """Special instructions and a gift message, when the order has such
115 fields at all -- .get, never [], see the module docstring."""
116 notes = []
117 note = _text(order.get("customer_note"))
118 if note:
119 notes.append({"title": "Special instructions", "body": note})
120 gift = _text(order.get("gift_message"))
121 if gift:
122 notes.append({"title": "Gift message", "body": gift})
123 return notes
124
125
126def GET(request):
127 base = _base_dir()
128 shipment_id = _text(request.get("shipment_id"))
129 if not shipment_id:
130 return _not_found()
131
132 # The page reads storage directly, so it is the only gate in front of a
133 # shipment's recipient, address and its order's notes: it asks the
134 # permission engine what the shipments and orders APIs would answer. A
135 # refused shipment reads exactly like a missing one.
136 access = object_access.Access(request.get("_identity"), base_dir=base)
137 try:
138 shipment = access.get("shipments", shipment_id)
139 except Exception:
140 return _not_found()
141 if not shipment:
142 return _not_found()
143
144 try:
145 order = access.get("orders", _text(shipment.get("order_id")))
146 except Exception:
147 order = {}
148 order = order or {}
149
150 try:
151 # access: lines of the shipment read through Access above
152 lines = [row for row in object_records.read_collection_records(
153 "shipment_lines", base_dir=base)
154 if _text(row.get("shipment_id")) == shipment_id]
155 except Exception:
156 lines = []
157 lines.sort(key=lambda row: _text(row.get("description")))
158
159 number = _text(order.get("number")) or _text(shipment.get("order_id"))
160 when = (_text(shipment.get("shipped_on"))
161 or _text(shipment.get("created_at"))[:10]
162 or _text(order.get("order_date")))
163
164 carrier = _text(shipment.get("carrier"))
165 tracking = _text(shipment.get("tracking_number"))
166 carriage = " &middot; ".join(
167 part for part in (_esc(carrier), _esc(tracking)) if part)
168
169 # money=None is not an omission: on a no-prices kind the fold never calls
170 # a formatter, so there is nothing here that could produce a price.
171 facts = object_documents.facts_from_records(
172 KIND, shipment, lines, money=None,
173 extra={
174 "number": number,
175 "date": when,
176 "reference": f"shipment {shipment_id}",
177 "to": {"name": (_text(shipment.get("ship_to_name"))
178 or _text(order.get("customer_name"))
179 or "No name on this order"),
180 "address": _text(shipment.get("ship_to_address"))},
181 "notes": _notes_for(order),
182 "footer": ("This is a packing slip, not an invoice: it says what "
183 "is in the box and deliberately carries no prices, so "
184 "any parcel can be sent as a gift."
185 + ((" &middot; " + carriage) if carriage else "")),
186 })
187
188 settings = _settings(base)
189 # require_business=False: an operator is standing at a printer and a parcel
190 # is waiting. A nameless business is refused where refusing costs nothing
191 # -- at send time, in action_send_document -- and nudged (no-print) here.
192 model = object_documents.build_model(KIND, facts, settings,
193 require_business=False)
194
195 empty = ('<p class="doc-hint">This shipment has no lines yet -- nothing '
196 'has been picked into it.</p>' if not model["lines"] else "")
197 pdf = object_documents.pdf_engine_status(
198 settings.get(object_documents.PDF_ENGINE_SETTING))
199
200 return object_documents.render_page(
201 model,
202 title=f"Packing slip -- order {number}",
203 after=empty + '<p class="doc-hint noprint"><a href="/pick-list">Pick list</a></p>',
204 pdf=pdf["available"],
205 size=settings.get(object_documents.PAGE_SIZE_SETTING))