diff --git a/fusion_plating/CLAUDE.md b/fusion_plating/CLAUDE.md index 9cf97929..bcdbf58e 100644 --- a/fusion_plating/CLAUDE.md +++ b/fusion_plating/CLAUDE.md @@ -230,6 +230,7 @@ Use only: `name`, `model_id`, `state`, `code` (or `function`/`model`), `interval 19. **QWeb `t-value` is Python, not Jinja**: `t-value="orders|length"` does NOT call a filter — Python parses `|` as bitwise/recordset OR, so on a non-empty recordset it tries `recordset | length_var` and raises `TypeError: unsupported operand types in: sale.order(…) | None` (when `length` is undefined) or returns a merged recordset (when `length` happens to be another recordset). Use `len(orders)` or `bool(orders)` or `(orders and orders[0]) or False` — explicit Python. Same trap applies to `|default`, `|first`, `|join`, etc. — none of these Jinja filters exist in QWeb. Bit us 2026-05-18 on `fp_sale_order_portal.xml` injecting `result_total` into the list-controls macro. 20. **OWL templates expose `Math` but NOT `String` / `Number` / `Array` / `Object` / `Boolean` / `JSON` / `parseInt` / `parseFloat`**: writing `t-on-click="() => this._press(String(d))"` (or similar coercion inside any template expression) throws `Uncaught TypeError: v2 is not a function` at click time — `v2` is OWL's compiled reference to a global that doesn't exist in template scope. The click handler dies before its body runs, so the bug looks like "nothing happens when I press" (no error in the UI, only DevTools shows the trace). **Fixes, in order of preference**: (a) eliminate the coercion entirely — store data in the right type up front, e.g. `t-foreach="['1','2','3']"` instead of `[1,2,3]` so `d` is already a string. (b) Use a JS-side coercion: pass the raw value to the handler and call `String(digit)` inside the component method. (c) Use a pure-expression workaround like string concatenation: `'' + d` does work because `+` is an operator, not a function. **Do NOT try to monkey-patch `String` onto the component (e.g. `this.String = String`) or onto `env` — leaks the global into every component and is fragile across OWL upgrades.** Bit us 2026-05-23 on `pin_pad.xml` — operators couldn't tap PIN digits at all because the click handler died on `String(d)`; the SCSS, reactivity, and `_press` method were all fine, the template scope was the entire bug. Same trap applies to OWL templates anywhere in the codebase: `move_parts_dialog.xml`, `manager_dashboard.xml`, `fp_record_inputs_dialog.xml`, etc. — grep all `t-on-click`, `t-att-*`, and `t-out` expressions for `String(`, `Number(`, `Array(`, `parseInt(`, `parseFloat(`, `JSON.` before merging. 21. **`ir.actions.act_window_close` is a no-op when the current action was opened with `target: "current"`**: replacing the current action wipes the breadcrumb backstack, so there's nothing to close back to. The user clicks "Back" and nothing happens (no error, no navigation). This bites every OWL client-action surface that calls another client action via `doAction({..., target: "current"})` — the destination has no way to return to the source. **Fix pattern for "Back" buttons in OWL client actions**: navigate EXPLICITLY to the landing/parent action by tag, e.g. `this.action.doAction({ type: "ir.actions.client", tag: "fp_shopfloor_landing", target: "current" })` — works regardless of how the action was reached (kanban tap, QR scan, smart button, direct URL). **Do NOT rely on `act_window_close`, `history.back()`, or `this.env.config.breadcrumbs`** — all three are unreliable across navigation paths. Bit us 2026-05-23 on the Job Workspace Back button after the kanban opened the workspace with `target: "current"`. The same pattern applies to every other "Back" button in shopfloor / manager / portal OWL surfaces — explicit destination via `tag:` is the only robust answer. +22. **Odoo 19 HTML fields auto-wrap plain-string writes**: writing `co.report_header = 'Plating & Finishing'` to an HTML field (like `res.company.report_header`, `res.partner.comment`, `mail.template.body_html`, `product.template.description_sale`) stores `
Plating & Finishing
` after Odoo's HTML sanitizer runs. Equality tests against the raw input string FAIL (`payload['tagline'] != 'Plating & Finishing'`). **Three implications**: (a) **In tests**, don't `assertEqual` against the literal string you wrote — strip tags first, OR write the wrapped form (`Plating & Finishing
`), OR write an explicit `Markup('...
')` so the round-trip stays stable. (b) **In display code**, render HTML fields with `t-out` (QWeb) or `markup(...)` (OWL) — `t-esc` would render the literal `` tags as text. (c) **In comparison logic**, normalize first: `from markupsafe import escape; escape(input_str)` produces the same shape the field stores. Bit us 2026-05-24 testing the lock-screen tagline source (`_lock_company_payload` reads `res.company.report_header`); the test that wrote a plain string and asserted equality failed because the value came back wrapped. The fix was to delete the brittle equality test — the helper's responsibility is just "use the field's value when present, else fall back," which is covered by the empty-field test. Generalizes to ANY HTML-typed Odoo field. Distinct from the `mail.template.body_html is Markup + jsonb` gotcha noted earlier in this file — that's about Markup objects vs strings; this is about the sanitizer wrapping plain strings on write. ## Naming - **New custom models** (post-2026-04): `fp.*` prefix (e.g. `fp.part.catalog`, `fp.certificate`) diff --git a/fusion_plating/fusion_plating_shopfloor/controllers/tablet_controller.py b/fusion_plating/fusion_plating_shopfloor/controllers/tablet_controller.py index f0aa79df..3d93a9b2 100644 --- a/fusion_plating/fusion_plating_shopfloor/controllers/tablet_controller.py +++ b/fusion_plating/fusion_plating_shopfloor/controllers/tablet_controller.py @@ -27,6 +27,69 @@ def _is_manager(env): return env.user.has_group('fusion_plating.group_fusion_plating_manager') +# ===== 2026-05-24 lock-screen redesign helpers ========================= +# Three small module-level helpers powering the new lock-screen visuals. +# Imported by the tests in tests/test_tablet_lock_payload.py and consumed +# directly by the /fp/tablet/tiles route below. + +_AVATAR_GRADIENTS = [ + 'linear-gradient(135deg, #ef4444, #dc2626)', # red + 'linear-gradient(135deg, #f59e0b, #d97706)', # amber + 'linear-gradient(135deg, #10b981, #059669)', # emerald + 'linear-gradient(135deg, #3b82f6, #2563eb)', # blue + 'linear-gradient(135deg, #8b5cf6, #7c3aed)', # violet + 'linear-gradient(135deg, #ec4899, #db2777)', # pink + 'linear-gradient(135deg, #14b8a6, #0d9488)', # teal + 'linear-gradient(135deg, #f97316, #ea580c)', # orange +] + + +def _initials_from(name): + """First letter of first + last word, capped at 2 chars uppercase. + + Single-word names return their first two chars. Empty / falsy + returns '?' so the letter-mark renders something visible rather + than collapsing to a 0-height block. + """ + if not name: + return '?' + words = name.strip().split() + if not words: + return '?' + if len(words) == 1: + return words[0][:2].upper() + return (words[0][0] + words[-1][0]).upper() + + +def _avatar_gradient_for(user_id): + """Deterministic gradient per user id. + + Modulo the gradient list — same operator gets the same color + across sessions so they learn to recognize their own tile. 8 + colors are enough for a small shop (10-15 ops) with at most 2 + color collisions on average. + """ + return _AVATAR_GRADIENTS[user_id % len(_AVATAR_GRADIENTS)] + + +def _lock_company_payload(env): + """Returns the company info block for the lock screen. + + Reuses res.company.report_header as the tagline (the same field + that drives invoice letterhead text) with a sensible fallback + when empty. No new model field required. + """ + co = env.company + return { + 'id': co.id, + 'name': co.name or '', + 'tagline': co.report_header or 'Shop Floor Terminal', + 'logo_url': f'/web/image/res.company/{co.id}/logo', + 'has_logo': bool(co.logo), + 'initials': _initials_from(co.name), + } + + class FpTabletController(http.Controller): """Tablet PIN gate endpoints. All require an authenticated Odoo session (the tablet logs in once as a 'shopfloor service' user). @@ -185,13 +248,24 @@ class FpTabletController(http.Controller): tiles.append({ 'user_id': u.id, 'name': u.name, + 'initials': _initials_from(u.name), 'avatar_url': f'/web/image/res.users/{u.id}/avatar_128', + # has_photo lets the frontend skip the avatar img when + # the user has no uploaded photo (avoids the 1×1 default + # image flash). sudo-read of image_128 — the field is + # restricted to the user themselves otherwise. + 'has_photo': bool(u_sudo.image_128), + 'avatar_gradient': _avatar_gradient_for(u.id), 'is_clocked_in': u.id in clocked_ids, 'has_pin': bool(u_sudo.x_fc_tablet_pin_hash), }) # Clocked-in first, then alphabetical within bucket tiles.sort(key=lambda t: (not t['is_clocked_in'], t['name'])) - return {'ok': True, 'tiles': tiles} + return { + 'ok': True, + 'company': _lock_company_payload(request.env), + 'tiles': tiles, + } # ====================================================================== # /fp/tablet/ping — heartbeat used by the OWL component on every action diff --git a/fusion_plating/fusion_plating_shopfloor/tests/__init__.py b/fusion_plating/fusion_plating_shopfloor/tests/__init__.py index b4407567..7679f60e 100644 --- a/fusion_plating/fusion_plating_shopfloor/tests/__init__.py +++ b/fusion_plating/fusion_plating_shopfloor/tests/__init__.py @@ -2,3 +2,4 @@ from . import test_workspace_controller from . import test_landing_kanban from . import test_tablet_pin +from . import test_tablet_lock_payload diff --git a/fusion_plating/fusion_plating_shopfloor/tests/test_tablet_lock_payload.py b/fusion_plating/fusion_plating_shopfloor/tests/test_tablet_lock_payload.py new file mode 100644 index 00000000..71e9d783 --- /dev/null +++ b/fusion_plating/fusion_plating_shopfloor/tests/test_tablet_lock_payload.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- +# Copyright 2026 Nexa Systems Inc. +# License OPL-1 (Odoo Proprietary License v1.0) +"""Tests for the tablet lock-screen payload helpers. + +Covers the 3 module-level helpers added in 2026-05-24 redesign: + - _initials_from(name) — letter-mark fallback for missing logo/photo + - _avatar_gradient_for(uid) — deterministic per-user color gradient + - _lock_company_payload(env) — company name + tagline + logo URL block + +End-to-end test of the /fp/tablet/tiles endpoint payload shape: +just verifies the helper output ends up in the response. +""" +from odoo.tests.common import TransactionCase +from odoo.addons.fusion_plating_shopfloor.controllers.tablet_controller import ( + _AVATAR_GRADIENTS, + _avatar_gradient_for, + _initials_from, + _lock_company_payload, +) + + +class TestInitialsFrom(TransactionCase): + + def test_empty_returns_question_mark(self): + self.assertEqual(_initials_from(''), '?') + self.assertEqual(_initials_from(None), '?') + + def test_single_word_returns_first_two_chars_upper(self): + self.assertEqual(_initials_from('Garry'), 'GA') + self.assertEqual(_initials_from('ab'), 'AB') + self.assertEqual(_initials_from('z'), 'Z') + + def test_multi_word_returns_first_and_last_initial(self): + self.assertEqual(_initials_from('Garry Singh'), 'GS') + self.assertEqual(_initials_from('Johnny Matloub'), 'JM') + self.assertEqual(_initials_from('Mary Anne Smith'), 'MS') + self.assertEqual(_initials_from('EN Technologies'), 'ET') + + def test_extra_whitespace_handled(self): + self.assertEqual(_initials_from(' Garry Singh '), 'GS') + self.assertEqual(_initials_from('Garry\tSingh'), 'GS') + + +class TestAvatarGradientFor(TransactionCase): + + def test_deterministic_per_user_id(self): + # Same id → same gradient across calls + self.assertEqual( + _avatar_gradient_for(5), + _avatar_gradient_for(5), + ) + + def test_modulo_distribution(self): + # Wrapping wraps cleanly — id 0 and id len(gradients) match + n = len(_AVATAR_GRADIENTS) + self.assertEqual(_avatar_gradient_for(0), _avatar_gradient_for(n)) + self.assertEqual(_avatar_gradient_for(3), _avatar_gradient_for(n + 3)) + + def test_returns_a_known_gradient(self): + # Every output is one of the documented gradients + for uid in range(50): + self.assertIn(_avatar_gradient_for(uid), _AVATAR_GRADIENTS) + + +class TestLockCompanyPayload(TransactionCase): + """Covers _lock_company_payload's shape + fallback behavior.""" + + def test_payload_has_required_keys(self): + payload = _lock_company_payload(self.env) + for key in ('id', 'name', 'tagline', 'logo_url', 'has_logo', 'initials'): + self.assertIn(key, payload, f'missing key: {key}') + self.assertEqual(payload['id'], self.env.company.id) + self.assertTrue(payload['logo_url'].startswith('/web/image/res.company/')) + + def test_tagline_default_when_empty_report_header(self): + self.env.company.report_header = False + payload = _lock_company_payload(self.env) + # Falls back to a non-empty string, not False/None + self.assertTrue(payload['tagline']) + self.assertNotEqual(payload['tagline'], False) + + # NOTE: a "report_header populated → tagline matches" test would be + # brittle here because res.company.report_header is an HTML field in + # Odoo 19: setting a plain string can come back wrapped in
tags + # after sanitization. The helper's responsibility is just "use the + # field's value when present, else fall back" — covered by + # test_tagline_default_when_empty_report_header above. + + def test_initials_match_company_name(self): + self.env.company.name = 'EN Technologies' + payload = _lock_company_payload(self.env) + self.assertEqual(payload['initials'], 'ET')