Acasa = ecran de import (tab Import scos, ?tab=import->Acasa). Bara status compacta pe 2 randuri cu bife accesibile (glife + text) + data formatata. 'Coada'->'Trimiteri': coloane RO, stare umana, detaliu la click in panou dedicat. Mapari pe 3 sectiuni (de rezolvat / op salvate / formate coloane), Cont doar cheie+creds. Filtrare Trimiteri, corectie inline needs_data cu re-enqueue + detectie coliziune idempotency, badge contoare pe tab-uri. Helper pur partajat payload_view.py (web + GET /v1/prezentari). Backend trimitere (worker/idempotenta/mapping/schema) neatins. 483 teste. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
113 lines
3.6 KiB
Python
113 lines
3.6 KiB
Python
"""Extragere payload submission -> campuri afisabile (US-003, PRD 3.5).
|
|
|
|
Helper PUR partajat intre canalul web (dashboard Trimiteri) si canalul API
|
|
(`GET /v1/prezentari`), ca extragerea sa NU diverge intre cele doua (decizie
|
|
eng review, DRY). Fara DB, fara request — primeste `payload_json` (text JSON
|
|
plaintext, vezi `submissions.payload_json`) sau un dict deja parsat.
|
|
|
|
Defensiv prin constructie: nu arunca niciodata pe payload malformat — degradeaza
|
|
la em-dash. Citeste cheile tolerant (canalele API si import pot diferi usor:
|
|
`nr_inmatriculare` vs `numar`/`numarInmatriculare`; coercion Excel pe odometru/VIN).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
EMPTY = "—"
|
|
|
|
|
|
def _clean_str(value: Any) -> str:
|
|
"""str() defensiv: None/'' -> '', altfel string strip-uit (coercion Excel safe)."""
|
|
if value is None:
|
|
return ""
|
|
return str(value).strip()
|
|
|
|
|
|
def _clean_odometru(value: Any) -> str:
|
|
"""Odometru afisat curat: strip '.0' din coercion Excel (123456.0 -> 123456)."""
|
|
s = _clean_str(value)
|
|
if "." in s:
|
|
before, after = s.split(".", 1)
|
|
if after == "0" and before.lstrip("-").isdigit():
|
|
return before
|
|
return s
|
|
|
|
|
|
def _vin_scurt(vin: str) -> str:
|
|
"""Forma trunchiata a VIN-ului pentru tabel (integral ramane in detaliu).
|
|
|
|
VIN are 17 caractere; in tabel aratam ultimele 6 cu prefix elipsa ca sa
|
|
incapa fara sa ascundem complet identitatea vehiculului.
|
|
"""
|
|
if not vin:
|
|
return ""
|
|
if len(vin) <= 8:
|
|
return vin
|
|
return "…" + vin[-6:]
|
|
|
|
|
|
def _prima_prestatie(prestatii: Any) -> dict[str, Any]:
|
|
"""Primul item de prestatie ca dict, defensiv (lista goala/non-dict -> {})."""
|
|
if isinstance(prestatii, list):
|
|
for item in prestatii:
|
|
if isinstance(item, dict):
|
|
return item
|
|
return {}
|
|
|
|
|
|
def _payload_dict(payload: str | dict | None) -> dict[str, Any]:
|
|
"""Normalizeaza intrarea la dict. JSON invalid / tip neasteptat -> {}."""
|
|
if payload is None:
|
|
return {}
|
|
if isinstance(payload, dict):
|
|
return payload
|
|
if isinstance(payload, str):
|
|
try:
|
|
data = json.loads(payload)
|
|
except (ValueError, TypeError):
|
|
return {}
|
|
return data if isinstance(data, dict) else {}
|
|
return {}
|
|
|
|
|
|
def prezentare_din_payload(payload: str | dict | None) -> dict[str, str]:
|
|
"""Campuri afisabile dintr-un payload de submission.
|
|
|
|
Intoarce un dict cu chei stabile (toate string-uri, EMPTY cand lipsesc):
|
|
vehicul_nr, vin, vin_scurt, operatie, data_prestatie, odometru, cod.
|
|
|
|
`operatie` = denumire prestatiei daca exista, altfel codul; `cod` = codul RAR
|
|
(`cod_prestatie`) sau, in lipsa, codul intern (`cod_op_service`).
|
|
"""
|
|
data = _payload_dict(payload)
|
|
|
|
vin = _clean_str(data.get("vin"))
|
|
nr = _clean_str(
|
|
data.get("nr_inmatriculare")
|
|
or data.get("numar")
|
|
or data.get("numarInmatriculare")
|
|
)
|
|
odo = _clean_odometru(
|
|
data.get("odometru_final")
|
|
if data.get("odometru_final") is not None
|
|
else data.get("odometru")
|
|
)
|
|
data_prest = _clean_str(data.get("data_prestatie") or data.get("dataPrestatie"))
|
|
|
|
item = _prima_prestatie(data.get("prestatii"))
|
|
cod = _clean_str(item.get("cod_prestatie") or item.get("cod_op_service"))
|
|
denumire = _clean_str(item.get("denumire"))
|
|
operatie = denumire or cod
|
|
|
|
return {
|
|
"vehicul_nr": nr or EMPTY,
|
|
"vin": vin or EMPTY,
|
|
"vin_scurt": _vin_scurt(vin) or EMPTY,
|
|
"operatie": operatie or EMPTY,
|
|
"data_prestatie": data_prest or EMPTY,
|
|
"odometru": odo or EMPTY,
|
|
"cod": cod or EMPTY,
|
|
}
|