from flask import ( Blueprint, redirect, url_for, render_template, flash, ) from flask_login import current_user from application import db from forms import FoodItemForm from models import FoodItem from datetime import datetime, timezone user_bp = Blueprint( "user", __name__, template_folder="templates", ) @user_bp.before_request def login_required(): if not current_user.is_authenticated: return redirect(url_for("login")) @user_bp.route("/dashboard", methods=["GET"]) 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(id: int): item = FoodItem.query.get(id) if item: if item.owner_id == current_user.id: db.session.delete(item) db.session.commit() else: flash("You do not own this food item!") else: flash("This food item does not exist") return redirect(url_for("user.dashboard")) @user_bp.route("/edit_food_item/", methods=["GET", "POST"]) def edit_food_item(id: int): item = FoodItem.query.get(id) if item: if item.owner_id == current_user.id: form = FoodItemForm() if form.validate_on_submit(): item.updateFromForm(form=form) db.session.commit() return redirect(url_for("user.dashboard")) form.barcode.data = item.barcode form.name.data = item.name form.energy.data = item.energy_100 form.protein.data = item.protein_100 form.carbs.data = item.carbs_100 form.sugar.data = item.sugar_100 form.fat.data = item.fat_100 form.saturated_fat.data = item.saturated_fat_100 return render_template("edit_food_item.html", form=form) return redirect(url_for("user.dashboard")) @user_bp.route("/overview", methods=["GET"]) def overview(): return render_template("overview.html") @user_bp.route("/", methods=["GET"]) def daily_log(): today = datetime.now(timezone.utc).date() logs_today = current_user.food_logs.filter_by(date_=today).all() logs = [[], [], [], []] for log in logs_today: logs[log.part_of_day].append(log) print(logs) return render_template( "daily_log.html", date=(today.strftime("%d/%m/%y")), logs=logs )