fix: convert PostgreSQL Decimal to float for math operations
All checks were successful
Deploy Development / deploy (push) Successful in 59s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 13s

Fixed TypeError when preparing AI prompt template variables.
PostgreSQL returns NUMERIC columns as decimal.Decimal, not float.

**Fixed in _prepare_template_vars:**
- Weight calculations (protein targets, delta)
- Nutrition averages (kcal, protein, fat, carbs)
- Activity totals (kcal_active)

All Decimal values now converted to float before math operations.

Error was: "TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'"

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lars 2026-03-18 21:44:10 +01:00
parent 8e25b54cc2
commit f7f7f745b1

View File

@ -790,7 +790,7 @@ def _prepare_template_vars(data: dict) -> dict:
# Weight trend summary
if len(weight) >= 2:
recent = weight[:30]
delta = recent[0]['weight'] - recent[-1]['weight']
delta = float(recent[0]['weight']) - float(recent[-1]['weight'])
vars['weight_trend'] = f"{len(recent)} Einträge, Δ30d: {delta:+.1f}kg"
else:
vars['weight_trend'] = "zu wenig Daten"
@ -815,15 +815,15 @@ def _prepare_template_vars(data: dict) -> dict:
# Nutrition summary
if nutrition:
n = len(nutrition)
avg_kcal = sum(d.get('kcal',0) for d in nutrition) / n
avg_prot = sum(d.get('protein_g',0) for d in nutrition) / n
avg_kcal = sum(float(d.get('kcal',0) or 0) for d in nutrition) / n
avg_prot = sum(float(d.get('protein_g',0) or 0) for d in nutrition) / n
vars['nutrition_summary'] = f"{n} Tage, Ø {avg_kcal:.0f}kcal, {avg_prot:.0f}g Protein"
vars['nutrition_detail'] = vars['nutrition_summary']
vars['nutrition_days'] = n
vars['kcal_avg'] = round(avg_kcal)
vars['protein_avg'] = round(avg_prot,1)
vars['fat_avg'] = round(sum(d.get('fat_g',0) for d in nutrition) / n,1)
vars['carb_avg'] = round(sum(d.get('carbs_g',0) for d in nutrition) / n,1)
vars['fat_avg'] = round(sum(float(d.get('fat_g',0) or 0) for d in nutrition) / n,1)
vars['carb_avg'] = round(sum(float(d.get('carbs_g',0) or 0) for d in nutrition) / n,1)
else:
vars['nutrition_summary'] = "keine Daten"
vars['nutrition_detail'] = "keine Daten"
@ -835,13 +835,14 @@ def _prepare_template_vars(data: dict) -> dict:
# Protein targets
w = weight[0]['weight'] if weight else prof.get('height',178) - 100
w = float(w) # Convert Decimal to float for math operations
vars['protein_ziel_low'] = round(w * 1.6)
vars['protein_ziel_high'] = round(w * 2.2)
# Activity summary
if activity:
n = len(activity)
total_kcal = sum(a.get('kcal_active',0) for a in activity)
total_kcal = sum(float(a.get('kcal_active',0) or 0) for a in activity)
vars['activity_summary'] = f"{n} Trainings, {total_kcal:.0f}kcal gesamt"
vars['activity_detail'] = vars['activity_summary']
vars['activity_kcal_summary'] = f"Ø {total_kcal/n:.0f}kcal/Training"