From 74ce42c5782a2c649cde5eb410fdc1e26ed4d439 Mon Sep 17 00:00:00 2001 From: Stef Date: Mon, 7 Jul 2025 13:23:50 +0200 Subject: [PATCH] Add food logging functionality for users Introduces a new FoodLogForm and a /log_food/ route to allow users to log food consumption. Updates the food item route to redirect to the log page, adds a log_food.html template, and updates the Bootstrap CDN version in base.html. --- application/templates/base.html | 2 +- application/user/routes.py | 25 +++++++++++++++++++++--- application/user/templates/log_food.html | 20 +++++++++++++++++++ forms.py | 5 +++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 application/user/templates/log_food.html diff --git a/application/templates/base.html b/application/templates/base.html index 36a4757..3cf8b6b 100644 --- a/application/templates/base.html +++ b/application/templates/base.html @@ -7,7 +7,7 @@ {% block title %}My Flask App{% endblock %} - + diff --git a/application/user/routes.py b/application/user/routes.py index 7e11c82..78e989d 100644 --- a/application/user/routes.py +++ b/application/user/routes.py @@ -1,8 +1,8 @@ from flask import Blueprint, redirect, url_for, render_template, flash from flask_login import current_user from application import db -from forms import FoodItemForm -from models import FoodItem +from forms import FoodItemForm, FoodLogForm +from models import FoodItem, FoodLog user_bp = Blueprint( "user", @@ -113,7 +113,7 @@ def edit_food_item(id: int): def food_item(barcode): food = FoodItem.query.filter_by(barcode=barcode).first() if food: - return render_template("food_item.html", item=food) + return redirect(url_for("user.log_food", item_id=food.id)) else: return render_template( "add_food_item.html", @@ -149,3 +149,22 @@ def add_food_item_manual(): db.session.commit() return redirect(url_for("user.dashboard")) return render_template("add_food_item_manual.html", form=form) + + +@user_bp.route("/log_food/", methods=["GET", "POST"]) +def log_food(item_id): + form = FoodLogForm() + if item_id is not None: + if db.session.get(FoodItem, item_id): + if form.validate_on_submit(): + assert form.amount.data is not None + db.session.add( + FoodLog( + item_id, + current_user.id, + form.amount.data, + ) + ) + db.session.commit() + return redirect(url_for("user.dashboard")) + return render_template("log_food.html", item_id=item_id, form=form) diff --git a/application/user/templates/log_food.html b/application/user/templates/log_food.html new file mode 100644 index 0000000..5ec13f2 --- /dev/null +++ b/application/user/templates/log_food.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} + +{% block content %} +
+ {{ form.hidden_tag() }} + +
+
+ {{item_id}} +
+
+ +
+ {{ form.amount.label(class="form-label") }} + {{ form.amount(class="form-control") }} +
+ + {{ form.submit(class="btn btn-primary") }} +
+{% endblock%} \ No newline at end of file diff --git a/forms.py b/forms.py index ba71960..7c2c657 100644 --- a/forms.py +++ b/forms.py @@ -25,3 +25,8 @@ class FoodItemForm(FlaskForm): fat = FloatField("fat per 100g", validators=[InputRequired()]) saturated_fat = FloatField("saturated_fat per 100g") submit = SubmitField("Add Item") + + +class FoodLogForm(FlaskForm): + amount = IntegerField("amount of food (g/ml)") + submit = SubmitField("Log Item")