mirror of
https://github.com/sstent/foodplanner.git
synced 2025-12-06 08:01:47 +00:00
sync - build workin
This commit is contained in:
77
main.py
77
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)
|
@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)):
|
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
|
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:
|
if template_id:
|
||||||
# Show template details
|
# Show template details
|
||||||
|
logging.info(f"DEBUG: Loading template with id: {template_id}")
|
||||||
template = db.query(Template).filter(Template.id == template_id).first()
|
template = db.query(Template).filter(Template.id == template_id).first()
|
||||||
if not template:
|
if not template:
|
||||||
|
logging.error(f"DEBUG: Template with id {template_id} not found")
|
||||||
return templates.TemplateResponse("detailed.html", {
|
return templates.TemplateResponse("detailed.html", {
|
||||||
"request": request, "title": "Template Not Found",
|
"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()
|
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
|
# Calculate template nutrition
|
||||||
template_nutrition = {'calories': 0, 'protein': 0, 'carbs': 0, 'fat': 0, 'fiber': 0, 'sugar': 0, 'sodium': 0, 'calcium': 0}
|
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['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['fat_pct'] = round((template_nutrition['fat'] * 9 / total_cals) * 100, 1)
|
||||||
template_nutrition['net_carbs'] = template_nutrition['carbs'] - template_nutrition['fiber']
|
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
|
# Tracker tab - Main page
|
||||||
@app.get("/tracker", response_class=HTMLResponse)
|
@app.get("/tracker", response_class=HTMLResponse)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-6 text-end">
|
<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>
|
<small class="text-muted">Detailed nutritional breakdown</small>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -169,19 +169,19 @@
|
|||||||
{% for meal_food in meal_detail.foods %}
|
{% for meal_food in meal_detail.foods %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<div class="food-name">{{ meal_food.food.name }}</div>
|
<div class="food-name">{{ meal_food.name }}</div>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<div class="serving-info">
|
<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>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td class="nutrient-value">{{ "%.0f"|format(meal_food.food.calories * meal_food.quantity) }}</td>
|
<td class="nutrient-value">{{ "%.0f"|format(meal_food.calories) }}</td>
|
||||||
<td class="nutrient-value">{{ "%.1f"|format(meal_food.food.protein * meal_food.quantity) }}g</td>
|
<td class="nutrient-value">{{ "%.1f"|format(meal_food.protein) }}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.carbs) }}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.fat) }}g</td>
|
||||||
<td class="nutrient-value">{{ "%.1f"|format((meal_food.food.fiber or 0) * meal_food.quantity) }}g</td>
|
<td class="nutrient-value">{{ "%.1f"|format(meal_food.fiber or 0) }}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.sodium or 0) }}mg</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<!-- Meal Totals Row -->
|
<!-- Meal Totals Row -->
|
||||||
@@ -231,6 +231,7 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
<!-- Day Totals -->
|
<!-- Day Totals -->
|
||||||
|
{% if day_totals and day_totals.calories is defined and day_totals.calories > 0 %}
|
||||||
<div class="day-totals">
|
<div class="day-totals">
|
||||||
<h5 class="mb-3 text-center">
|
<h5 class="mb-3 text-center">
|
||||||
<i class="bi bi-calendar-check"></i> Daily Totals - {{ "%.0f"|format(day_totals.calories) }} Total Calories
|
<i class="bi bi-calendar-check"></i> Daily Totals - {{ "%.0f"|format(day_totals.calories) }} Total Calories
|
||||||
@@ -276,19 +277,24 @@
|
|||||||
|
|
||||||
<div class="text-center mt-2">
|
<div class="text-center mt-2">
|
||||||
<small>
|
<small>
|
||||||
<strong>Daily Macro Ratio:</strong>
|
<strong>Daily Macro Ratio:</strong>
|
||||||
{{ day_totals.protein_pct or 0 }}% Protein : {{ day_totals.carbs_pct or 0 }}% Carbs : {{ day_totals.fat_pct or 0 }}% Fat
|
{{ day_totals.protein_pct or 0 }}% Protein : {{ day_totals.carbs_pct or 0 }}% Carbs : {{ day_totals.fat_pct or 0 }}% Fat
|
||||||
</small>
|
</small>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% if not meal_details %}
|
{% if error %}
|
||||||
<div class="alert alert-info">
|
<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>
|
<i class="bi bi-info-circle"></i>
|
||||||
{% if view_mode == 'template' %}
|
{% if view_mode == 'template' %}
|
||||||
No template selected. Please select a template to view.
|
This template has no meals.
|
||||||
{% else %}
|
{% else %}
|
||||||
No meals planned for this date. Go to the Plan tab to add meals.
|
No meals planned for this day.
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ def client_fixture(session):
|
|||||||
def test_detailed_page_no_params(client):
|
def test_detailed_page_no_params(client):
|
||||||
response = client.get("/detailed")
|
response = client.get("/detailed")
|
||||||
assert response.status_code == 200
|
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):
|
def test_detailed_page_with_plan_date(client, session):
|
||||||
# Create mock data
|
# 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()}")
|
response = client.get(f"/detailed?person=Sarah&plan_date={test_date.isoformat()}")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b"Sarah's Detailed Plan" in response.content
|
assert "Sarah's Detailed Plan for" in response.text
|
||||||
assert b"Fruit Snack" in response.content
|
assert "Fruit Snack" in response.text
|
||||||
|
|
||||||
def test_detailed_page_with_template_id(client, session):
|
def test_detailed_page_with_template_id(client, session):
|
||||||
# Create mock data
|
# 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}")
|
response = client.get(f"/detailed?template_id={template.id}")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b"Morning Boost Template" in response.content
|
assert "Morning Boost Template" in response.text
|
||||||
assert b"Banana Smoothie" in response.content
|
assert "Banana Smoothie" in response.text
|
||||||
|
|
||||||
def test_detailed_page_with_invalid_plan_date(client):
|
def test_detailed_page_with_invalid_plan_date(client):
|
||||||
invalid_date = date.today() + timedelta(days=100) # A date far in the future
|
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()}")
|
response = client.get(f"/detailed?person=Sarah&plan_date={invalid_date.isoformat()}")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b"Sarah's Detailed Plan" in response.content
|
assert "Sarah's Detailed Plan for" in response.text
|
||||||
assert b"No meals planned for this day." in response.content # Assuming this message is displayed
|
assert "No meals planned for this day." in response.text
|
||||||
|
|
||||||
def test_detailed_page_with_invalid_template_id(client):
|
def test_detailed_page_with_invalid_template_id(client):
|
||||||
response = client.get(f"/detailed?template_id=99999")
|
response = client.get(f"/detailed?template_id=99999")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b"Template Not Found" in response.content
|
assert "Template Not Found" in response.text
|
||||||
|
|||||||
Reference in New Issue
Block a user