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
kitchen_ticket.py · 7.2 KB · 201 lines
| 1 | """site_kitchen_ticket -- one order, printed for the person making it. |
| 2 | GET /kitchen/{order_id}/ticket. |
| 3 | |
| 4 | NO PRICES. Not "prices in small type", not "prices unless somebody ticks |
| 5 | something": none, ever, for the same reason site_packing_slip carries |
| 6 | none. A ticket is not an invoice. It exists so the cook can make the |
| 7 | right thing, and what it cost is a conversation that already happened |
| 8 | with somebody else -- usually a card reader, sometimes a different person |
| 9 | entirely. A cook who can see the money on every ticket is being handed |
| 10 | information they cannot act on, on the one piece of paper that has to be |
| 11 | readable in three seconds with their hands full. |
| 12 | |
| 13 | **The lines and their notes, and almost nothing else.** The packing slip |
| 14 | already prints `customer_note` -- what the shopper needs the PACKER to |
| 15 | know. This prints `line_note`, per line, which is where a cook actually |
| 16 | reads it: "no onions" under the burger it is true of, not in a paragraph |
| 17 | at the bottom of the order that has to be matched back up by hand. |
| 18 | `customer_note` is printed too, once, at the top, because "I am |
| 19 | allergic" is addressed to whoever is making the food and it is worth |
| 20 | having on the paper in the kitchen rather than only on the one in |
| 21 | dispatch. |
| 22 | |
| 23 | Printable rather than pretty, and BIG: minimal print CSS, nav hidden at |
| 24 | @media print, and type sized for a metre away. Operator-facing, behind |
| 25 | the ordinary registered permission like the packing slip and for the same |
| 26 | reason -- the order id is an internal record id, so a public page here |
| 27 | would enumerate every order in the shop. |
| 28 | |
| 29 | The promised time is on it. A ticket that says what to make and not when |
| 30 | it is due is a ticket that gets done in the order the pile happens to sit |
| 31 | in, which is exactly the failure the queue page exists to prevent. |
| 32 | |
| 33 | An unknown order is a friendly 404 in the same shell, never a traceback: |
| 34 | a mistyped link is somebody who is still standing in a kitchen trying to |
| 35 | make food. |
| 36 | """ |
| 37 | |
| 38 | import html |
| 39 | import os |
| 40 | from datetime import datetime |
| 41 | |
| 42 | import object_records |
| 43 | |
| 44 | ACTOR = "site_kitchen_ticket" |
| 45 | |
| 46 | |
| 47 | def _base_dir(): |
| 48 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 49 | |
| 50 | |
| 51 | def _text(value): |
| 52 | return str(value if value is not None else "").strip() |
| 53 | |
| 54 | |
| 55 | def _esc(value): |
| 56 | return html.escape(_text(value)) |
| 57 | |
| 58 | |
| 59 | def _esc_multiline(value): |
| 60 | return _esc(value).replace("\n", "<br>") |
| 61 | |
| 62 | |
| 63 | def _parse_when(value): |
| 64 | """Same reading as site_kitchen's and system_pickup_attention's: ISO |
| 65 | in, NAIVE LOCAL wall-clock out, None when it will not parse. |
| 66 | Duplicated rather than shared because objects on this platform are |
| 67 | executed, not imported, and ten lines is not worth a module boundary |
| 68 | (docs/logic-decisions.md #4).""" |
| 69 | raw = _text(value) |
| 70 | if not raw: |
| 71 | return None |
| 72 | try: |
| 73 | when = datetime.fromisoformat(raw.replace("Z", "+00:00")) |
| 74 | except ValueError: |
| 75 | return None |
| 76 | if when.tzinfo is not None: |
| 77 | when = when.astimezone().replace(tzinfo=None) |
| 78 | return when |
| 79 | |
| 80 | |
| 81 | _STYLE = """ |
| 82 | .wrap { max-width: 640px; margin: 0 auto; padding: 2rem 1.25rem; } |
| 83 | .pagehead { margin-bottom: 1rem; } |
| 84 | .pagehead h1 { margin: 0 0 0.25rem; font-size: 1.6rem; } |
| 85 | .promised { font-size: 2.4rem; font-weight: 700; line-height: 1.1; margin: 0.2rem 0; } |
| 86 | .who { font-size: 1.3rem; font-weight: 600; } |
| 87 | .hint { color: var(--muted, #999); font-size: 0.9rem; } |
| 88 | .note { border: 1px solid var(--line, #333); border-radius: 8px; padding: 0.8rem 1rem; margin: 1rem 0; white-space: pre-wrap; font-size: 1.1rem; } |
| 89 | ul.klines { list-style: none; margin: 1.2rem 0; padding: 0; font-size: 1.35rem; } |
| 90 | ul.klines li { padding: 0.5rem 0; border-top: 2px solid var(--line, #38384a); } |
| 91 | ul.klines .qty { font-weight: 700; display: inline-block; min-width: 2.6rem; } |
| 92 | ul.klines .line-note { display: block; margin-left: 2.6rem; font-style: italic; font-weight: 600; font-size: 1.15rem; } |
| 93 | .notfound { text-align: center; padding: 3rem 1rem; } |
| 94 | @media print { |
| 95 | nav, header.app, .noprint, .btn { display: none !important; } |
| 96 | .wrap { max-width: none; padding: 0; } |
| 97 | body { background: #fff; color: #000; } |
| 98 | .note { border-color: #999; } |
| 99 | } |
| 100 | """ |
| 101 | |
| 102 | |
| 103 | def _page(body, *, title, status=200): |
| 104 | page = { |
| 105 | "content_type": "text/html; charset=utf-8", |
| 106 | "body": f"""<!doctype html> |
| 107 | <html lang="en"> |
| 108 | <head> |
| 109 | <meta charset="utf-8"> |
| 110 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 111 | <title>{_esc(title)}</title> |
| 112 | <link rel="stylesheet" href="/style"> |
| 113 | <style>{_STYLE}</style> |
| 114 | </head> |
| 115 | <body> |
| 116 | <div class="wrap"> |
| 117 | {body} |
| 118 | </div> |
| 119 | <script src="/nav"></script> |
| 120 | </body> |
| 121 | </html>""", |
| 122 | } |
| 123 | if status != 200: |
| 124 | page["status"] = status |
| 125 | return page |
| 126 | |
| 127 | |
| 128 | def _not_found(): |
| 129 | return _page( |
| 130 | '<div class="notfound"><h1>Not found</h1>' |
| 131 | '<p class="hint">There is no order with that id. It may have been ' |
| 132 | 'mistyped, or the order may have been deleted. ' |
| 133 | '<a href="/kitchen">Back to the kitchen</a>.</p></div>', |
| 134 | title="Kitchen ticket -- not found", status=404) |
| 135 | |
| 136 | |
| 137 | def _lines_html(lines): |
| 138 | if not lines: |
| 139 | return ('<p class="hint">This order has no lines -- there is nothing ' |
| 140 | 'on it to make.</p>') |
| 141 | items = [] |
| 142 | for line in lines: |
| 143 | quantity = _text(line.get("quantity")) or "1" |
| 144 | description = (_text(line.get("description")) |
| 145 | or _text(line.get("product_id")) or "Item") |
| 146 | note = _text(line.get("line_note")) |
| 147 | note_html = (f'<span class="line-note">{_esc(note)}</span>' |
| 148 | if note else "") |
| 149 | items.append(f'<li><span class="qty">{_esc(quantity)}×</span>' |
| 150 | f'{_esc(description)}{note_html}</li>') |
| 151 | return f'<ul class="klines">{"".join(items)}</ul>' |
| 152 | |
| 153 | |
| 154 | def GET(request): |
| 155 | base = _base_dir() |
| 156 | order_id = _text(request.get("order_id")) |
| 157 | if not order_id: |
| 158 | return _not_found() |
| 159 | |
| 160 | try: |
| 161 | order = object_records.get_collection_record("orders", order_id, |
| 162 | base_dir=base) |
| 163 | except Exception: |
| 164 | return _not_found() |
| 165 | if not order: |
| 166 | return _not_found() |
| 167 | |
| 168 | try: |
| 169 | lines = [row for row in object_records.read_collection_records( |
| 170 | "order_lines", base_dir=base) |
| 171 | if _text(row.get("order_id")) == order_id] |
| 172 | except Exception: |
| 173 | lines = [] |
| 174 | lines.sort(key=lambda row: _text(row.get("description"))) |
| 175 | |
| 176 | number = _text(order.get("number")) or order_id |
| 177 | who = _text(order.get("customer_name")) or "No name on this order" |
| 178 | when = _parse_when(order.get("promised_at")) |
| 179 | promised = (f'<div class="promised">{when.strftime("%H:%M")}</div>' |
| 180 | if when is not None |
| 181 | else '<p class="hint">No promised time on this order.</p>') |
| 182 | |
| 183 | note = _text(order.get("customer_note")) |
| 184 | note_html = ('<div class="note"><strong>For the kitchen</strong><br>' |
| 185 | f'{_esc_multiline(note)}</div>' if note else "") |
| 186 | |
| 187 | body = f""" |
| 188 | <div class="pagehead"> |
| 189 | <h1>Kitchen ticket</h1> |
| 190 | <p class="hint">Order {_esc(number)}</p> |
| 191 | {promised} |
| 192 | <div class="who">{_esc(who)}</div> |
| 193 | </div> |
| 194 | {note_html} |
| 195 | {_lines_html(lines)} |
| 196 | <p class="hint">This is a kitchen ticket, not an invoice: it says what to |
| 197 | make and deliberately carries no prices -- the money conversation happened |
| 198 | somewhere else, with somebody who may not be the person cooking.</p> |
| 199 | <p class="hint noprint"><a href="/kitchen">Kitchen queue</a></p> |
| 200 | """ |
| 201 | return _page(body, title=f"Kitchen ticket -- order {number}") |