mirror of
				https://github.com/StefBuwalda/cal_counter.git
				synced 2025-10-30 03:10:00 +00:00 
			
		
		
		
	Refactor meal addition flow and improve date handling
Refactored add_meal_v2 routes to simplify and clarify the meal addition process, including renaming endpoints and templates, and introducing decorators for date and item selection. Updated daily_log2 to use user's timezone and display the selected date. Adjusted templates and barcode scan logic to match new routes and improved user experience.
This commit is contained in:
		| @@ -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/<int:id>", methods=["POST"]) | ||||
|   | ||||
| @@ -6,7 +6,7 @@ | ||||
| <link rel="stylesheet" href="{{ url_for('static', filename='css/macros.css') }}"> | ||||
|  | ||||
| <div class="container my-4"> | ||||
|     <h2 class="mb-3">Daily Calorie Dashboard</h2> | ||||
|     <h2 class="mb-3">Daily Calorie Dashboard ({{ today }})</h2> | ||||
|  | ||||
|     <!-- Macro Summary --> | ||||
|     <div class="card p-3 mb-3"> | ||||
| @@ -52,7 +52,7 @@ | ||||
|         </a> | ||||
|  | ||||
|         <!-- Center Button (highlighted) --> | ||||
|         <a id="set_link_date" href="{{ url_for('add_meal.step1', meal_type=0) }}" | ||||
|         <a id="set_link_date" href="{{ url_for('add_meal_v2.get_barcode', meal_type=0) }}" | ||||
|             class="btn btn-success flex-fill mx-2 fw-bold rounded-pill"> | ||||
|             + Add Item | ||||
|         </a> | ||||
| @@ -64,21 +64,4 @@ | ||||
|     </div> | ||||
| </div> | ||||
|  | ||||
| {% endblock %} | ||||
|  | ||||
| {% block scripts %} | ||||
| <script> | ||||
|     function formatToday() { | ||||
|         const today = new Date(); | ||||
|         const yyyy = today.getFullYear(); | ||||
|         const mm = String(today.getMonth() + 1).padStart(2, '0'); | ||||
|         const dd = String(today.getDate()).padStart(2, '0'); | ||||
|         return `${yyyy}-${mm}-${dd}`; | ||||
|     } | ||||
|  | ||||
|     const link = document.getElementById('set_link_date'); | ||||
|     const todayString = formatToday(); | ||||
|     // Set href on page load | ||||
|     link.href = `/select_date?date=${todayString}`; | ||||
| </script> | ||||
| {% endblock %} | ||||
		Reference in New Issue
	
	Block a user