Add ability to remove food logs from daily log

Introduced a new route and backend logic to allow users to delete their own food log entries. Updated the daily_log.html template to display delete buttons for each log entry, improving user control over their daily food logs.
This commit is contained in:
2025-08-07 20:19:23 +02:00
parent dd5923a03e
commit b575180b0a
2 changed files with 141 additions and 29 deletions

View File

@@ -4,11 +4,12 @@ from flask import (
url_for, url_for,
render_template, render_template,
flash, flash,
abort,
) )
from flask_login import current_user from flask_login import current_user
from application import db from application import db
from forms import FoodItemForm from forms import FoodItemForm
from models import FoodItem from models import FoodItem, FoodLog
from datetime import datetime, timezone from datetime import datetime, timezone
user_bp = Blueprint( user_bp = Blueprint(
@@ -78,7 +79,19 @@ def daily_log():
logs = [[], [], [], []] logs = [[], [], [], []]
for log in logs_today: for log in logs_today:
logs[log.part_of_day].append(log) logs[log.part_of_day].append(log)
print(logs)
return render_template( return render_template(
"daily_log.html", date=(today.strftime("%d/%m/%y")), logs=logs "daily_log.html", date=(today.strftime("%d/%m/%y")), logs=logs
) )
@user_bp.route("/remove_log/<int:id>", methods=["POST"])
def remove_log(id: int):
log = db.session.get(FoodLog, id)
# Check if log exists and if user owns log
if log is None or log.user_id != current_user.id:
abort(404)
# Delete log
db.session.delete(log)
db.session.commit()
return redirect(url_for("user.daily_log"))

View File

@@ -19,55 +19,154 @@ Food Nutritional Info
<div class="p-3 border rounded"> <div class="p-3 border rounded">
<div class="text-center"> <div class="text-center">
<h2>Detailed Information</h2> <h2>Eaten today</h2>
<p>More content here...</p>
</div> </div>
<div class="p-3 mb-2 border rounded"> <div class="p-3 mb-2 border rounded">
<div class="d-flex justify-content-between align-items-center"> <!-- Header row (centered vertically for consistency) -->
<h4 class="mb-0">Breakfast</h4> <div class="row align-items-center mb-2">
<a href="{{url_for('add_meal.step1', meal_type=0)}}" class="btn btn-sm btn-primary">Add</a> <div class="col">
<h4 class="mb-0">Breakfast</h4>
</div>
<div class="col-auto text-end" style="min-width: 80px;">
<h4 class="mb-0">Amount</h4>
</div>
<div class="col-auto text-end" style="min-width: 80px;">
<a href="{{ url_for('add_meal.step1', meal_type=0) }}"
class="btn btn-sm btn-primary px-3 py-1">Add</a>
</div>
</div> </div>
<!-- Data rows -->
<div> <div>
{% for log in logs[0] %} {% for log in logs[0] %}
<p class="p-0 mb-0">{{log.food_item.name}} - {{"{:g}".format(log.amount)}}</p> <div class="row mb-2">
<div class="col text-wrap">
{{ log.food_item.name }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
{{ "{:g}".format(log.amount) }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
<form method="POST" action="{{url_for('user.remove_log', id=log.id)}}" class="d-inline">
<button type="submit" class="btn btn-sm btn-danger px-3 py-1"
title="Delete">&times;</button>
</form>
</div>
</div>
{% endfor %}
</div>
</div>
<div class="p-3 mb-2 border rounded">
<!-- Header row (centered vertically for consistency) -->
<div class="row align-items-center mb-2">
<div class="col">
<h4 class="mb-0">Lunch</h4>
</div>
<div class="col-auto text-end" style="min-width: 80px;">
<h4 class="mb-0">Amount</h4>
</div>
<div class="col-auto text-end" style="min-width: 80px;">
<a href="{{ url_for('add_meal.step1', meal_type=1) }}"
class="btn btn-sm btn-primary px-3 py-1">Add</a>
</div>
</div>
<!-- Data rows -->
<div>
{% for log in logs[1] %}
<div class="row mb-2">
<div class="col text-wrap">
{{ log.food_item.name }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
{{ "{:g}".format(log.amount) }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
<form method="POST" action="{{url_for('user.remove_log', id=log.id)}}" class="d-inline">
<button type="submit" class="btn btn-sm btn-danger px-3 py-1"
title="Delete">&times;</button>
</form>
</div>
</div>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
<div class="p-3 mb-2 border rounded"> <div class="p-3 mb-2 border rounded">
<div class="d-flex justify-content-between align-items-center"> <!-- Header row (centered vertically for consistency) -->
<h4 class="mb-0">Lunch</h4> <div class="row align-items-center mb-2">
<a href="{{url_for('add_meal.step1', meal_type=1)}}" class="btn btn-sm btn-primary">Add</a> <div class="col">
</div> <h4 class="mb-0">Dinner</h4>
<div> </div>
{% for log in logs[1] %} <div class="col-auto text-end" style="min-width: 80px;">
<p class="p-0 mb-0">{{log.food_item.name}} - {{"{:g}".format(log.amount)}}</p> <h4 class="mb-0">Amount</h4>
{% endfor %} </div>
</div> <div class="col-auto text-end" style="min-width: 80px;">
</div> <a href="{{ url_for('add_meal.step1', meal_type=2) }}"
<div class="p-3 mb-2 border rounded"> class="btn btn-sm btn-primary px-3 py-1">Add</a>
<div class="d-flex justify-content-between align-items-center"> </div>
<h4 class="mb-0">Dinner</h4>
<a href="{{url_for('add_meal.step1', meal_type=2)}}" class="btn btn-sm btn-primary">Add</a>
</div> </div>
<!-- Data rows -->
<div> <div>
{% for log in logs[2] %} {% for log in logs[2] %}
<p class="p-0 mb-0">{{log.food_item.name}} - {{"{:g}".format(log.amount)}}</p> <div class="row mb-2">
<div class="col text-wrap">
{{ log.food_item.name }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
{{ "{:g}".format(log.amount) }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
<form method="POST" action="{{url_for('user.remove_log', id=log.id)}}" class="d-inline">
<button type="submit" class="btn btn-sm btn-danger px-3 py-1"
title="Delete">&times;</button>
</form>
</div>
</div>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
<div class="p-3 mb-2 border rounded"> <div class="p-3 mb-2 border rounded">
<div class="d-flex justify-content-between align-items-center"> <!-- Header row (centered vertically for consistency) -->
<h4 class="mb-0">Snacks</h4> <div class="row align-items-center mb-2">
<a href="{{url_for('add_meal.step1', meal_type=3)}}" class="btn btn-sm btn-primary">Add</a> <div class="col">
<h4 class="mb-0">Snacks</h4>
</div>
<div class="col-auto text-end" style="min-width: 80px;">
<h4 class="mb-0">Amount</h4>
</div>
<div class="col-auto text-end" style="min-width: 80px;">
<a href="{{ url_for('add_meal.step1', meal_type=3) }}"
class="btn btn-sm btn-primary px-3 py-1">Add</a>
</div>
</div> </div>
<!-- Data rows -->
<div> <div>
{% for log in logs[3] %} {% for log in logs[3] %}
<p class="p-0 mb-0">{{log.food_item.name}} - {{"{:g}".format(log.amount)}}</p> <div class="row mb-2">
<div class="col text-wrap">
{{ log.food_item.name }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
{{ "{:g}".format(log.amount) }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
<form method="POST" action="{{url_for('user.remove_log', id=log.id)}}" class="d-inline">
<button type="submit" class="btn btn-sm btn-danger px-3 py-1"
title="Delete">&times;</button>
</form>
</div>
</div>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
</div> </div>
</div> {% endblock%}
{% endblock%}