mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-29 19:00:00 +00:00
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:
@@ -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/<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"))
|
||||
|
||||
Reference in New Issue
Block a user