fixed food details not loading on details tab

This commit is contained in:
2025-10-01 10:58:01 -07:00
parent 617d8f8ca1
commit 63b3575797
4 changed files with 94 additions and 52 deletions

View File

@@ -807,35 +807,49 @@ async def detailed_tracked_day(request: Request, person: str = "Sarah", date: Op
foods = []
# Add foods from the base meal definition
for mf in tracked_meal.meal.meal_foods:
try:
serving_size_value = float(mf.food.serving_size)
num_servings = mf.quantity / serving_size_value if serving_size_value != 0 else 0
except (ValueError, TypeError):
num_servings = 0 # Fallback for invalid serving_size
foods.append({
'name': mf.food.name,
'quantity': mf.quantity,
'total_grams': mf.quantity,
'num_servings': num_servings,
'serving_size': mf.food.serving_size,
'serving_unit': mf.food.serving_unit,
'calories': mf.food.calories * mf.quantity,
'protein': mf.food.protein * mf.quantity,
'carbs': mf.food.carbs * mf.quantity,
'fat': mf.food.fat * mf.quantity,
'fiber': (mf.food.fiber or 0) * mf.quantity,
'sugar': (mf.food.sugar or 0) * mf.quantity,
'sodium': (mf.food.sodium or 0) * mf.quantity,
'calcium': (mf.food.calcium or 0) * mf.quantity,
'calories': mf.food.calories * num_servings,
'protein': mf.food.protein * num_servings,
'carbs': mf.food.carbs * num_servings,
'fat': mf.food.fat * num_servings,
'fiber': (mf.food.fiber or 0) * num_servings,
'sugar': (mf.food.sugar or 0) * num_servings,
'sodium': (mf.food.sodium or 0) * num_servings,
'calcium': (mf.food.calcium or 0) * num_servings,
})
# Add custom tracked foods (overrides or additions)
for tmf in tracked_meal.tracked_foods:
try:
serving_size_value = float(tmf.food.serving_size)
num_servings = tmf.quantity / serving_size_value if serving_size_value != 0 else 0
except (ValueError, TypeError):
num_servings = 0 # Fallback for invalid serving_size
foods.append({
'name': tmf.food.name,
'quantity': tmf.quantity,
'total_grams': tmf.quantity,
'num_servings': num_servings,
'serving_size': tmf.food.serving_size,
'serving_unit': tmf.food.serving_unit,
'calories': tmf.food.calories * tmf.quantity,
'protein': tmf.food.protein * tmf.quantity,
'carbs': tmf.food.carbs * tmf.quantity,
'fat': tmf.food.fat * tmf.quantity,
'fiber': (tmf.food.fiber or 0) * tmf.quantity,
'sugar': (tmf.food.sugar or 0) * tmf.quantity,
'sodium': (tmf.food.sodium or 0) * tmf.quantity,
'calcium': (tmf.food.calcium or 0) * tmf.quantity,
'calories': tmf.food.calories * num_servings,
'protein': tmf.food.protein * num_servings,
'carbs': tmf.food.carbs * num_servings,
'fat': tmf.food.fat * num_servings,
'fiber': (tmf.food.fiber or 0) * num_servings,
'sugar': (tmf.food.sugar or 0) * num_servings,
'sodium': (tmf.food.sodium or 0) * num_servings,
'calcium': (tmf.food.calcium or 0) * num_servings,
})
meal_details.append({