Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fixed issue #210
purplebird2c8e892122b8
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex 74d3d4107..49ef21ef8 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -37,6 +37,7 @@ from wger.utils.cache import cache_mapperfrom wger.utils.fields import Html5TimeFieldfrom wger.utils.models import AbstractLicenseModelfrom wger.utils.units import AbstractWeight+from wger.weight.models import WeightEntryMEALITEM_WEIGHT_GRAM = '1'MEALITEM_WEIGHT_UNIT = '2'@@ -137,10 +138,17 @@ class NutritionPlan(models.Model):for key in result['percent'].keys():result['percent'][key] = \result['total'][key] * ENERGY_FACTOR[key][unit] / energy * 100-+# Per body weightif self.user.userprofile.weight:- weight = Decimal(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)for key in result['per_kg'].keys():result['per_kg'][key] = result['total'][key] / weight