mirror of
https://github.com/sstent/foodplanner.git
synced 2026-03-14 01:05:23 +00:00
tryiong to fix the details page
This commit is contained in:
119
main.py
119
main.py
@@ -1895,19 +1895,24 @@ 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
|
||||
from datetime import datetime, date
|
||||
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:
|
||||
# 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(request, "detailed.html", {
|
||||
return templates.TemplateResponse("detailed.html", {
|
||||
"request": request, "title": "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()
|
||||
@@ -1920,7 +1925,7 @@ async def detailed(request: Request, person: str = "Sarah", plan_date: str = Non
|
||||
for tm in template_meals:
|
||||
meal_nutrition = calculate_meal_nutrition(tm.meal, db)
|
||||
meal_details.append({
|
||||
'plan': {'meal': tm.meal},
|
||||
'plan': {'meal': tm.meal, 'meal_time': tm.meal_time},
|
||||
'nutrition': meal_nutrition,
|
||||
'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['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']
|
||||
template_nutrition['net_carbs'] = template_nutrition['carbs'] - template_nutrition.get('fiber', 0)
|
||||
|
||||
context = {
|
||||
"request": request,
|
||||
"title": f"{template.name} Template",
|
||||
"meal_details": meal_details,
|
||||
"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}")
|
||||
return templates.TemplateResponse(request, "detailed.html", 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}")
|
||||
# If no plan_date is provided, default to today's date
|
||||
if not plan_date:
|
||||
plan_date_obj = date.today()
|
||||
else:
|
||||
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(request, "detailed.html", {
|
||||
return templates.TemplateResponse("detailed.html", {
|
||||
"request": request, "title": "Invalid Date",
|
||||
"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: Found {len(plans)} plans for {person} on {plan_date_obj}")
|
||||
logging.info(f"DEBUG: Loading plan 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 = []
|
||||
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,
|
||||
'fiber': mf.food.fiber * mf.quantity,
|
||||
'sodium': mf.food.sodium * mf.quantity,
|
||||
})
|
||||
|
||||
meal_details.append({
|
||||
'plan': plan,
|
||||
'nutrition': meal_nutrition,
|
||||
'foods': foods
|
||||
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,
|
||||
'fiber': (mf.food.fiber or 0) * mf.quantity,
|
||||
'sodium': (mf.food.sodium or 0) * mf.quantity,
|
||||
})
|
||||
|
||||
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(request, "detailed.html", context)
|
||||
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,
|
||||
"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.error("DEBUG: Neither plan_date nor template_id were provided")
|
||||
return templates.TemplateResponse(request, "detailed.html", {
|
||||
"request": request, "title": "Error",
|
||||
"error": "Please provide either a plan date or a template ID.",
|
||||
"day_totals": {}
|
||||
})
|
||||
logging.info(f"DEBUG: Rendering plan details with context: {context}")
|
||||
return templates.TemplateResponse("detailed.html", context)
|
||||
|
||||
# Tracker tab - Main page
|
||||
@app.get("/tracker", response_class=HTMLResponse)
|
||||
|
||||
@@ -2,44 +2,38 @@
|
||||
{% block content %}
|
||||
<div class="row mb-3">
|
||||
<div class="col-md-6">
|
||||
<h3>Detailed View for {{ person }}</h3>
|
||||
<div class="mb-3">
|
||||
<div class="btn-group" role="group">
|
||||
<input type="radio" class="btn-check" name="viewMode" id="dayView" autocomplete="off" {% if view_mode == 'day' or not view_mode %}checked{% endif %}>
|
||||
<label class="btn btn-outline-primary" for="dayView">View Planned Day</label>
|
||||
<input type="radio" class="btn-check" name="viewMode" id="templateView" autocomplete="off" {% if view_mode == 'template' %}checked{% endif %}>
|
||||
<label class="btn btn-outline-primary" for="templateView">View Template</label>
|
||||
<h3>
|
||||
Detailed View for
|
||||
<div class="btn-group">
|
||||
<a href="{{ url_for('detailed', person='Sarah', plan_date=plan_date.isoformat() if plan_date else '') }}"
|
||||
class="btn btn-sm {% if person == 'Sarah' %}btn-primary{% else %}btn-outline-primary{% endif %}">Sarah</a>
|
||||
<a href="{{ url_for('detailed', person='Stuart', plan_date=plan_date.isoformat() if plan_date else '') }}"
|
||||
class="btn btn-sm {% if person == 'Stuart' %}btn-primary{% else %}btn-outline-primary{% endif %}">Stuart</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="daySelector" {% if view_mode == 'template' %}style="display: none;"{% endif %}>
|
||||
<form method="get" class="d-flex">
|
||||
</h3>
|
||||
|
||||
<div id="daySelector">
|
||||
<form action="{{ url_for('detailed') }}" method="get" class="d-flex">
|
||||
<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">
|
||||
<i class="bi bi-search"></i> View Day
|
||||
</button>
|
||||
</form>
|
||||
</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 class="col-md-6 text-end">
|
||||
<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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user