food logger context tips

This commit is contained in:
Joren de Graaf
2026-10-11 15:21:10 +02:00
parent 559c51e9bb
commit 400e8254c0
51 changed files with 1988 additions and 41 deletions
+15 -5
View File
@@ -5,6 +5,7 @@ from django.utils import timezone
from django.utils.dateparse import parse_date
from food.models import FoodLogEntry, MealType
from food.services.nutrient_help import nutrient_guidance
from planner.models import UserSettings
from planner.timezones import localdate_for_user
@@ -22,12 +23,15 @@ def selected_date(request):
return day
def diary_context(user, day):
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')
.prefetch_related('attachments').defer('product__raw_data'))
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)
totals['fibre_g'] = sum(fibre_values, ZERO) if fibre_values or not entries else None
meals = []
for key, label in MealType.choices:
items = [entry for entry in entries if entry.meal == key]
@@ -35,16 +39,22 @@ def diary_context(user, day):
'calories': sum((entry.calories_kcal or ZERO for entry in items), ZERO)})
macros = []
for label, name, goal, color in (
('Carbohydrates', 'carbohydrates_g', goals.carbohydrate_goal_g, 'pink'),
('Protein', 'protein_g', goals.protein_goal_g, 'blue'),
('Carbohydrates', 'carbohydrates_g', goals.carbohydrate_goal_g, 'pink'),
('Fat', 'fat_g', goals.fat_goal_g, 'orange'),
('Fibre', 'fibre_g', goals.fibre_goal_g, 'green'),
):
eaten = totals[name]
macros.append({'label': label, 'eaten': eaten, 'goal': goal, 'remaining': goal - eaten if goal is not None else None,
'percent': min(100, int(eaten * 100 / goal)) if goal else None, 'color': color})
incomplete = name == 'fibre_g' and fibre_incomplete
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})
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, 'calorie_goal': calorie_goal,
'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}