quantity fields in food log and recipe upload drop zone

This commit is contained in:
Joren de Graaf
2026-10-15 20:35:56 +02:00
parent f36f05b9bc
commit 86e3afaca7
25 changed files with 420 additions and 67 deletions
+10 -8
View File
@@ -28,22 +28,24 @@ def diary_context(user, day, language='en'):
entries = list(FoodLogEntry.objects.filter(user=user, date=day).select_related('product', 'recipe_cooking_run')
.prefetch_related('attachments').defer('product__raw_data'))
for entry in entries:
entry.effective_calories = entry.effective_nutrient('calories_kcal')
run = getattr(entry, 'recipe_cooking_run', None)
entry.nutrition_incomplete = bool(run and any(
not row.get('complete', False) for row in run.nutrition_snapshot.values()))
totals = {name: sum((getattr(entry, name) or ZERO for entry in entries), ZERO) for name in
('calories_kcal', 'carbohydrates_g', 'protein_g', 'fat_g')}
fibre_values = [entry.fibre_g for entry in entries if entry.fibre_g is not None]
fibre_incomplete = len(fibre_values) != len(entries) or any(
names = ('calories_kcal', 'carbohydrates_g', 'protein_g', 'fat_g', 'fibre_g', 'sugars_g', 'salt_g')
totals = {name: sum((entry.effective_nutrient(name) or ZERO for entry in entries), ZERO)
if all(getattr(entry, name) is not None for entry in entries) else None for name in names}
fibre_incomplete = totals['fibre_g'] is None or any(
(getattr(entry, 'recipe_cooking_run', None) and
not entry.recipe_cooking_run.nutrition_snapshot.get('fibre_g', {}).get('complete', False))
for entry in entries)
totals['fibre_g'] = sum(fibre_values, ZERO) if fibre_values or not entries else None
if fibre_incomplete:
totals['fibre_g'] = sum((entry.effective_nutrient('fibre_g') or ZERO for entry in entries), ZERO) if any(entry.fibre_g is not None for entry in entries) else None
meals = []
for key, label in MealType.choices:
items = [entry for entry in entries if entry.meal == key]
meals.append({'key': key, 'label': label, 'entries': items,
'calories': sum((entry.calories_kcal or ZERO for entry in items), ZERO)})
'calories': sum((entry.effective_calories or ZERO for entry in items), ZERO)})
macros = []
for label, name, goal, color in (
('Protein', 'protein_g', goals.protein_goal_g, 'blue'),
@@ -63,5 +65,5 @@ def diary_context(user, day, language='en'):
return {'day': day, 'previous_day': day - timedelta(days=1), 'next_day': day + timedelta(days=1),
'meals': meals, 'totals': totals, 'macros': macros, 'help_language': language,
'calorie_goal': calorie_goal,
'calorie_remaining': Decimal(calorie_goal) - calories if calorie_goal is not None else None,
'calorie_percent': min(100, int(calories * 100 / calorie_goal)) if calorie_goal else 0}
'calorie_remaining': Decimal(calorie_goal) - calories if calorie_goal is not None and calories is not None else None,
'calorie_percent': min(100, int(calories * 100 / calorie_goal)) if calorie_goal and calories is not None else 0}