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