mirror of
				https://github.com/StefBuwalda/cal_counter.git
				synced 2025-10-30 03:10:00 +00:00 
			
		
		
		
	Refactor add meal flow and remove v2 routes
Migrated add_meal_v2 routes and templates to add_meal, renaming endpoints and updating references throughout the app. Removed legacy daily_log2 route and template, consolidating to daily_log. Moved macro_arr_to_json to utils.py for reuse. Updated navigation and redirect logic to use new routes. Improved item finding and barcode scanning UI.
This commit is contained in:
		| @@ -12,7 +12,7 @@ from application import db | ||||
| from forms import FoodItemForm | ||||
| from models import FoodItem, FoodLog | ||||
| from datetime import datetime | ||||
| from application.utils import login_required | ||||
| from application.utils import login_required, macro_arr_to_json | ||||
| from numpy import array | ||||
| from zoneinfo import ZoneInfo | ||||
|  | ||||
| @@ -22,58 +22,6 @@ user_bp = Blueprint( | ||||
|     template_folder="templates", | ||||
| ) | ||||
|  | ||||
|  | ||||
| def macro_arr_to_json(data: list[float]): | ||||
|     assert len(data) == 4 | ||||
|     cal = data[0] | ||||
|     pro = data[3] | ||||
|     car = data[2] | ||||
|     fat = data[1] | ||||
|     macros = [ | ||||
|         { | ||||
|             "name": "Calories", | ||||
|             "current": cal, | ||||
|             "target": 2000, | ||||
|             "bar_width": 100 - abs(cal / 20 - 100), | ||||
|             "bar_width_overflow": max(0, cal / 20 - 100), | ||||
|             "unit": " kcal", | ||||
|             "color": "bg-calories", | ||||
|             "overflow_color": "bg-calories-dark", | ||||
|         }, | ||||
|         { | ||||
|             "name": "Protein", | ||||
|             "current": pro, | ||||
|             "target": 150, | ||||
|             "bar_width": 100 - abs(pro / 1.5 - 100), | ||||
|             "bar_width_overflow": max(0, pro / 1.5 - 100), | ||||
|             "unit": "g", | ||||
|             "color": "bg-protein", | ||||
|             "overflow_color": "bg-protein-dark", | ||||
|         }, | ||||
|         { | ||||
|             "name": "Carbs", | ||||
|             "current": car, | ||||
|             "target": 250, | ||||
|             "bar_width": 100 - abs(car / 2.5 - 100), | ||||
|             "bar_width_overflow": max(0, car / 2.5 - 100), | ||||
|             "unit": "g", | ||||
|             "color": "bg-carbs", | ||||
|             "overflow_color": "bg-carbs-dark", | ||||
|         }, | ||||
|         { | ||||
|             "name": "Fat", | ||||
|             "current": fat, | ||||
|             "target": 70, | ||||
|             "bar_width": 100 - abs(fat / 0.7 - 100), | ||||
|             "bar_width_overflow": max(0, fat / 0.7 - 100), | ||||
|             "unit": "g", | ||||
|             "color": "bg-fat", | ||||
|             "overflow_color": "bg-fat-dark", | ||||
|         }, | ||||
|     ] | ||||
|     return macros | ||||
|  | ||||
|  | ||||
| user_bp.before_request(login_required) | ||||
|  | ||||
|  | ||||
| @@ -83,6 +31,35 @@ def dashboard(): | ||||
|     return render_template("dashboard.html", items=items) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/daily_log", methods=["GET"]) | ||||
| def daily_log(): | ||||
|     # 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: | ||||
|         macros += array(log.food_item.macros()) / 100 * log.amount | ||||
|     macros = macro_arr_to_json(macros.tolist()) | ||||
|  | ||||
|     # Render HTML | ||||
|     return render_template( | ||||
|         "daily_log.html", | ||||
|         macros=macros, | ||||
|         logs=logs_today, | ||||
|         today=today.strftime("%d/%m/%Y"), | ||||
|         min=min, | ||||
|     ) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/delete_food_item/<int:id>", methods=["POST"]) | ||||
| def delete_food_item(id: int): | ||||
|     item = FoodItem.query.get(id) | ||||
| @@ -119,35 +96,6 @@ def edit_food_item(id: int): | ||||
|     return redirect(url_for("user.dashboard")) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/daily_log2", methods=["GET"]) | ||||
| def daily_log2(): | ||||
|     # 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: | ||||
|         macros += array(log.food_item.macros()) / 100 * log.amount | ||||
|     macros = macro_arr_to_json(macros.tolist()) | ||||
|  | ||||
|     # Render HTML | ||||
|     return render_template( | ||||
|         "daily_log2.html", | ||||
|         macros=macros, | ||||
|         logs=logs_today, | ||||
|         today=today.strftime("%d/%m/%Y"), | ||||
|         min=min, | ||||
|     ) | ||||
|  | ||||
|  | ||||
| @user_bp.route("/remove_log/<int:id>", methods=["POST"]) | ||||
| def remove_log(id: int): | ||||
|     log = db.session.get(FoodLog, id) | ||||
| @@ -158,4 +106,4 @@ def remove_log(id: int): | ||||
|     # Delete log | ||||
|     db.session.delete(log) | ||||
|     db.session.commit() | ||||
|     return redirect(url_for("user.daily_log2")) | ||||
|     return redirect(url_for("user.daily_log")) | ||||
|   | ||||
| @@ -50,7 +50,7 @@ | ||||
|         </a> | ||||
| 
 | ||||
|         <!-- Center Button (highlighted) --> | ||||
|         <a id="set_link_date" href="{{ url_for('add_meal_v2.get_barcode') }}" | ||||
|         <a id="set_link_date" href="{{ url_for('add_meal.find_item') }}" | ||||
|             class="btn btn-success flex-fill mx-2 fw-bold rounded-pill"> | ||||
|             + Add Item | ||||
|         </a> | ||||
		Reference in New Issue
	
	Block a user