mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-29 19:00:00 +00:00
Add food logging functionality for users
Introduces a new FoodLogForm and a /log_food/<item_id> 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.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
<title>{% block title %}My Flask App{% endblock %}</title>
|
||||
|
||||
<!-- Bootstrap 5 CDN -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
<body class="bg-body-secondary">
|
||||
|
||||
@@ -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/<int:item_id>", 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)
|
||||
|
||||
20
application/user/templates/log_food.html
Normal file
20
application/user/templates/log_food.html
Normal file
@@ -0,0 +1,20 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<form method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
|
||||
<div class="mb-3">
|
||||
<div class="form-control-plaintext">
|
||||
{{item_id}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.amount.label(class="form-label") }}
|
||||
{{ form.amount(class="form-control") }}
|
||||
</div>
|
||||
|
||||
{{ form.submit(class="btn btn-primary") }}
|
||||
</form>
|
||||
{% endblock%}
|
||||
5
forms.py
5
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")
|
||||
|
||||
Reference in New Issue
Block a user