Move food item routes to user blueprint

Refactored food item related routes from app.py to application/user/routes.py under the user blueprint. Updated template and JS references to use the blueprint route names, improving code organization and modularity.
This commit is contained in:
2025-06-29 17:52:03 +02:00
parent a6f6cdf346
commit 1b79925b49
4 changed files with 54 additions and 66 deletions

View File

@@ -49,6 +49,42 @@ fields = [
]
@user_bp.route("/add_food_item", methods=["POST"])
def add_food_item():
form = FoodItemForm()
if form.validate_on_submit():
print("[DEBUG] Valid form")
if FoodItem.query.filter_by(barcode=form.barcode.data).first() is None:
assert form.name.data is not None
assert form.energy.data is not None
assert form.protein.data is not None
assert form.carbs.data is not None
assert form.fat.data is not None
db.session.add(
FoodItem(
name=form.name.data,
owner_id=current_user.id,
energy=form.energy.data,
protein=form.protein.data,
carbs=form.carbs.data,
fat=form.fat.data,
barcode=form.barcode.data,
saturated_fat=form.saturated_fat.data,
sugar=form.sugar.data,
)
)
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:
return redirect(url_for("food_item", barcode=form.barcode.data))
else:
return redirect(url_for("scan"))
@user_bp.route("/edit_food_item/<int:id>", methods=["GET", "POST"])
def edit_food_item(id: int):
item = FoodItem.query.get(id)
@@ -71,6 +107,19 @@ def edit_food_item(id: int):
return redirect(url_for("user.dashboard"))
@user_bp.route("/food_item/<int:barcode>", methods=["GET"])
def food_item(barcode):
food = FoodItem.query.filter_by(barcode=barcode).first()
if food:
return render_template("food_item.html", item=food)
else:
return render_template(
"add_food_item.html",
barcode=barcode,
form=FoodItemForm(barcode=barcode),
)
@user_bp.route("/add_food_item_manual", methods=["GET", "POST"])
def add_food_item_manual():
form = FoodItemForm()

View File

@@ -1,7 +1,7 @@
{% extends "base.html" %}
{% block content %}
<form method="POST" action="{{ url_for('add_food_item') }}">
<form method="POST" action="{{ url_for('user.add_food_item') }}">
{{ form.hidden_tag() }}
<div class="mb-3">