From 19cbbb0626cf9b5593e4f4a56d6ff32850b07464 Mon Sep 17 00:00:00 2001 From: sstent Date: Mon, 29 Sep 2025 10:14:58 -0700 Subject: [PATCH] sync - build workin --- main.py | 77 +++++++++++++++++++++++++++++++++++++++-- templates/detailed.html | 34 ++++++++++-------- tests/test_detailed.py | 16 ++++----- 3 files changed, 103 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 8dabd4d..fbf7750 100644 --- a/main.py +++ b/main.py @@ -1726,17 +1726,22 @@ async def remove_from_plan(plan_id: int, db: Session = Depends(get_db)): @app.get("/detailed", response_class=HTMLResponse) async def detailed(request: Request, person: str = "Sarah", plan_date: str = None, template_id: int = None, db: Session = Depends(get_db)): from datetime import datetime + logging.info(f"DEBUG: Detailed page requested with person={person}, plan_date={plan_date}, template_id={template_id}") if template_id: # Show template details + logging.info(f"DEBUG: Loading template with id: {template_id}") template = db.query(Template).filter(Template.id == template_id).first() if not template: + logging.error(f"DEBUG: Template with id {template_id} not found") return templates.TemplateResponse("detailed.html", { "request": request, "title": "Template Not Found", - "error": "Template not found" + "error": "Template not found", + "day_totals": {} }) template_meals = db.query(TemplateMeal).filter(TemplateMeal.template_id == template_id).all() + logging.info(f"DEBUG: Found {len(template_meals)} meals for template id {template_id}") # Calculate template nutrition template_nutrition = {'calories': 0, 'protein': 0, 'carbs': 0, 'fat': 0, 'fiber': 0, 'sugar': 0, 'sodium': 0, 'calcium': 0} @@ -1761,8 +1766,76 @@ async def detailed(request: Request, person: str = "Sarah", plan_date: str = Non template_nutrition['carbs_pct'] = round((template_nutrition['carbs'] * 4 / total_cals) * 100, 1) template_nutrition['fat_pct'] = round((template_nutrition['fat'] * 9 / total_cals) * 100, 1) template_nutrition['net_carbs'] = template_nutrition['carbs'] - template_nutrition['fiber'] + + context = { + "request": request, + "title": f"{template.name} Template", + "meal_details": meal_details, + "day_totals": template_nutrition, + "person": person + } + logging.info(f"DEBUG: Rendering template details with context: {context}") + return templates.TemplateResponse("detailed.html", context) - return templates + if plan_date: + # Show plan details for a specific date + logging.info(f"DEBUG: Loading plan for date: {plan_date}") + try: + plan_date_obj = datetime.fromisoformat(plan_date).date() + except ValueError: + logging.error(f"DEBUG: Invalid date format for plan_date: {plan_date}") + return templates.TemplateResponse("detailed.html", { + "request": request, "title": "Invalid Date", + "error": "Invalid date format. Please use YYYY-MM-DD.", + "day_totals": {} + }) + + plans = db.query(Plan).filter(Plan.person == person, Plan.date == plan_date_obj).all() + logging.info(f"DEBUG: Found {len(plans)} plans for {person} on {plan_date_obj}") + + day_totals = calculate_day_nutrition(plans, db) + + meal_details = [] + for plan in plans: + meal_nutrition = calculate_meal_nutrition(plan.meal, db) + + foods = [] + for mf in plan.meal.meal_foods: + foods.append({ + 'name': mf.food.name, + 'quantity': mf.quantity, + 'serving_size': mf.food.serving_size, + 'serving_unit': mf.food.serving_unit, + 'calories': mf.food.calories * mf.quantity, + 'protein': mf.food.protein * mf.quantity, + 'carbs': mf.food.carbs * mf.quantity, + 'fat': mf.food.fat * mf.quantity, + }) + + meal_details.append({ + 'plan': plan, + 'nutrition': meal_nutrition, + 'foods': foods + }) + + context = { + "request": request, + "title": f"{person}'s Detailed Plan for {plan_date_obj.strftime('%B %d, %Y')}", + "meal_details": meal_details, + "day_totals": day_totals, + "person": person, + "plan_date": plan_date_obj + } + logging.info(f"DEBUG: Rendering plan details with context: {context}") + return templates.TemplateResponse("detailed.html", context) + + # If neither plan_date nor template_id is provided, return an error + logging.error("DEBUG: Neither plan_date nor template_id were provided") + return templates.TemplateResponse("detailed.html", { + "request": request, "title": "Error", + "error": "Please provide either a plan date or a template ID.", + "day_totals": {} + }) # Tracker tab - Main page @app.get("/tracker", response_class=HTMLResponse) diff --git a/templates/detailed.html b/templates/detailed.html index e5d72d3..2588753 100644 --- a/templates/detailed.html +++ b/templates/detailed.html @@ -38,7 +38,7 @@
-

