From 700b351b0ee5331c805edacbfb678f49fb857051 Mon Sep 17 00:00:00 2001 From: Stef Date: Sun, 29 Jun 2025 13:54:31 +0200 Subject: [PATCH] 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. --- app.py | 4 +- application/templates/dashboard.html | 5 -- application/user/routes.py | 45 ++++++++++++++++- .../{ => user}/templates/add_food_item.html | 0 application/user/templates/dashboard.html | 5 +- .../user/templates/edit_food_item.html | 49 +++++++++++++++++++ seed.py | 2 +- 7 files changed, 101 insertions(+), 9 deletions(-) delete mode 100644 application/templates/dashboard.html rename application/{ => user}/templates/add_food_item.html (100%) create mode 100644 application/user/templates/edit_food_item.html diff --git a/app.py b/app.py index 9477ec1..6d0f516 100644 --- a/app.py +++ b/app.py @@ -32,6 +32,7 @@ app.register_blueprint(user_bp) def default_return(next_page: Optional[str] = None): + return redirect(url_for("user.dashboard")) if next_page: return redirect(next_page) if current_user.is_admin: @@ -42,7 +43,7 @@ def default_return(next_page: Optional[str] = None): @app.route("/") @login_required def index(): - return render_template("index.html") + return redirect(url_for("login")) @app.route("/login", methods=["GET", "POST"]) @@ -130,6 +131,7 @@ def add_food_item(): ) db.session.commit() print("[DEBUG] New item added") + return redirect(url_for("food_item", barcode=form.barcode.data)) else: print("[DEBUG] Invalid form") if form.barcode.data: diff --git a/application/templates/dashboard.html b/application/templates/dashboard.html deleted file mode 100644 index 9f7f311..0000000 --- a/application/templates/dashboard.html +++ /dev/null @@ -1,5 +0,0 @@ -{% extends "base.html" %} - -{% block content %} -Hallo -{% endblock%} \ No newline at end of file diff --git a/application/user/routes.py b/application/user/routes.py index 2659bfb..5befb6d 100644 --- a/application/user/routes.py +++ b/application/user/routes.py @@ -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 application import db +from forms import FoodItemForm user_bp = Blueprint( "user", @@ -18,3 +20,44 @@ def login_required(): def dashboard(): items = current_user.food_items.all() return render_template("dashboard.html", items=items) + + +@user_bp.route("/delete_food_item/", 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/", 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")) diff --git a/application/templates/add_food_item.html b/application/user/templates/add_food_item.html similarity index 100% rename from application/templates/add_food_item.html rename to application/user/templates/add_food_item.html diff --git a/application/user/templates/dashboard.html b/application/user/templates/dashboard.html index 2ee2348..98f043d 100644 --- a/application/user/templates/dashboard.html +++ b/application/user/templates/dashboard.html @@ -32,10 +32,13 @@ Food Nutritional Info {{ food.carbs_100g }} {{ food.protein_100g }} -
+
+ +
{% endfor %} diff --git a/application/user/templates/edit_food_item.html b/application/user/templates/edit_food_item.html new file mode 100644 index 0000000..04b77bc --- /dev/null +++ b/application/user/templates/edit_food_item.html @@ -0,0 +1,49 @@ +{% extends "base.html" %} + +{% block content %} +
+ {{ form.hidden_tag() }} + +
+ {{ form.barcode.label(class="form-label") }} + {{ form.barcode(class="form-control-plaintext", readonly=true) }} +
+ +
+ {{ form.name.label(class="form-label") }} + {{ form.name(class="form-control") }} +
+ +
+ {{ form.energy.label(class="form-label") }} + {{ form.energy(class="form-control") }} +
+ +
+ {{ form.protein.label(class="form-label") }} + {{ form.protein(class="form-control") }} +
+ +
+ {{ form.carbs.label(class="form-label") }} + {{ form.carbs(class="form-control") }} +
+ +
+ {{ form.sugar.label(class="form-label") }} + {{ form.sugar(class="form-control") }} +
+ +
+ {{ form.fat.label(class="form-label") }} + {{ form.fat(class="form-control") }} +
+ +
+ {{ form.saturated_fat.label(class="form-label") }} + {{ form.saturated_fat(class="form-control") }} +
+ + {{ form.submit(class="btn btn-primary") }} +
+{% endblock%} \ No newline at end of file diff --git a/seed.py b/seed.py index 5d57fc8..9cfb597 100644 --- a/seed.py +++ b/seed.py @@ -10,7 +10,7 @@ with app.app_context(): db.session.add( FoodItem( name="AH Matcha cookie", - owner_id=0, + owner_id=1, energy=430, fats=19, carbs=59,