v0.1.27
Fix: Throughput API v1 available - Details posted to Irving. POWERBI_KEY was missing from the .ENV file, so was not live. Add: Editor now supports editing a mix's resolved formula directly, with % and kg dual entry on ingredient rows Fix: Mix Editor should bring through correct ingredients. New resolved formula (same logic we use in Mix Calculator). Fix: Security headers on all API responses (hardening) Add: New mix button available on the Mix Editor. Add: New ingredient button available on the Ingredient Editor
This commit is contained in:
@@ -4,6 +4,7 @@ import csv
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from datetime import date, datetime
|
||||
from pathlib import Path
|
||||
from typing import Iterable
|
||||
@@ -149,7 +150,17 @@ def _coerce_text(value: object) -> str | None:
|
||||
return text
|
||||
|
||||
|
||||
def _coerce_date(value: object) -> date | None:
|
||||
# Default slash-date preference. The app is Australian, so an ambiguous
|
||||
# "x/y/z" is read day-first unless a column is detected as month-first.
|
||||
_DAY_FIRST_FORMATS = ("%Y-%m-%d", "%d/%m/%Y", "%m/%d/%Y")
|
||||
_MONTH_FIRST_FORMATS = ("%Y-%m-%d", "%m/%d/%Y", "%d/%m/%Y")
|
||||
|
||||
_SLASH_DATE_RE = re.compile(r"^\s*(\d{1,2})[/-](\d{1,2})[/-](\d{2,4})\s*$")
|
||||
|
||||
|
||||
def _coerce_date(
|
||||
value: object, formats: tuple[str, ...] = _DAY_FIRST_FORMATS
|
||||
) -> date | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, datetime):
|
||||
@@ -159,7 +170,7 @@ def _coerce_date(value: object) -> date | None:
|
||||
text = str(value).strip()
|
||||
if not text:
|
||||
return None
|
||||
for fmt in ("%Y-%m-%d", "%d/%m/%Y", "%m/%d/%Y"):
|
||||
for fmt in formats:
|
||||
try:
|
||||
return datetime.strptime(text, fmt).date()
|
||||
except ValueError:
|
||||
@@ -167,6 +178,32 @@ def _coerce_date(value: object) -> date | None:
|
||||
return None
|
||||
|
||||
|
||||
def _detect_slash_date_formats(values: Iterable[object]) -> tuple[str, ...]:
|
||||
"""Inspect every slash/dash date in a column and decide whether the file is
|
||||
day-first (D/M/Y) or month-first (M/D/Y), so all rows parse consistently.
|
||||
|
||||
A first component > 12 proves day-first; a second component > 12 proves
|
||||
month-first. If only month-first evidence exists we switch to M/D/Y;
|
||||
otherwise we keep the Australian day-first default.
|
||||
"""
|
||||
day_first = False
|
||||
month_first = False
|
||||
for value in values:
|
||||
if value is None or isinstance(value, (datetime, date)):
|
||||
continue
|
||||
match = _SLASH_DATE_RE.match(str(value))
|
||||
if not match:
|
||||
continue
|
||||
first, second = int(match.group(1)), int(match.group(2))
|
||||
if first > 12:
|
||||
day_first = True
|
||||
elif second > 12:
|
||||
month_first = True
|
||||
if month_first and not day_first:
|
||||
return _MONTH_FIRST_FORMATS
|
||||
return _DAY_FIRST_FORMATS
|
||||
|
||||
|
||||
def _infer_bulka_default(name: str, bag_size: float | None) -> bool:
|
||||
lowered = name.lower()
|
||||
if "bulka" in lowered:
|
||||
@@ -571,6 +608,10 @@ def import_entries_from_file(
|
||||
return None
|
||||
return row[idx]
|
||||
|
||||
# Decide the slash-date order once for the whole file so ambiguous values
|
||||
# like "12/9/2025" follow the same convention as the unambiguous ones.
|
||||
date_formats = _detect_slash_date_formats(cell(row, "date") for row in data_rows)
|
||||
|
||||
# Index existing products for matching (by item_id and by lower-cased name).
|
||||
by_item: dict[str, ThroughputProduct] = {}
|
||||
by_name: dict[str, ThroughputProduct] = {}
|
||||
@@ -596,7 +637,7 @@ def import_entries_from_file(
|
||||
if not row or all(value is None or str(value).strip() == "" for value in row):
|
||||
continue
|
||||
|
||||
production_date = _coerce_date(cell(row, "date"))
|
||||
production_date = _coerce_date(cell(row, "date"), date_formats)
|
||||
product_name = _coerce_text(cell(row, "product"))
|
||||
quantity = _coerce_float(cell(row, "quantity"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user