food logger context tips
This commit is contained in:
@@ -68,6 +68,15 @@ def map_product(data):
|
||||
match = re.search(r'(?i)(\d+(?:[.,]\d+)?)\s*(g|ml)\b', serving_text)
|
||||
serving_amount = _decimal(match.group(1).replace(',', '.')) if match else None
|
||||
serving_unit = match.group(2).lower() if match else ''
|
||||
package_text = str(data.get('quantity') or '')[:100]
|
||||
package_match = re.fullmatch(r'\s*(\d+(?:[.,]\d+)?)\s*(kg|g|ml|l)\s*', package_text, re.IGNORECASE)
|
||||
package_amount = _decimal(package_match.group(1).replace(',', '.')) if package_match else None
|
||||
package_unit = package_match.group(2).lower() if package_match else ''
|
||||
if package_amount is not None and package_unit in ('kg', 'l'):
|
||||
package_amount *= 1000
|
||||
package_unit = 'g' if package_unit == 'kg' else 'ml'
|
||||
if package_amount is not None and package_amount <= 0:
|
||||
package_amount = None
|
||||
image_url = str(data.get('image_front_small_url') or data.get('image_front_url') or '')
|
||||
try:
|
||||
parsed_image = urlparse(image_url)
|
||||
@@ -78,7 +87,9 @@ def map_product(data):
|
||||
image_url = ''
|
||||
return {'barcode': code[:64], 'product_name': name[:255], 'brand': str(data.get('brands') or '')[:255],
|
||||
'image_url': image_url[:200],
|
||||
'package_quantity': str(data.get('quantity') or '')[:100], 'serving_size_text': serving_text,
|
||||
'package_quantity': package_text, 'package_amount': package_amount,
|
||||
'package_unit': package_unit if package_amount is not None else '',
|
||||
'serving_size_text': serving_text,
|
||||
'serving_amount': serving_amount, 'serving_unit': serving_unit,
|
||||
'base_unit': Unit.MILLILITRE if serving_unit == 'ml' else Unit.GRAM, **values}
|
||||
|
||||
@@ -148,7 +159,7 @@ def cached_product(user, barcode, *, refresh=False):
|
||||
return product
|
||||
raise
|
||||
mapped = map_product(data)
|
||||
if not mapped:
|
||||
if not mapped or mapped['barcode'] != barcode:
|
||||
return product
|
||||
mapped['source'] = FoodProduct.Source.OPEN_FOOD_FACTS
|
||||
mapped['fetched_at'] = timezone.now()
|
||||
|
||||
Reference in New Issue
Block a user