Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fixed #210 (without iterating this time)

purplebirde6391389c791
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index 49ef21ef8..e154391ba 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -143,12 +143,14 @@ class NutritionPlan(models.Model):
if self.user.userprofile.weight:
# Determine closest entry by date
target = self.creation_date
- closest_entry = None
- for entry in WeightEntry.objects.filter(user = self.user).order_by('date').iterator():
- if not closest_entry or \
- (abs(entry.date - self.creation_date) <= abs(closest_entry.date - self.creation_date)):
- closest_entry = entry
- weight = Decimal(closest_entry.weight)
+ closest_entry_gte = WeightEntry.objects.filter(user = self.user) \
+ .filter(date__gte = target).order_by('date')[0]
+ closest_entry_lte = WeightEntry.objects.filter(user = self.user) \
+ .filter(date__lte = target).order_by('-date')[0]
+ if abs(closest_entry_gte.date - target) < abs(closest_entry_lte.date - target):
+ weight = Decimal(closest_entry_gte.weight)
+ else:
+ weight = Decimal(closest_entry_lte.weight)
for key in result['per_kg'].keys():
result['per_kg'][key] = result['total'][key] / weight