Handle quantity ranges (e.g. 2-3) in Mealie and Tandoor clients
Both APIs require numeric quantity/amount fields. When a range like "2-3" is detected, uses the first number as the quantity and puts the full range (e.g. "2- 3 ek") in the note field. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+22
-5
@@ -5,6 +5,25 @@ import re
|
||||
import requests
|
||||
|
||||
|
||||
def _parse_qty(qty_str: str) -> tuple[float, str]:
|
||||
"""Parse a quantity string, handling ranges like '2-3' or '6- 8'.
|
||||
|
||||
Returns (number, range_note) where range_note is the original range
|
||||
string if the quantity is a range, or empty string if it's a plain number.
|
||||
"""
|
||||
if not qty_str:
|
||||
return (0, "")
|
||||
try:
|
||||
return (float(qty_str.replace(",", ".")), "")
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
m = re.match(r"^(\d+(?:[.,]\d+)?)\s*-\s*(\d+(?:[.,]\d+)?)$", qty_str.strip())
|
||||
if m:
|
||||
first = float(m.group(1).replace(",", "."))
|
||||
return (first, qty_str.strip())
|
||||
return (0, "")
|
||||
|
||||
|
||||
class TandoorClient:
|
||||
"""Thin wrapper around the Tandoor REST API."""
|
||||
|
||||
@@ -184,11 +203,9 @@ class TandoorClient:
|
||||
has_structured = bool(qty_str or unit_str)
|
||||
|
||||
if has_structured and food_str:
|
||||
qty = 0
|
||||
try:
|
||||
qty = float(qty_str.replace(",", "."))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
qty, range_note = _parse_qty(qty_str)
|
||||
if range_note:
|
||||
extra = f"{range_note} {unit_str}; {extra}".strip("; ") if extra else f"{range_note} {unit_str}".strip()
|
||||
|
||||
return {
|
||||
"food": {"name": food_str},
|
||||
|
||||
Reference in New Issue
Block a user