food diary bug fix, blocking calorie count

This commit is contained in:
Joren de Graaf
2026-10-18 00:03:47 +02:00
parent e3b14d6823
commit a8418cdba7
8 changed files with 150 additions and 41 deletions
+8 -7
View File
@@ -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',))