From f7f6d235625f6104b4e3f4f0002a899d82ddc98f Mon Sep 17 00:00:00 2001 From: Stef Date: Thu, 14 Aug 2025 12:29:02 +0200 Subject: [PATCH] Removed old log for new log --- app.py | 4 +- application/add_meal/routes.py | 187 ---------------- application/add_meal/templates/add_item.html | 49 ---- .../add_meal/templates/scan_barcode.html | 134 ----------- application/add_meal/templates/step4.html | 23 -- application/add_meal_v2/routes.py | 4 +- application/user/routes.py | 5 +- application/user/templates/daily_log.html | 211 ------------------ application/user/templates/daily_log2.html | 8 +- application/utils.py | 2 +- migrations/versions/65eaeafb0904_.py | 32 +++ models.py | 1 - 12 files changed, 42 insertions(+), 618 deletions(-) delete mode 100644 application/add_meal/routes.py delete mode 100644 application/add_meal/templates/add_item.html delete mode 100644 application/add_meal/templates/scan_barcode.html delete mode 100644 application/add_meal/templates/step4.html delete mode 100644 application/user/templates/daily_log.html create mode 100644 migrations/versions/65eaeafb0904_.py diff --git a/app.py b/app.py index 8342d29..6fcdea0 100644 --- a/app.py +++ b/app.py @@ -11,7 +11,6 @@ from models import User from application import db, app, login_manager from application.admin.routes import admin_bp from application.user.routes import user_bp -from application.add_meal.routes import bp as add_meal_bp from application.auth.routes import bp as auth_bp from application.add_meal_v2.routes import bp as add_meal_v2_bp from typing import Optional @@ -30,14 +29,13 @@ def load_user(user_id: int): # Register blueprints app.register_blueprint(admin_bp) app.register_blueprint(user_bp) -app.register_blueprint(add_meal_bp) app.register_blueprint(auth_bp) app.register_blueprint(add_meal_v2_bp) # Routes def default_return(next_page: Optional[str] = None): - return redirect(url_for("user.daily_log")) + return redirect(url_for("user.daily_log2")) if next_page: return redirect(next_page) if current_user.is_admin: diff --git a/application/add_meal/routes.py b/application/add_meal/routes.py deleted file mode 100644 index 7efdf0e..0000000 --- a/application/add_meal/routes.py +++ /dev/null @@ -1,187 +0,0 @@ -from flask import ( - Blueprint, - redirect, - url_for, - render_template, - 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 sqlalchemy.sql.elements import BinaryExpression -from typing import cast - -bp = Blueprint( - "add_meal", - __name__, - url_prefix="/add_meal", - template_folder="templates", -) - - -@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")) - - -@bp.route("/get_barcode", methods=["GET"]) -def step2(): - return render_template("scan_barcode.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("/") - - # Check if input is a barcode - if input.isdigit(): - item = current_user.food_items.filter_by(barcode=input).first() - - else: - item = current_user.food_items.filter_by(name=input).first() - - if item is None: - # Does not exist, add item - return redirect(url_for("add_meal.step3_alt1", input=input)) - - # Track item to add and continue to next step - session["item_id"] = item.id - return redirect(url_for("add_meal.step4")) - - -@bp.route("/step3_alt1/", methods=["GET"]) -def step3_alt1(input: str): - form = FoodItemForm() - - if input.isdigit(): - form.barcode.data = input - else: - form.name.data = input - return render_template("add_item.html", form=form) - - -@bp.route("/step3_alt1/", methods=["POST"]) -def step3_alt1_post(input: str): - form = FoodItemForm() - - if form.validate_on_submit(): - # Form has valid input - barcode = form.barcode.data - name = form.name.data - assert name - 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 - - # Check if name or barcode already exists - name_filter = cast(BinaryExpression, FoodItem.name == name) - barcode_filter = cast(BinaryExpression, FoodItem.barcode == barcode) - filter_exp = or_(name_filter, barcode_filter) - item = current_user.food_items.filter(filter_exp).first() - - if item is None: - # Item does not exist, add to DB - barcode = ( - barcode if barcode else None - ) # Turn empty strings into None - db.session.add( - FoodItem( - name=name, - owner_id=current_user.id, - energy=form.energy.data, - protein=form.protein.data, - carbs=form.carbs.data, - fat=form.fat.data, - barcode=barcode, - saturated_fat=form.saturated_fat.data, - sugar=form.sugar.data, - ) - ) - db.session.commit() - print("[DEBUG] New FoodItem Added") - input = barcode if barcode else name # update input - else: - 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)) - else: - print("[DEBUG] Form Invalid") - return redirect(url_for("add_meal.step3_alt1", input=input)) - - -@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 form.validate_on_submit(): - assert form.amount.data - db.session.add( - FoodLog( - food_item_id=item.id, - user_id=current_user.id, - amount=form.amount.data, - part_of_day=session["meal_type"], - date_=day, - ) - ) - db.session.commit() - session.pop("meal_type") - session.pop("item_id") - return redirect(url_for("user.daily_log", offset=offset)) - - 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) - - -@bp.route("/query", methods=["GET"]) -def query(): - q = request.args.get("q", "").strip().lower() - if not q: - return jsonify([]) - - words = q.split() - filters = [ - FoodItem.name.ilike(f"%{word}%") for word in words # type: ignore - ] - - results = current_user.food_items.filter(and_(*filters)).all() - return jsonify([item.name for item in results]) diff --git a/application/add_meal/templates/add_item.html b/application/add_meal/templates/add_item.html deleted file mode 100644 index f44b33d..0000000 --- a/application/add_meal/templates/add_item.html +++ /dev/null @@ -1,49 +0,0 @@ -{% extends "base.html" %} - -{% block content %} -
- {{ form.hidden_tag() }} - -
- {{ form.barcode.label(class="form-label") }} - {{ form.barcode(class="form-control-plaintext", readonly=true) }} -
- -
- {{ form.name.label(class="form-label") }} - {{ form.name(class="form-control") }} -
- -
- {{ form.energy.label(class="form-label") }} - {{ form.energy(class="form-control") }} -
- -
- {{ form.fat.label(class="form-label") }} - {{ form.fat(class="form-control") }} -
- -
- {{ form.saturated_fat.label(class="form-label") }} - {{ form.saturated_fat(class="form-control") }} -
- -
- {{ form.carbs.label(class="form-label") }} - {{ form.carbs(class="form-control") }} -
- -
- {{ form.sugar.label(class="form-label") }} - {{ form.sugar(class="form-control") }} -
- -
- {{ form.protein.label(class="form-label") }} - {{ form.protein(class="form-control") }} -
- - {{ form.submit(class="btn btn-primary") }} -
-{% endblock%} \ No newline at end of file diff --git a/application/add_meal/templates/scan_barcode.html b/application/add_meal/templates/scan_barcode.html deleted file mode 100644 index 1169b99..0000000 --- a/application/add_meal/templates/scan_barcode.html +++ /dev/null @@ -1,134 +0,0 @@ -{% extends "base.html" %} - -{% block content %} -
- -
-

