From 70eef5b9a2303729811b5c1facc68d405cf3d884 Mon Sep 17 00:00:00 2001 From: Stef Date: Wed, 8 Oct 2025 19:08:44 +0200 Subject: [PATCH] Daily log now sums up amount per item in a day --- application/user/routes.py | 20 +++++++++++++++----- application/user/templates/daily_log.html | 8 ++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/application/user/routes.py b/application/user/routes.py index c4513b2..c50f7d2 100644 --- a/application/user/routes.py +++ b/application/user/routes.py @@ -15,6 +15,7 @@ from datetime import datetime from application.utils import login_required, macro_arr_to_json from numpy import array from zoneinfo import ZoneInfo +from sqlalchemy import func user_bp = Blueprint( "user", @@ -40,14 +41,23 @@ def daily_log(): session["selected_date"] = today.isoformat() # Get logs from today - logs_today = current_user.food_logs.filter_by( - date_=today.isoformat() - ).all() + logs_today = ( + current_user.food_logs.join( + FoodItem, FoodItem.id == FoodLog.food_item_id + ) + .filter(FoodLog.date_ == today) + .group_by(FoodItem.id) + .with_entities( + FoodItem, # the full object + func.sum(FoodLog.amount).label("total_amount"), + ) + .all() + ) # calculate macros macros = array((0.0, 0.0, 0.0, 0.0)) - for log in logs_today: - macros += array(log.food_item.macros()) / 100 * log.amount + for food_item, amount in logs_today: + macros += array(food_item.macros()) / 100 * amount macros = macro_arr_to_json(macros.tolist()) # Render HTML diff --git a/application/user/templates/daily_log.html b/application/user/templates/daily_log.html index 9fd6017..3d4774f 100644 --- a/application/user/templates/daily_log.html +++ b/application/user/templates/daily_log.html @@ -29,22 +29,22 @@
Items Eaten Today
- {% for log in logs %} + {% for food_item, amount in logs %}
- ({{ log.amount | int }}g) + ({{ amount | int }}g) - {{ log.food_item.name }} + {{ food_item.name }} - {{ (log.food_item.energy_100 * log.amount / 100) | int }} kcal + {{ (food_item.energy_100 * amount / 100) | int }} kcal
{% endfor %}