tryiong to fix the details page

This commit is contained in:
2025-09-29 13:06:54 -07:00
parent d1bf3b817d
commit 7103c42c05
2 changed files with 86 additions and 83 deletions

119
main.py
View File

@@ -1895,19 +1895,24 @@ 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, date
logging.info(f"DEBUG: Detailed page requested with person={person}, plan_date={plan_date}, template_id={template_id}") logging.info(f"DEBUG: Detailed page requested with person={person}, plan_date={plan_date}, template_id={template_id}")
# Get all templates for the dropdown
templates = db.query(Template).order_by(Template.name).all()
if template_id: if template_id:
# Show template details # Show template details
logging.info(f"DEBUG: Loading template with id: {template_id}") 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") logging.error(f"DEBUG: Template with id {template_id} not found")
return templates.TemplateResponse(request, "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": {} "day_totals": {},
"templates": templates,
"person": person
}) })
template_meals = db.query(TemplateMeal).filter(TemplateMeal.template_id == template_id).all() template_meals = db.query(TemplateMeal).filter(TemplateMeal.template_id == template_id).all()
@@ -1920,7 +1925,7 @@ async def detailed(request: Request, person: str = "Sarah", plan_date: str = Non
for tm in template_meals: for tm in template_meals:
meal_nutrition = calculate_meal_nutrition(tm.meal, db) meal_nutrition = calculate_meal_nutrition(tm.meal, db)
meal_details.append({ meal_details.append({
'plan': {'meal': tm.meal}, 'plan': {'meal': tm.meal, 'meal_time': tm.meal_time},
'nutrition': meal_nutrition, 'nutrition': meal_nutrition,
'foods': [] # Template view doesn't show individual foods 'foods': [] # Template view doesn't show individual foods
}) })
@@ -1935,79 +1940,83 @@ async def detailed(request: Request, person: str = "Sarah", plan_date: str = Non
template_nutrition['protein_pct'] = round((template_nutrition['protein'] * 4 / total_cals) * 100, 1) template_nutrition['protein_pct'] = round((template_nutrition['protein'] * 4 / total_cals) * 100, 1)
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.get('fiber', 0)
context = { context = {
"request": request, "request": request,
"title": f"{template.name} Template", "title": f"{template.name} Template",
"meal_details": meal_details, "meal_details": meal_details,
"day_totals": template_nutrition, "day_totals": template_nutrition,
"person": person "person": person,
"templates": templates,
"selected_template_id": template_id
} }
logging.info(f"DEBUG: Rendering template details with context: {context}") logging.info(f"DEBUG: Rendering template details with context: {context}")
return templates.TemplateResponse(request, "detailed.html", context) return templates.TemplateResponse("detailed.html", context)
if plan_date: # If no plan_date is provided, default to today's date
# Show plan details for a specific date if not plan_date:
logging.info(f"DEBUG: Loading plan for date: {plan_date}") plan_date_obj = date.today()
else:
try: try:
plan_date_obj = datetime.fromisoformat(plan_date).date() plan_date_obj = datetime.fromisoformat(plan_date).date()
except ValueError: except ValueError:
logging.error(f"DEBUG: Invalid date format for plan_date: {plan_date}") logging.error(f"DEBUG: Invalid date format for plan_date: {plan_date}")
return templates.TemplateResponse(request, "detailed.html", { return templates.TemplateResponse("detailed.html", {
"request": request, "title": "Invalid Date", "request": request, "title": "Invalid Date",
"error": "Invalid date format. Please use YYYY-MM-DD.", "error": "Invalid date format. Please use YYYY-MM-DD.",
"day_totals": {} "day_totals": {},
"templates": templates,
"person": person
}) })
plans = db.query(Plan).filter(Plan.person == person, Plan.date == plan_date_obj).all() logging.info(f"DEBUG: Loading plan for {person} on {plan_date_obj}")
logging.info(f"DEBUG: Found {len(plans)} plans for {person} on {plan_date_obj}") 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) day_totals = calculate_day_nutrition(plans, db)
meal_details = []
for plan in plans:
meal_nutrition = calculate_meal_nutrition(plan.meal, db)
meal_details = [] foods = []
for plan in plans: for mf in plan.meal.meal_foods:
meal_nutrition = calculate_meal_nutrition(plan.meal, db) foods.append({
'name': mf.food.name,
foods = [] 'quantity': mf.quantity,
for mf in plan.meal.meal_foods: 'serving_size': mf.food.serving_size,
foods.append({ 'serving_unit': mf.food.serving_unit,
'name': mf.food.name, 'calories': mf.food.calories * mf.quantity,
'quantity': mf.quantity, 'protein': mf.food.protein * mf.quantity,
'serving_size': mf.food.serving_size, 'carbs': mf.food.carbs * mf.quantity,
'serving_unit': mf.food.serving_unit, 'fat': mf.food.fat * mf.quantity,
'calories': mf.food.calories * mf.quantity, 'fiber': (mf.food.fiber or 0) * mf.quantity,
'protein': mf.food.protein * mf.quantity, 'sodium': (mf.food.sodium or 0) * mf.quantity,
'carbs': mf.food.carbs * mf.quantity,
'fat': mf.food.fat * mf.quantity,
'fiber': mf.food.fiber * mf.quantity,
'sodium': mf.food.sodium * mf.quantity,
})
meal_details.append({
'plan': plan,
'nutrition': meal_nutrition,
'foods': foods
}) })
context = { meal_details.append({
"request": request, 'plan': plan,
"title": f"{person}'s Detailed Plan for {plan_date_obj.strftime('%B %d, %Y')}", 'nutrition': meal_nutrition,
"meal_details": meal_details, 'foods': foods
"day_totals": day_totals, })
"person": person,
"plan_date": plan_date_obj context = {
} "request": request,
logging.info(f"DEBUG: Rendering plan details with context: {context}") "title": f"{person}'s Detailed Plan for {plan_date_obj.strftime('%B %d, %Y')}",
return templates.TemplateResponse(request, "detailed.html", context) "meal_details": meal_details,
"day_totals": day_totals,
"person": person,
"plan_date": plan_date_obj,
"templates": templates
}
# If no meals are planned, add a message
if not meal_details:
context["message"] = "No meals planned for this day."
# If neither plan_date nor template_id is provided, return an error logging.info(f"DEBUG: Rendering plan details with context: {context}")
logging.error("DEBUG: Neither plan_date nor template_id were provided") return templates.TemplateResponse("detailed.html", context)
return templates.TemplateResponse(request, "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)

View File

@@ -2,44 +2,38 @@
{% block content %} {% block content %}
<div class="row mb-3"> <div class="row mb-3">
<div class="col-md-6"> <div class="col-md-6">
<h3>Detailed View for {{ person }}</h3> <h3>
<div class="mb-3"> Detailed View for
<div class="btn-group" role="group"> <div class="btn-group">
<input type="radio" class="btn-check" name="viewMode" id="dayView" autocomplete="off" {% if view_mode == 'day' or not view_mode %}checked{% endif %}> <a href="{{ url_for('detailed', person='Sarah', plan_date=plan_date.isoformat() if plan_date else '') }}"
<label class="btn btn-outline-primary" for="dayView">View Planned Day</label> class="btn btn-sm {% if person == 'Sarah' %}btn-primary{% else %}btn-outline-primary{% endif %}">Sarah</a>
<input type="radio" class="btn-check" name="viewMode" id="templateView" autocomplete="off" {% if view_mode == 'template' %}checked{% endif %}> <a href="{{ url_for('detailed', person='Stuart', plan_date=plan_date.isoformat() if plan_date else '') }}"
<label class="btn btn-outline-primary" for="templateView">View Template</label> class="btn btn-sm {% if person == 'Stuart' %}btn-primary{% else %}btn-outline-primary{% endif %}">Stuart</a>
</div> </div>
</div> </h3>
<div id="daySelector" {% if view_mode == 'template' %}style="display: none;"{% endif %}> <div id="daySelector">
<form method="get" class="d-flex"> <form action="{{ url_for('detailed') }}" method="get" class="d-flex">
<input type="hidden" name="person" value="{{ person }}"> <input type="hidden" name="person" value="{{ person }}">
<input type="date" class="form-control me-2" name="plan_date" value="{% if view_mode == 'day' %}{{ plan_date }}{% else %}{{ today|default('') }}{% endif %}" required> <input type="date" class="form-control me-2" name="plan_date" value="{{ plan_date.isoformat() if plan_date else '' }}" required>
<button class="btn btn-primary" type="submit"> <button class="btn btn-primary" type="submit">
<i class="bi bi-search"></i> View Day <i class="bi bi-search"></i> View Day
</button> </button>
</form> </form>
</div> </div>
<div id="templateSelector" {% if view_mode != 'template' %}style="display: none;"{% endif %}>
<form method="get" class="d-flex">
<input type="hidden" name="person" value="{{ person }}">
<select class="form-control me-2" name="template_id" required>
<option value="">Select Template...</option>
{% for template in templates %}
<option value="{{ template.id }}" {% if template_id == template.id %}selected{% endif %}>{{ template.name }}</option>
{% endfor %}
</select>
<button class="btn btn-primary" type="submit">
<i class="bi bi-search"></i> View Template
</button>
</form>
</div>
</div> </div>
<div class="col-md-6 text-end"> <div class="col-md-6 text-end">
<h4 class="mb-0">{{ title }}</h4> <h4 class="mb-0">{{ title }}</h4>
<small class="text-muted">Detailed nutritional breakdown</small> <div class="dropdown mt-2">
<button class="btn btn-secondary dropdown-toggle" type="button" id="templateDropdown" data-bs-toggle="dropdown" aria-expanded="false">
View Template
</button>
<ul class="dropdown-menu" aria-labelledby="templateDropdown">
{% for t in templates %}
<li><a class="dropdown-item" href="{{ url_for('detailed', template_id=t.id) }}">{{ t.name }}</a></li>
{% endfor %}
</ul>
</div>
</div> </div>
</div> </div>