diff --git a/main/food/api.py b/main/food/api.py
index fc3db0f..c754df4 100644
--- a/main/food/api.py
+++ b/main/food/api.py
@@ -24,6 +24,7 @@ from .models import (ApiIdempotencyRecord, FoodLogAttachment, FoodLogEntry, Food
InventoryCategory, InventoryLocation, KitchenInventoryItem,
MealType, PersonalAccessToken, Unit)
from .services.logging import apply_product_snapshot
+from .services.diary import nutrition_breakdown
from .services.hydration import sync_food_hydration, validate_rules
from planner.services.hydration import daily_hydration
from .services.openfoodfacts import OpenFoodFactsError
@@ -250,20 +251,20 @@ def today(request):
if day is None or day.isoformat() != requested_date:
return error('invalid_date', 'Use a date in YYYY-MM-DD format.')
entries = list(FoodLogEntry.objects.filter(user=request.api_user, date=day)
- .select_related('product').prefetch_related('attachments'))
+ .select_related('product', 'recipe_cooking_run').prefetch_related('attachments'))
meals = {meal: [_entry_data(e) for e in entries if e.meal == meal] for meal in MealType.values}
- totals = {name: str(sum((getattr(e, name) or Decimal('0') for e in entries), Decimal('0')))
- for name in NUTRIENTS if name != 'fibre_g'}
- fibre_values = [entry.fibre_g for entry in entries if entry.fibre_g is not None]
- fibre_incomplete = len(fibre_values) != len(entries)
- totals['fibre_g'] = str(sum(fibre_values, Decimal('0'))) if fibre_values or not entries else None
+ known_totals, known_counts, incomplete, _ = nutrition_breakdown(entries)
+ totals = {name: str(known_totals[name].quantize(Decimal('0.001'))) if known_counts[name] or not entries else None
+ for name in NUTRIENTS}
+ fibre_incomplete = incomplete['fibre_g']
goals = UserSettings.objects.filter(user=request.api_user).first()
goal_data = {name: str(getattr(goals, name)) if goals and getattr(goals, name) is not None else None
for name in ('daily_calorie_goal', 'carbohydrate_goal_g', 'protein_goal_g', 'fat_goal_g', 'fibre_goal_g')}
if goals is None:
goal_data['fibre_goal_g'] = '40.0'
return JsonResponse({'date': day.isoformat(), 'meals': meals, 'totals': totals,
- 'fibre_incomplete': fibre_incomplete, 'goals': goal_data})
+ 'incomplete': incomplete, 'fibre_incomplete': fibre_incomplete,
+ 'goals': goal_data})
@api_view(None, ('GET',))
diff --git a/main/food/services/diary.py b/main/food/services/diary.py
index 4fcf502..8b96f7e 100644
--- a/main/food/services/diary.py
+++ b/main/food/services/diary.py
@@ -11,6 +11,34 @@ from planner.timezones import localdate_for_user
ZERO = Decimal('0')
+NUTRIENT_NAMES = ('calories_kcal', 'carbohydrates_g', 'protein_g', 'fat_g', 'fibre_g', 'sugars_g', 'salt_g')
+NUTRIENT_LABELS = {'calories_kcal': 'calories', 'carbohydrates_g': 'carbs',
+ 'protein_g': 'protein', 'fat_g': 'fat', 'fibre_g': 'fibre',
+ 'sugars_g': 'sugars', 'salt_g': 'salt'}
+
+
+def nutrition_breakdown(entries):
+ """Known subtotals and completeness, based only on saved diary snapshots."""
+ totals = {name: ZERO for name in NUTRIENT_NAMES}
+ known_counts = {name: 0 for name in NUTRIENT_NAMES}
+ incomplete = {name: False for name in NUTRIENT_NAMES}
+ missing_entries = []
+ for entry in entries:
+ run = getattr(entry, 'recipe_cooking_run', None)
+ snapshot = run.nutrition_snapshot if run else None
+ missing = []
+ for name in NUTRIENT_NAMES:
+ value = entry.effective_nutrient(name)
+ if value is not None:
+ totals[name] += value
+ known_counts[name] += 1
+ if value is None or (snapshot is not None and not snapshot.get(name, {}).get('complete', False)):
+ incomplete[name] = True
+ missing.append(NUTRIENT_LABELS[name])
+ entry.nutrition_incomplete = bool(missing)
+ if missing:
+ missing_entries.append({'entry': entry, 'nutrients': ', '.join(missing)})
+ return totals, known_counts, incomplete, missing_entries
def selected_date(request):
@@ -27,20 +55,9 @@ def diary_context(user, day, language='en'):
goals, _ = UserSettings.objects.get_or_create(user=user)
entries = list(FoodLogEntry.objects.filter(user=user, date=day).select_related('product', 'recipe_cooking_run')
.prefetch_related('attachments').defer('product__raw_data'))
+ totals, known_counts, incomplete, missing_entries = nutrition_breakdown(entries)
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()))
- 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)
- 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]
@@ -54,16 +71,18 @@ def diary_context(user, day, language='en'):
('Fibre', 'fibre_g', goals.fibre_goal_g, 'green'),
):
eaten = totals[name]
- incomplete = name == 'fibre_g' and fibre_incomplete
+ is_incomplete = incomplete[name]
macros.append({'label': label, 'slug': name, 'guidance': nutrient_guidance(name, language),
- 'eaten': eaten, 'goal': goal,
- 'remaining': goal - eaten if goal is not None and eaten is not None and not incomplete else None,
- 'percent': min(100, int(eaten * 100 / goal)) if goal and eaten is not None else None,
- 'color': color, 'incomplete': incomplete})
+ 'eaten': eaten if known_counts[name] or not entries else None, 'goal': goal,
+ 'remaining': goal - eaten if goal is not None and not is_incomplete else None,
+ 'percent': min(100, int(eaten * 100 / goal)) if goal and (known_counts[name] or not entries) else None,
+ 'color': color, 'incomplete': is_incomplete})
calorie_goal = goals.daily_calorie_goal
calories = totals['calories_kcal']
return {'day': day, 'previous_day': day - timedelta(days=1), 'next_day': day + timedelta(days=1),
'meals': meals, 'totals': totals, 'macros': macros, 'help_language': language,
+ 'known_counts': known_counts, 'nutrition_incomplete': incomplete,
+ 'missing_entries': missing_entries,
'calorie_goal': calorie_goal,
- '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}
+ 'calorie_remaining': Decimal(calorie_goal) - calories if calorie_goal is not None and not incomplete['calories_kcal'] else None,
+ 'calorie_percent': min(100, int(calories * 100 / calorie_goal)) if calorie_goal else 0}
diff --git a/main/food/static/food/food.css b/main/food/static/food/food.css
index b807977..eac4c9b 100644
--- a/main/food/static/food/food.css
+++ b/main/food/static/food/food.css
@@ -24,6 +24,8 @@ body[data-theme="dark"]{--food-surface:#202923;--food-border:#3b473f;--food-ink:
.food-export-actions a{color:var(--food-ink);border-color:var(--food-border)}
.food-export-actions a:hover,.food-export-actions a:focus-visible{background:var(--food-progress-track)}
.food-macro p,.food-product-list small{color:var(--food-muted)}
+.food-warning-mark{display:inline-grid;place-items:center;width:1.25em;height:1.25em;border-radius:50%;background:#a15a12;color:#fff;font-size:.85em;font-weight:800;vertical-align:middle}
+.food-incomplete-warning{border-color:#b98b45}.food-incomplete-warning h2{font-size:1.1rem}.food-incomplete-warning p{line-height:1.45}.food-incomplete-warning details{margin-top:.65rem}.food-incomplete-warning summary{cursor:pointer;font-weight:700}.food-incomplete-warning ul{padding-left:1.25rem;margin:.6rem 0 0}.food-incomplete-warning li{margin:.4rem 0;overflow-wrap:anywhere}.food-partial-note{line-height:1.4}
body[data-theme="dark"] .food-tabs a[aria-current=page]{color:#102317}
/* Four goals fit the existing nutrition sidebar in a compact two-column grid. */
@@ -47,6 +49,7 @@ body[data-theme="dark"] .food-tabs a[aria-current=page]{color:#102317}
/* Nutrient guidance sits above the card content and stays within the viewport. */
.food-macro{position:relative;padding-top:2.8rem}
+@media(max-width:460px){.food-macros{grid-template-columns:1fr}.food-macro{flex-direction:row;align-items:center;text-align:left;gap:1rem;padding:.85rem 3rem .85rem .85rem}.food-macro h3{font-size:1rem;white-space:normal}.food-macro p{font-size:.88rem;line-height:1.35}.food-ring{flex:none}}
.food-help-trigger{position:absolute;top:.25rem;right:.25rem;display:grid;place-items:center;width:44px;height:44px;padding:0;border:0;background:transparent;color:var(--food-ink);cursor:pointer}
.food-help-trigger span{display:grid;place-items:center;width:24px;height:24px;border:1px solid var(--food-muted);border-radius:50%;font:700 .85rem/1 inherit}
.food-help-trigger:hover span,.food-help-trigger[aria-expanded="true"] span{background:var(--food-progress-track)}
diff --git a/main/food/templates/food/diary.html b/main/food/templates/food/diary.html
index 1b88ae4..bb701f4 100644
--- a/main/food/templates/food/diary.html
+++ b/main/food/templates/food/diary.html
@@ -2,7 +2,7 @@
{% load static %}
{% block title %}Food · Tasknest{% endblock %}
{% block content %}
-
+
{% include 'food/partials/diary_content.html' %}
diff --git a/main/food/templates/food/partials/nutrition_summary.html b/main/food/templates/food/partials/nutrition_summary.html
index ae50206..c7529bf 100644
--- a/main/food/templates/food/partials/nutrition_summary.html
+++ b/main/food/templates/food/partials/nutrition_summary.html
@@ -1,14 +1,41 @@
-
Calories
{% if calorie_goal is not None and totals.calories_kcal is not None %}
{% if calorie_remaining < 0 %}{{ calorie_remaining|floatformat:0|cut:'-' }} kcal over goal{% else %}{{ calorie_remaining|floatformat:0 }} kcal remaining{% endif %}
{% if macro.percent is not None %}{{ macro.percent }}%{% else %}—{% endif %}
-
{{ macro.label }}
{% if macro.eaten is None %}Unknown{% elif macro.incomplete %}At least {{ macro.eaten|floatformat:1 }} g{% else %}{{ macro.eaten|floatformat:1 }} g eaten{% endif %}{% if macro.goal is not None %} · {{ macro.goal|floatformat:1 }} g goal{% endif %}
{% if macro.incomplete %}
Some entries unknown
{% elif macro.goal is not None %}
{% if macro.remaining < 0 %}{{ macro.remaining|floatformat:1|cut:'-' }} g over target{% else %}{{ macro.remaining|floatformat:1 }} g remaining{% endif %}
+
+
+ {% endif %}
+
+ {% for macro in macros %}
+
+
{% if macro.percent is not None %}{{ macro.percent }}%{% else %}—{% endif %}
+
{{ macro.label }}
+
{% if macro.eaten is None %}Known amount unavailable{% elif macro.incomplete %}At least {{ macro.eaten|floatformat:1 }} g{% else %}{{ macro.eaten|floatformat:1 }} g eaten{% endif %}{% if macro.goal is not None %} · {{ macro.goal|floatformat:1 }} g goal{% endif %}
+ {% if macro.incomplete %}
! {% if macro.goal is not None %}Remaining unknown{% else %}Nutrition incomplete · No target{% endif %}
{% elif macro.goal is not None %}
{% if macro.remaining < 0 %}{{ macro.remaining|floatformat:1|cut:'-' }} g over target{% else %}{{ macro.remaining|floatformat:1 }} g remaining{% endif %}