Self hosted FLOSS fitness/workout, nutrition and weight tracker
Stop the calorie calculator from storing implausible weights
Roland Geiderd46aa24cff96
diff --git a/wger/measurements/migrations/0007_official_categories.py b/wger/measurements/migrations/0007_official_categories.pyindex 2b4e4dedc..74df4d41f 100644--- a/wger/measurements/migrations/0007_official_categories.py+++ b/wger/measurements/migrations/0007_official_categories.py@@ -50,7 +50,8 @@ def migrate_weight_to_measurements(apps, schema_editor):}batch = []- for entry in WeightEntry.objects.all().iterator():+ # A zero is not a measurement, but were being added by faulty logic in the BMR calculator+ for entry in WeightEntry.objects.exclude(weight=0).iterator():if entry.user_id not in category_by_user:# Entries of users without a profile (created outside the normal# signal path)diff --git a/wger/nutrition/forms.py b/wger/nutrition/forms.pyindex 81a6c27ed..07f533adc 100644--- a/wger/nutrition/forms.py+++ b/wger/nutrition/forms.py@@ -20,6 +20,10 @@ from datetime import datetime# Djangofrom django import forms+from django.core.validators import (+ MaxValueValidator,+ MinValueValidator,+)from django.forms import BooleanFieldfrom django.utils.translation import (gettext as _,@@ -37,6 +41,8 @@ from crispy_forms.layout import (# wgerfrom wger.core.models import UserProfile+from wger.measurements.limits import limits_for+from wger.measurements.models.category import MetricTypefrom wger.nutrition.models import (Ingredient,IngredientWeightUnit,@@ -123,6 +129,18 @@ class BmrForm(forms.ModelForm):# height is optional on the profile, but the BMR calculation needs itself.fields['height'].required = True+ # The calculator writes its weight back as a measurement, so it is+ # bounded like one. The form is prefilled with the profile weight,+ # which is 0 for a user who has none yet, and submitting that unchanged+ # is where the stored zeroes come from+ limits = limits_for(MetricType.BODY_WEIGHT, self.instance.weight_unit)+ self.fields['weight'].min_value = limits.min+ self.fields['weight'].max_value = limits.max+ self.fields['weight'].validators += [+ MinValueValidator(limits.min),+ MaxValueValidator(limits.max),+ ]+self.helper = FormHelper()self.helper.layout = Layout('age',diff --git a/wger/nutrition/tests/test_calories_calculator.py b/wger/nutrition/tests/test_calories_calculator.pyindex e4aa9879d..6923b5e27 100644--- a/wger/nutrition/tests/test_calories_calculator.py+++ b/wger/nutrition/tests/test_calories_calculator.py@@ -89,6 +89,29 @@ class CaloriesCalculatorTestCase(WgerTestCase):self.assertFalse(form.is_valid())self.assertIn('height', form.errors)+ def test_bmr_refuses_an_implausible_weight(self):+ """The weight is written back as a measurement, so it is bounded like one."""+ for weight in (0, 10, 400):+ form = BmrForm(data={'age': 30, 'height': 180, 'gender': 1, 'weight': weight})+ self.assertFalse(form.is_valid())+ self.assertIn('weight', form.errors)++ def test_bmr_writes_no_entry_for_a_prefilled_zero(self):+ """+ The form is prefilled with the profile weight, which is 0 without any+ entry; submitting that unchanged used to store it+ """+ self.user_login('test')+ user = User.objects.get(username=self.current_user)+ Measurement.body_weight_for(user).delete()++ response = self.client.post(+ reverse('nutrition:calories:bmr'), {'age': 30, 'height': 180, 'gender': 1, 'weight': 0}+ )++ self.assertEqual(response.status_code, 200)+ self.assertFalse(Measurement.body_weight_for(user).exists())+def test_automatic_weight_entry_bmr(self):"""Tests that weight entries are automatically created or updated