Barcode Scanner

-

Use your camera to scan barcodes

-
- - -
- -
- - -
- - -
- - -
-
- -
- - -
- - -
    -
    -
    - - - -
    -
      -
    -
    -
    - - - - - - - - -{% endblock %} \ No newline at end of file diff --git a/application/add_meal/templates/step4.html b/application/add_meal/templates/step4.html deleted file mode 100644 index 85cd332..0000000 --- a/application/add_meal/templates/step4.html +++ /dev/null @@ -1,23 +0,0 @@ -{% extends "base.html" %} - -{% block content %} -

    {{ tod }}

    -

    {{ item.name }}

    - -
    - {{ form.hidden_tag() }} - -
    -
    - {{item_id}} -
    -
    - -
    - {{ form.amount.label(class="form-label") }} - {{ form.amount(class="form-control") }} -
    - - {{ form.submit(class="btn btn-primary") }} -
    -{% endblock%} \ No newline at end of file diff --git a/application/add_meal_v2/routes.py b/application/add_meal_v2/routes.py index f11dbec..1aef4a4 100644 --- a/application/add_meal_v2/routes.py +++ b/application/add_meal_v2/routes.py @@ -126,10 +126,10 @@ def add_new_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_v2.step3", input=input)) + return redirect(url_for("add_meal_v2.add_existing", input=input)) else: print("[DEBUG] Form Invalid") - return redirect(url_for("add_meal_v2.step3_alt1", input=input)) + return redirect(url_for("add_meal_v2.add_new", input=input)) @date_present diff --git a/application/user/routes.py b/application/user/routes.py index 9b61b52..d0380a6 100644 --- a/application/user/routes.py +++ b/application/user/routes.py @@ -178,6 +178,7 @@ def daily_log2(): macros=macros, logs=logs_today, today=today.strftime("%d/%m/%Y"), + min=min, ) @@ -191,6 +192,4 @@ def remove_log(id: int): # Delete log db.session.delete(log) db.session.commit() - if "offset" in session: - return redirect(url_for("user.daily_log", offset=session["offset"])) - return redirect(url_for("user.daily_log")) + return redirect(url_for("user.daily_log2")) diff --git a/application/user/templates/daily_log.html b/application/user/templates/daily_log.html deleted file mode 100644 index 0c57753..0000000 --- a/application/user/templates/daily_log.html +++ /dev/null @@ -1,211 +0,0 @@ -{% extends "base.html" %} - -{% block title %} -Food Nutritional Info -{% endblock %} - -{% block content %} - - -
    - -
    - -
    - -
    - - -
    -

    Daily Overview ({{date}})

    - - -
    -
    - Calories: {{ '%.0f' % calories }} kcal -
    -
    - Protein: {{ '%.1f' % protein }} g -
    -
    - - -
    -
    - Carbs: {{ '%.1f' % carbs }} g -
    -
    - Fat: {{ '%.1f' % fat }} g -
    -
    -
    - - -
    - -
    -
    - - - - -
    -
    -

    Eaten today

    -
    -
    - -
    -
    -

    Breakfast

    -
    -
    -

    Amount

    -
    -
    - Add -
    -
    - - -
    - {% for log in logs[0] %} -
    -
    - {{ log.food_item.name }} -
    -
    - {{ "{:g}".format(log.amount) }} -
    -
    -
    - -
    -
    -
    - {% endfor %} -
    -
    - - - - -
    - -
    -
    -

    Lunch

    -
    -
    -

    Amount

    -
    -
    - Add -
    -
    - - -
    - {% for log in logs[1] %} -
    -
    - {{ log.food_item.name }} -
    -
    - {{ "{:g}".format(log.amount) }} -
    -
    -
    - -
    -
    -
    - {% endfor %} -
    -
    - -
    - -
    -
    -

    Dinner

    -
    -
    -

    Amount

    -
    -
    - Add -
    -
    - - -
    - {% for log in logs[2] %} -
    -
    - {{ log.food_item.name }} -
    -
    - {{ "{:g}".format(log.amount) }} -
    -
    -
    - -
    -
    -
    - {% endfor %} -
    -
    - -
    - -
    -
    -

    Snacks

    -
    -
    -

    Amount

    -
    -
    - Add -
    -
    - - -
    - {% for log in logs[3] %} -
    -
    - {{ log.food_item.name }} -
    -
    - {{ "{:g}".format(log.amount) }} -
    -
    -
    - -
    -
    -
    - {% endfor %} -
    -
    -
    - - {% endblock%} \ No newline at end of file diff --git a/application/user/templates/daily_log2.html b/application/user/templates/daily_log2.html index e46c32e..c3a7e51 100644 --- a/application/user/templates/daily_log2.html +++ b/application/user/templates/daily_log2.html @@ -17,10 +17,10 @@
    - {{ macro.current - macro.target }}{{ macro.unit }} + {{ (macro.current - macro.target) }}{{ macro.unit }}
    - {{ macro.current }}{{ macro.unit }} + {{ min(macro.current, macro.target) }}{{ macro.unit }}
    @@ -34,7 +34,7 @@ {% for log in logs %}
    ({{ log.amount }}g) {{ log.food_item.name }} - {{ log.food_item.energy_100 }} kcal + {{ log.food_item.energy_100 * log.amount / 100 }} kcal
    {% endfor %} @@ -52,7 +52,7 @@ - + Add Item diff --git a/application/utils.py b/application/utils.py index 74f14cb..fac23ae 100644 --- a/application/utils.py +++ b/application/utils.py @@ -14,7 +14,7 @@ def login_required(): def default_return(next_page: Optional[str] = None): - return redirect(url_for("user.daily_log")) + return redirect(url_for("user.daily_log2")) if next_page: return redirect(next_page) if current_user.is_admin: diff --git a/migrations/versions/65eaeafb0904_.py b/migrations/versions/65eaeafb0904_.py new file mode 100644 index 0000000..bb66806 --- /dev/null +++ b/migrations/versions/65eaeafb0904_.py @@ -0,0 +1,32 @@ +"""empty message + +Revision ID: 65eaeafb0904 +Revises: 9eb23abd5294 +Create Date: 2025-08-14 12:28:56.157288 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '65eaeafb0904' +down_revision = '9eb23abd5294' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('food_log', schema=None) as batch_op: + batch_op.drop_column('part_of_day') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('food_log', schema=None) as batch_op: + batch_op.add_column(sa.Column('part_of_day', sa.INTEGER(), nullable=False)) + + # ### end Alembic commands ### diff --git a/models.py b/models.py index 667b01d..7c6b27e 100644 --- a/models.py +++ b/models.py @@ -134,7 +134,6 @@ class FoodLog(db.Model): food_item_id = db.Column( db.Integer, db.ForeignKey("food_item.id"), nullable=False ) - part_of_day = db.Column(db.Integer, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) amount = db.Column(db.Float, nullable=False)