feat(stations): ricette in lista, reset per riga e cambio stazione al volo

Punto 7 del documento modifiche del 28/07, terza richiesta di Menoncin.

GET /api/stations ora usa StationWithRecipesResponse, che era gia' definita nello
schema senza che nessun endpoint la usasse. Non costa query in piu':
Station.assignments e' gia' selectin-loaded. In lista compaiono le sole ricette
attive, coerente con cio' che l'operatore vede davvero a quella stazione.

DELETE /api/stations/{id}/recipes azzera le assegnazioni di una stazione senza
eliminarla, cosi' si riassegna da capo. E' idempotente: azzerare una stazione gia'
vuota risponde removed=0, non 404. In tabella e' un pulsante per riga, con
conferma perche' l'azione e' distruttiva.

Il cambio stazione al volo (?station=CODE, override in sessione) serve al
collaudo: senza, provare N stazioni richiede N PC. E' dietro il flag
STATION_SWITCH_ENABLED, default 0. Non e' legato al ruolo admin perche'
/measure/select richiede MeasurementTec, e gatearlo su is_admin avrebbe escluso
proprio chi fa il collaudo. In fabbrica l'identita' della stazione viene
dall'installazione locale, e misurare contro le ricette di un'altra stazione
romperebbe in silenzio la tracciabilita': per questo il codice richiesto e'
validato prima di essere memorizzato - un refuso lascia l'operatore sulla
stazione configurata con un messaggio, invece di incastrarlo su una stazione
inesistente - e quando l'override e' attivo la pagina lo dichiara, con il codice
configurato accanto e un link per ripristinare.

Le traduzioni: pybabel update aveva indovinato dieci voci da stringhe simili
marcandole fuzzy, in entrambi i cataloghi. "Azzera Stazione" era diventato "Crea
Stazione", l'opposto, su un dialogo distruttivo. Non si vedeva perche' la
compilazione salta le fuzzy e ricade sul sorgente italiano, ma sarebbe emerso al
primo build con --use-fuzzy. Tradotte per esteso le 17 stringhe nuove in IT ed EN
e tolti i flag.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adriano Dal Pastro
2026-07-28 15:42:17 +00:00
parent 28ee44bcf1
commit a159eb2b8b
15 changed files with 1188 additions and 363 deletions
+64 -4
View File
@@ -12,6 +12,61 @@ from services.file_proxy import proxy_file
measure_bp = Blueprint("measure", __name__)
# Session key holding a station chosen at runtime, overriding Config.STATION_CODE.
STATION_OVERRIDE_KEY = "station_override"
def _current_station() -> tuple[str | None, bool]:
"""Return the station this client acts as, and whether it is an override."""
override = session.get(STATION_OVERRIDE_KEY)
if override:
return override, True
return Config.STATION_CODE, False
def _apply_station_switch() -> None:
"""Honour ?station=CODE, used to exercise several stations from one PC.
Only active when STATION_SWITCH_ENABLED is set: in production the station
identity comes from the local install, and silently measuring against another
station's recipes would break traceability. An empty value clears the override
and falls back to the configured station.
The requested code is validated against the server before being stored, so a
typo leaves the operator on the current station with an explanation rather
than stranded on a station that does not exist.
"""
if "station" not in request.args:
return
requested = (request.args.get("station") or "").strip().upper()
if not Config.STATION_SWITCH_ENABLED:
flash(_("Il cambio stazione da URL non è abilitato su questa postazione."), "error")
return
if not requested:
if session.pop(STATION_OVERRIDE_KEY, None):
flash(_("Stazione riportata a quella configurata."), "info")
return
try:
probe = api_client.get_station_recipes(requested)
except Exception:
flash(_("Impossibile contattare il server per cambiare stazione."), "error")
return
if isinstance(probe, dict) and probe.get("error"):
flash(
_("Stazione '%(code)s' inesistente o disattivata: stazione invariata.",
code=requested),
"error",
)
return
session[STATION_OVERRIDE_KEY] = requested
flash(_("Stazione corrente: %(code)s", code=requested), "info")
# ---------------------------------------------------------------------------
# Route: Recipe selection
@@ -21,13 +76,16 @@ measure_bp = Blueprint("measure", __name__)
@role_required("MeasurementTec")
def select_recipe():
"""Recipe selection page with search and barcode support."""
# Fail-fast if STATION_CODE is not configured
if not Config.STATION_CODE:
_apply_station_switch()
station_code, station_overridden = _current_station()
# Fail-fast if no station is configured and none was chosen
if not station_code:
return render_template("errors/station_not_configured.html"), 503
# Load recipes filtered by station
try:
resp = api_client.get_station_recipes(Config.STATION_CODE)
resp = api_client.get_station_recipes(station_code)
except Exception as e:
return render_template(
"errors/station_not_configured.html", error=str(e),
@@ -52,7 +110,9 @@ def select_recipe():
return render_template(
"measure/select_recipe.html",
recipes=recipes,
station_code=Config.STATION_CODE,
station_code=station_code,
station_overridden=station_overridden,
station_configured=Config.STATION_CODE,
auto_recipe_code=auto_recipe_code,
auto_lot=auto_lot,
auto_serial=auto_serial,