House-move inventory: Sheets, labels and French - Part 8
~9 min read
The inventory only does its job if it leaves the screen. A sheet goes inside each box. A label goes on the outside. Customs gets a document, and since the move is to France, the document is in French. This post covers the rendering layer and the project's only outbound network call.
Pure formatting over domain objects
render/ holds three modules:
sheets.py- the plain-text contents sheet.pdf.py- PDFs withreportlab: a box sheet, a room sheet, and/labels.pdf, A4 pages of 2×2 A6 shipping labels.i18n.py- the translations.
The rule that governs them: rendering is pure formatting over domain objects and never touches storage. pdf.py receives a BoxContents or RoomSheet and returns bytes. When a sheet needs an invoice, the service reads the file and hands pdf.py an InvoiceDoc carrying the bytes. This is the same layering as post 2, applied to output, and it makes the renderers trivially testable: build a sheet in memory, render it, and inspect the result.
How do you test a PDF? With pypdf. The tests render a real file and extract its text to assert on - not "did render return bytes" but "does page 3 contain KIT-02 and not KIT-01". Several features in the roadmap carry a note like "verified end to end, not just at the unit level" after that kind of check caught what a unit test of an intermediate function would have missed: a caption that exists in the data but lands on the wrong page.
What goes on a sheet
A box sheet is the itemised contents: each line's name, quantity and a string of extras. Those extras come from one shared function, item_extras, used by the text sheet and every PDF variant - [FRAGILE], [FLAT-PACK], the value, (bought 2024-03-02), an invoice count, and dimensions as (W × H × D cm) using only whichever of the three are set.
Phase 3.31 existed because dimensions and flat-pack had been stored and shown in the UI for a release but "never made it onto anything printed". The fix was two more tokens in that one shared function, and it appeared on the text sheet, the box PDF and the room PDF at once. One function that every format shares is how features arrive everywhere or nowhere, never in two places out of three.
A room sheet is a summary page - the box list with room-wide market and retail totals - followed by every box's contents. show_going adds each box's going-or-not decision to its line on the summary.
Labels
/labels.pdf prints four A6 labels per A4 page: the big box code, room and name, item and line counts, a FRAGILE mark, and an Updated … · Printed … footer. You can ask for one box, an explicit set (?codes=KIT-01&codes=KIT-03), or filter by status, decision, room or search text. Labels are always code-ordered so the printed stack matches the order you'll pick the boxes up in. From a room page you can tick a few boxes and print only those labels.
French, in two layers
There are two different things to translate, and conflating them was the design mistake to avoid.
The chrome - headings, field labels, "Market value", box status words - is fixed text in render/i18n.py, with en and fr tables. Every print endpoint takes ?lang=en|fr. That's just a lookup.
The data - the names and notes you typed - is different. Items and boxes have optional stored name_fr and notes_fr fields, and pick() prefers them when printing in French and falls back to the original:
def pick(base, translated, lang):
if lang != DEFAULT_LANGUAGE and translated:
return translated
return base
The key point is that data translation is opt-in and explicit. Nothing is translated implicitly at print time, so what's printed is exactly what's stored and reviewed. Missing translations are made visible instead: a warning on the box page, an FR column in lists, and a top-level view of everything still untranslated (0 means ready for the French sheet).
The one outbound call
Typing French labels for several hundred items by hand is tedious, so there's a suggest button backed by translate.py and the Anthropic API. The design constraints are what make it acceptable:
- Optional. Without an
ANTHROPIC_API_KEY, the translate endpoints return503and the rest of the app is unaffected. It's the project's only outbound network call, and the code says so at the top of the module. - Drafts only.
POST /items/{id}/translateand/boxes/{code}/translatereturn suggestions. They are never persisted. You review one and PATCH it back intoname_fr/notes_frlike any other edit. - Terse, and checked. The system prompt frames it as short inventory labels for a customs declaration, to leave brand names, model numbers, sizes and units alone, and to answer with only a JSON array the same length and order as the input. The adapter parses that array and raises a domain error if the response isn't a list of strings.
- A Protocol again.
Translatoris injected intoInventoryService, so tests use a fake and never hit the network.
A machine suggestion that a person approves, on a document that goes to customs, is the right amount of automation. The model writes the first draft; a person signs it off.
Next: what happens when a PDF takes so long to build that the web server gives up on it.
Previous: ← Invoices, photos and HEIC · Next: Slow PDFs as background jobs →
