food diary bug fix, blocking calorie count
This commit is contained in:
+38
-19
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user