Add edit and delete functionality for food items

Implemented routes and templates to allow users to edit and delete their food items from the dashboard. Updated dashboard template to include Edit and Delete buttons. Refactored template locations and removed unused dashboard.html. Fixed redirect logic in app.py and updated seed data.
This commit is contained in:
2025-06-29 13:54:31 +02:00
parent 970d102831
commit 700b351b0e
7 changed files with 101 additions and 9 deletions

4
app.py
View File

@@ -32,6 +32,7 @@ app.register_blueprint(user_bp)
def default_return(next_page: Optional[str] = None): def default_return(next_page: Optional[str] = None):
return redirect(url_for("user.dashboard"))
if next_page: if next_page:
return redirect(next_page) return redirect(next_page)
if current_user.is_admin: if current_user.is_admin:
@@ -42,7 +43,7 @@ def default_return(next_page: Optional[str] = None):
@app.route("/") @app.route("/")
@login_required @login_required
def index(): def index():
return render_template("index.html") return redirect(url_for("login"))
@app.route("/login", methods=["GET", "POST"]) @app.route("/login", methods=["GET", "POST"])
@@ -130,6 +131,7 @@ def add_food_item():
) )
db.session.commit() db.session.commit()
print("[DEBUG] New item added") print("[DEBUG] New item added")
return redirect(url_for("food_item", barcode=form.barcode.data))
else: else:
print("[DEBUG] Invalid form") print("[DEBUG] Invalid form")
if form.barcode.data: if form.barcode.data:

View File

@@ -1,5 +0,0 @@
{% extends "base.html" %}
{% block content %}
Hallo
{% endblock%}

View File

@@ -1,5 +1,7 @@
from flask import Blueprint, redirect, url_for, render_template from flask import Blueprint, redirect, url_for, render_template, flash
from flask_login import current_user from flask_login import current_user
from application import db
from forms import FoodItemForm
user_bp = Blueprint( user_bp = Blueprint(
"user", "user",
@@ -18,3 +20,44 @@ def login_required():
def dashboard(): def dashboard():
items = current_user.food_items.all() items = current_user.food_items.all()
return render_template("dashboard.html", items=items) return render_template("dashboard.html", items=items)
@user_bp.route("/delete_food_item/<int:barcode>", methods=["POST"])
def delete_food_item(barcode: int):
item = current_user.food_items.filter_by(barcode=barcode).first()
if item:
db.session.delete(item)
db.session.commit()
else:
flash(f"You do not own a food item with barcode {barcode}")
return redirect(url_for("user.dashboard"))
def edit_helper(form: FoodItemForm, item) -> bool:
change = False
for item in form:
print(item)
return change
@user_bp.route("/edit_food_item/<int:barcode>", methods=["GET", "POST"])
def edit_food_item(barcode: int):
item = current_user.food_items.filter_by(barcode=barcode).first()
if item:
form = FoodItemForm()
if form.validate_on_submit():
edit_helper(form, item)
return redirect(url_for("user.dashboard"))
form = FoodItemForm(
barcode=item.barcode,
name=item.name,
energy=item.energy_100g,
protein=item.protein_100g,
carbs=item.carbs_100g,
sugar=item.sugar_100g,
fat=item.fats_100g,
saturated_fat=item.saturated_fats_100g,
)
return render_template("edit_food_item.html", form=form)
else:
return redirect(url_for("user.dashboard"))

View File

@@ -32,10 +32,13 @@ Food Nutritional Info
<td class="bg-body-tertiary">{{ food.carbs_100g }}</td> <td class="bg-body-tertiary">{{ food.carbs_100g }}</td>
<td class="bg-body-tertiary">{{ food.protein_100g }}</td> <td class="bg-body-tertiary">{{ food.protein_100g }}</td>
<td class="bg-body-tertiary"> <td class="bg-body-tertiary">
<form method="POST" action="{{ url_for('admin.delete_food', id=food.id) }}" <form method="POST" action="{{ url_for('user.delete_food_item', barcode=food.barcode) }}"
onsubmit="return confirm('Are you sure you want to delete this item?');"> onsubmit="return confirm('Are you sure you want to delete this item?');">
<button type="submit" class="btn btn-danger btn-sm">Delete</button> <button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form> </form>
<form method="GET" action="{{ url_for('user.edit_food_item', barcode=food.barcode) }}">
<button type="submit" class="btn btn-warning btn-sm">Edit</button>
</form>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@@ -0,0 +1,49 @@
{% extends "base.html" %}
{% block content %}
<form method="POST">
{{ form.hidden_tag() }}
<div class="mb-3">
{{ form.barcode.label(class="form-label") }}
{{ form.barcode(class="form-control-plaintext", readonly=true) }}
</div>
<div class="mb-3">
{{ form.name.label(class="form-label") }}
{{ form.name(class="form-control") }}
</div>
<div class="mb-3">
{{ form.energy.label(class="form-label") }}
{{ form.energy(class="form-control") }}
</div>
<div class="mb-3">
{{ form.protein.label(class="form-label") }}
{{ form.protein(class="form-control") }}
</div>
<div class="mb-3">
{{ form.carbs.label(class="form-label") }}
{{ form.carbs(class="form-control") }}
</div>
<div class="mb-3">
{{ form.sugar.label(class="form-label") }}
{{ form.sugar(class="form-control") }}
</div>
<div class="mb-3">
{{ form.fat.label(class="form-label") }}
{{ form.fat(class="form-control") }}
</div>
<div class="mb-3">
{{ form.saturated_fat.label(class="form-label") }}
{{ form.saturated_fat(class="form-control") }}
</div>
{{ form.submit(class="btn btn-primary") }}
</form>
{% endblock%}

View File

@@ -10,7 +10,7 @@ with app.app_context():
db.session.add( db.session.add(
FoodItem( FoodItem(
name="AH Matcha cookie", name="AH Matcha cookie",
owner_id=0, owner_id=1,
energy=430, energy=430,
fats=19, fats=19,
carbs=59, carbs=59,