sync - build workin

This commit is contained in:
2025-09-29 10:14:58 -07:00
parent c91b01665d
commit 19cbbb0626
3 changed files with 103 additions and 24 deletions

77
main.py
View File

@@ -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}
@@ -1762,7 +1767,75 @@ async def detailed(request: Request, person: str = "Sarah", plan_date: str = Non
template_nutrition['fat_pct'] = round((template_nutrition['fat'] * 9 / total_cals) * 100, 1)
template_nutrition['net_carbs'] = template_nutrition['carbs'] - template_nutrition['fiber']
return templates
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)
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)

View File

@@ -38,7 +38,7 @@
</div>
</div>
<div class="col-md-6 text-end">
<h4 class="mb-0">{{ selected_day }}</h4>
<h4 class="mb-0">{{ title }}</h4>
<small class="text-muted">Detailed nutritional breakdown</small>
</div>
</div>
@@ -169,19 +169,19 @@
{% for meal_food in meal_detail.foods %}
<tr>
<td>
<div class="food-name">{{ meal_food.food.name }}</div>
<div class="food-name">{{ meal_food.name }}</div>
</td>
<td>
<div class="serving-info">
{{ meal_food.quantity }} × {{ meal_food.food.serving_size }}{{ meal_food.food.serving_unit }}
{{ meal_food.quantity }} × {{ meal_food.serving_size }}{{ meal_food.serving_unit }}
</div>
</td>
<td class="nutrient-value">{{ "%.0f"|format(meal_food.food.calories * meal_food.quantity) }}</td>
<td class="nutrient-value">{{ "%.1f"|format(meal_food.food.protein * meal_food.quantity) }}g</td>
<td class="nutrient-value">{{ "%.1f"|format(meal_food.food.carbs * meal_food.quantity) }}g</td>
<td class="nutrient-value">{{ "%.1f"|format(meal_food.food.fat * meal_food.quantity) }}g</td>
<td class="nutrient-value">{{ "%.1f"|format((meal_food.food.fiber or 0) * meal_food.quantity) }}g</td>
<td class="nutrient-value">{{ "%.0f"|format((meal_food.food.sodium or 0) * meal_food.quantity) }}mg</td>
<td class="nutrient-value">{{ "%.0f"|format(meal_food.calories) }}</td>
<td class="nutrient-value">{{ "%.1f"|format(meal_food.protein) }}g</td>
<td class="nutrient-value">{{ "%.1f"|format(meal_food.carbs) }}g</td>
<td class="nutrient-value">{{ "%.1f"|format(meal_food.fat) }}g</td>
<td class="nutrient-value">{{ "%.1f"|format(meal_food.fiber or 0) }}g</td>
<td class="nutrient-value">{{ "%.0f"|format(meal_food.sodium or 0) }}mg</td>
</tr>
{% endfor %}
<!-- Meal Totals Row -->
@@ -231,6 +231,7 @@
{% endfor %}
<!-- Day Totals -->
{% if day_totals and day_totals.calories is defined and day_totals.calories > 0 %}
<div class="day-totals">
<h5 class="mb-3 text-center">
<i class="bi bi-calendar-check"></i> Daily Totals - {{ "%.0f"|format(day_totals.calories) }} Total Calories
@@ -281,14 +282,19 @@
</small>
</div>
</div>
{% endif %}
{% if not meal_details %}
<div class="alert alert-info">
{% if error %}
<div class="alert alert-danger mt-3">
<i class="bi bi-exclamation-triangle-fill"></i> <strong>Error:</strong> {{ error }}
</div>
{% elif not meal_details %}
<div class="alert alert-info mt-3">
<i class="bi bi-info-circle"></i>
{% 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 %}
</div>
{% endif %}

View File

@@ -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