diff --git a/application/add_meal_v2/routes.py b/application/add_meal_v2/routes.py index 8f10cd9..f11dbec 100644 --- a/application/add_meal_v2/routes.py +++ b/application/add_meal_v2/routes.py @@ -6,14 +6,13 @@ from flask import ( session, request, jsonify, - abort, ) from flask_login import current_user from forms import FoodItemForm, FoodLogForm from application import db from models import FoodItem, FoodLog from sqlalchemy import and_, or_ -from datetime import datetime, timedelta, timezone +from datetime import datetime from sqlalchemy.sql.elements import BinaryExpression from typing import cast @@ -25,31 +24,34 @@ bp = Blueprint( ) +def date_present(func): + def check_date(): + if "selected_date" not in session: + return redirect(url_for("user.daily_log2")) + + +def item_selected(func): + def check_item(): + if check_item(): + if "item_id" not in session: + return redirect(url_for("add_meal_v2.get_barcode")) + + @bp.before_request def login_required(): if not current_user.is_authenticated: return redirect(url_for("auth.login")) -@bp.route("/select_meal/", methods=["GET"]) -def step1(meal_type: int): - assert type(meal_type) is int - assert 0 <= meal_type <= 3 - session["meal_type"] = meal_type - return redirect(url_for("add_meal.step2")) - - +@date_present @bp.route("/get_barcode", methods=["GET"]) -def step2(): - return render_template("scan_barcode.html") +def get_barcode(): + return render_template("scan_barcode_v2.html") -@bp.route("/step3/", methods=["GET"]) -def step3(input: str): - # check if meal_type cookie is set - if "meal_type" not in session: - return redirect("/") - +@date_present +@bp.route("/add_existing/", methods=["GET"]) +def add_existing(input: str): # Check if input is a barcode if input.isdigit(): item = current_user.food_items.filter_by(barcode=input).first() @@ -59,15 +61,16 @@ def step3(input: str): if item is None: # Does not exist, add item - return redirect(url_for("add_meal.step3_alt1", input=input)) + return redirect(url_for("add_meal_v2.add_new", input=input)) # Track item to add and continue to next step session["item_id"] = item.id - return redirect(url_for("add_meal.step4")) + return redirect(url_for("add_meal_v2.step4")) -@bp.route("/step3_alt1/", methods=["GET"]) -def step3_alt1(input: str): +@date_present +@bp.route("/add_new/", methods=["GET"]) +def add_new(input: str): form = FoodItemForm() if input.isdigit(): @@ -77,8 +80,9 @@ def step3_alt1(input: str): return render_template("add_item.html", form=form) -@bp.route("/step3_alt1/", methods=["POST"]) -def step3_alt1_post(input: str): +@date_present +@bp.route("/add_new/", methods=["POST"]) +def add_new_post(input: str): form = FoodItemForm() if form.validate_on_submit(): @@ -122,25 +126,20 @@ def step3_alt1_post(input: str): print(f"Item exists: {item.barcode} {item.name}") # Item added or already present, return to step 3. - return redirect(url_for("add_meal.step3", input=input)) + return redirect(url_for("add_meal_v2.step3", input=input)) else: print("[DEBUG] Form Invalid") - return redirect(url_for("add_meal.step3_alt1", input=input)) + return redirect(url_for("add_meal_v2.step3_alt1", input=input)) +@date_present +@item_selected @bp.route("/step4", methods=["GET", "POST"]) def step4(): - if "item_id" not in session: - return redirect(url_for("add_meal.step2")) form = FoodLogForm() item = db.session.get(FoodItem, session["item_id"]) - - offset = session["offset"] - if offset is None or item is None: - abort(404) - - today = datetime.now(timezone.utc).date() - day = today + timedelta(days=offset) + if not item: + return redirect(url_for("add_meal_v2.get_barcode")) if form.validate_on_submit(): assert form.amount.data @@ -149,29 +148,21 @@ def step4(): food_item_id=item.id, user_id=current_user.id, amount=form.amount.data, - part_of_day=session["meal_type"], - date_=day, + part_of_day=0, + date_=datetime.strptime( + session["selected_date"], "%Y-%m-%d" + ).date(), ) ) db.session.commit() - session.pop("meal_type") session.pop("item_id") - return redirect(url_for("user.daily_log", offset=offset)) + session.pop("selected_date") + return redirect(url_for("user.daily_log2")) - match session["meal_type"]: - case 0: - tod = "Breakfast" - case 1: - tod = "Lunch" - case 2: - tod = "Dinner" - case 3: - tod = "Snack" - case _: - tod = "Unknown" - return render_template("step4.html", tod=tod, item=item, form=form) + return render_template("step4.html", tod="idk", item=item, form=form) +@date_present @bp.route("/query", methods=["GET"]) def query(): q = request.args.get("q", "").strip().lower() diff --git a/application/add_meal_v2/templates/scan_barcode.html b/application/add_meal_v2/templates/scan_barcode_v2.html similarity index 96% rename from application/add_meal_v2/templates/scan_barcode.html rename to application/add_meal_v2/templates/scan_barcode_v2.html index 1169b99..dd6655d 100644 --- a/application/add_meal_v2/templates/scan_barcode.html +++ b/application/add_meal_v2/templates/scan_barcode_v2.html @@ -85,7 +85,7 @@ goButton.addEventListener('click', () => { const value = searchBox.value.trim(); if (value) { - const baseURL = "{{url_for('add_meal.step3', input='!')}}"; + const baseURL = "{{url_for('add_meal_v2.add_existing', input='!')}}"; window.location.href = baseURL.replace("!", encodeURIComponent(value)); } }); @@ -121,7 +121,7 @@ if (result) { // Result found, this should post the barcode const codeText = result.getText(); - const baseURL = "{{url_for('add_meal.step3', input='!')}}"; + const baseURL = "{{url_for('add_meal_v2.add_existing', input='!')}}"; window.location.href = baseURL.replace("!", encodeURIComponent(codeText)); } }) diff --git a/application/user/routes.py b/application/user/routes.py index 395f892..9b61b52 100644 --- a/application/user/routes.py +++ b/application/user/routes.py @@ -13,8 +13,8 @@ from forms import FoodItemForm from models import FoodItem, FoodLog from datetime import datetime, timezone, timedelta from application.utils import login_required -from typing import cast from numpy import array +from zoneinfo import ZoneInfo user_bp = Blueprint( "user", @@ -155,14 +155,30 @@ def daily_log(offset: int = 0): @user_bp.route("/daily_log2", methods=["GET"]) def daily_log2(): - today = datetime.now(timezone.utc).date() - logs_today = current_user.food_logs.filter_by(date_=today).all() + # Get today's date according to user's timezone + today = datetime.now(ZoneInfo(current_user.timezone)).date() + + # Save date in session + session["selected_date"] = today.isoformat() + + # Get logs from today + logs_today = current_user.food_logs.filter_by( + date_=today.isoformat() + ).all() + + # calculate macros macros = array((0.0, 0.0, 0.0, 0.0)) for log in logs_today: - item = cast(FoodItem, log.food_item) - macros += array(item.macros()) / 100 * log.amount + macros += array(log.food_item.macros()) / 100 * log.amount macros = macro_arr_to_json(macros.tolist()) - return render_template("daily_log2.html", macros=macros, logs=logs_today) + + # Render HTML + return render_template( + "daily_log2.html", + macros=macros, + logs=logs_today, + today=today.strftime("%d/%m/%Y"), + ) @user_bp.route("/remove_log/", methods=["POST"]) diff --git a/application/user/templates/daily_log2.html b/application/user/templates/daily_log2.html index fed4194..e46c32e 100644 --- a/application/user/templates/daily_log2.html +++ b/application/user/templates/daily_log2.html @@ -6,7 +6,7 @@
-

Daily Calorie Dashboard

+

Daily Calorie Dashboard ({{ today }})

@@ -52,7 +52,7 @@ - + Add Item @@ -64,21 +64,4 @@
-{% endblock %} - -{% block scripts %} - {% endblock %} \ No newline at end of file