fix: safe int/float helpers for dirty Excel data (min_box_qty, stock, etc.)
This commit is contained in:
32
main.py
32
main.py
@@ -17,6 +17,28 @@ EXCEL_URL = os.getenv(
|
||||
REFRESH_HOURS = int(os.getenv("REFRESH_HOURS", "6"))
|
||||
|
||||
app = FastAPI(title="MTZ Stock API", version="1.0.0")
|
||||
|
||||
|
||||
def safe_int(value, default):
|
||||
if value is None:
|
||||
return default
|
||||
if isinstance(value, (int, float)):
|
||||
return int(value)
|
||||
try:
|
||||
return int(str(value).strip())
|
||||
except (ValueError, TypeError):
|
||||
return default
|
||||
|
||||
|
||||
def safe_float(value, default):
|
||||
if value is None:
|
||||
return default
|
||||
if isinstance(value, (int, float)):
|
||||
return float(value)
|
||||
try:
|
||||
return float(str(value).strip())
|
||||
except (ValueError, TypeError):
|
||||
return default
|
||||
products_cache = []
|
||||
last_refresh = None
|
||||
cache_lock = threading.Lock()
|
||||
@@ -49,16 +71,16 @@ def download_and_parse():
|
||||
ean = str(ean_raw).strip()
|
||||
brand_raw = row[9] # col J
|
||||
brand = str(brand_raw) if brand_raw is not None else ""
|
||||
min_box = row[10] if row[10] is not None else 1 # col K
|
||||
min_box_raw = row[10] # col K
|
||||
parsed.append(
|
||||
{
|
||||
"item_code": int(row[1]),
|
||||
"item_code": safe_int(row[1], 0),
|
||||
"ean": ean,
|
||||
"description": str(row[2] or ""),
|
||||
"price_usd": float(row[4] or 0),
|
||||
"stock": int(row[5] or 0),
|
||||
"price_usd": safe_float(row[4], 0.0),
|
||||
"stock": safe_int(row[5], 0),
|
||||
"brand": brand,
|
||||
"min_box_qty": int(min_box),
|
||||
"min_box_qty": safe_int(min_box_raw, 1),
|
||||
}
|
||||
)
|
||||
with cache_lock:
|
||||
|
||||
Reference in New Issue
Block a user