{{ selected_day }}

+

{{ title }}

Detailed nutritional breakdown
@@ -169,19 +169,19 @@ {% for meal_food in meal_detail.foods %} -
{{ meal_food.food.name }}
+
{{ meal_food.name }}
- {{ meal_food.quantity }} × {{ meal_food.food.serving_size }}{{ meal_food.food.serving_unit }} + {{ meal_food.quantity }} × {{ meal_food.serving_size }}{{ meal_food.serving_unit }}
- {{ "%.0f"|format(meal_food.food.calories * meal_food.quantity) }} - {{ "%.1f"|format(meal_food.food.protein * meal_food.quantity) }}g - {{ "%.1f"|format(meal_food.food.carbs * meal_food.quantity) }}g - {{ "%.1f"|format(meal_food.food.fat * meal_food.quantity) }}g - {{ "%.1f"|format((meal_food.food.fiber or 0) * meal_food.quantity) }}g - {{ "%.0f"|format((meal_food.food.sodium or 0) * meal_food.quantity) }}mg + {{ "%.0f"|format(meal_food.calories) }} + {{ "%.1f"|format(meal_food.protein) }}g + {{ "%.1f"|format(meal_food.carbs) }}g + {{ "%.1f"|format(meal_food.fat) }}g + {{ "%.1f"|format(meal_food.fiber or 0) }}g + {{ "%.0f"|format(meal_food.sodium or 0) }}mg {% endfor %} @@ -231,6 +231,7 @@ {% endfor %} +{% if day_totals and day_totals.calories is defined and day_totals.calories > 0 %}
Daily Totals - {{ "%.0f"|format(day_totals.calories) }} Total Calories @@ -276,19 +277,24 @@
- Daily Macro Ratio: + Daily Macro Ratio: {{ day_totals.protein_pct or 0 }}% Protein : {{ day_totals.carbs_pct or 0 }}% Carbs : {{ day_totals.fat_pct or 0 }}% Fat
+{% endif %} -{% if not meal_details %} -
+{% if error %} +
+ Error: {{ error }} +
+{% elif not meal_details %} +
{% if view_mode == 'template' %} - No template selected. Please select a template to view. + This template has no meals. {% else %} - No meals planned for this date. Go to the Plan tab to add meals. + No meals planned for this day. {% endif %}
{% endif %} diff --git a/tests/test_detailed.py b/tests/test_detailed.py index 2cefc1e..0ad66f7 100644 --- a/tests/test_detailed.py +++ b/tests/test_detailed.py @@ -32,7 +32,7 @@ def client_fixture(session): def test_detailed_page_no_params(client): response = client.get("/detailed") assert response.status_code == 200 - assert "error" in response.text or "Template Not Found" in response.text # Based on the existing code, it returns an error template if neither plan_date nor template_id is provided, and template_id is None. + assert "Please provide either a plan date or a template ID." in response.text def test_detailed_page_with_plan_date(client, session): # Create mock data @@ -57,8 +57,8 @@ def test_detailed_page_with_plan_date(client, session): response = client.get(f"/detailed?person=Sarah&plan_date={test_date.isoformat()}") assert response.status_code == 200 - assert b"Sarah's Detailed Plan" in response.content - assert b"Fruit Snack" in response.content + assert "Sarah's Detailed Plan for" in response.text + assert "Fruit Snack" in response.text def test_detailed_page_with_template_id(client, session): # Create mock data @@ -87,17 +87,17 @@ def test_detailed_page_with_template_id(client, session): response = client.get(f"/detailed?template_id={template.id}") assert response.status_code == 200 - assert b"Morning Boost Template" in response.content - assert b"Banana Smoothie" in response.content + assert "Morning Boost Template" in response.text + assert "Banana Smoothie" in response.text def test_detailed_page_with_invalid_plan_date(client): invalid_date = date.today() + timedelta(days=100) # A date far in the future response = client.get(f"/detailed?person=Sarah&plan_date={invalid_date.isoformat()}") assert response.status_code == 200 - assert b"Sarah's Detailed Plan" in response.content - assert b"No meals planned for this day." in response.content # Assuming this message is displayed + assert "Sarah's Detailed Plan for" in response.text + assert "No meals planned for this day." in response.text def test_detailed_page_with_invalid_template_id(client): response = client.get(f"/detailed?template_id=99999") assert response.status_code == 200 - assert b"Template Not Found" in response.content + assert "Template Not Found" in response.text