mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-30 03:10: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 +0,0 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
Hallo
|
||||
{% endblock%}
|
||||
@@ -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"))
|
||||
|
||||
@@ -32,10 +32,13 @@ Food Nutritional Info
|
||||
<td class="bg-body-tertiary">{{ food.carbs_100g }}</td>
|
||||
<td class="bg-body-tertiary">{{ food.protein_100g }}</td>
|
||||
<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?');">
|
||||
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
|
||||
</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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
49
application/user/templates/edit_food_item.html
Normal file
49
application/user/templates/edit_food_item.html
Normal 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%}
|
||||
Reference in New Issue
Block a user