Self hosted FLOSS fitness/workout, nutrition and weight tracker
Automatically create a new weight entry when calculating the BMR
Roland Geider178cae2d6f18
diff --git a/wger/manager/models.py b/wger/manager/models.pyindex 345b6b1b8..06b5c3df9 100644--- a/wger/manager/models.py+++ b/wger/manager/models.py@@ -771,6 +771,27 @@ by the US Department of Agriculture. It is extremely complete, with aroundtotal = (sleep + work + sport + freetime) / 24.0return decimal.Decimal(str(total)).quantize(decimal.Decimal('.01'))+ def user_bodyweight(self, weight):+ '''+ Create a new weight entry as needed+ '''++ if (not WeightEntry.objects.filter(user=self.user).exists()+ or (datetime.date.today()+ - WeightEntry.objects.filter(user=self.user).latest().creation_date+ > datetime.timedelta(1))):+ entry = WeightEntry()+ entry.weight = weight+ entry.user = self.user+ entry.creation_date = datetime.date.today()+ entry.save()++ # Update the last entry+ else:+ entry = WeightEntry.objects.filter(user=self.user).latest()+ entry.weight = weight+ entry.save()+# Every new user gets a profile@disable_for_loaddatadiff --git a/wger/nutrition/tests/test_bmi.py b/wger/nutrition/tests/test_bmi.pyindex 17b3008a1..a9744f6e2 100644--- a/wger/nutrition/tests/test_bmi.py+++ b/wger/nutrition/tests/test_bmi.py@@ -51,7 +51,7 @@ class BmiTestCase(WorkoutManagerTestCase):self.assertEqual(decimal.Decimal(bmi['weight']), decimal.Decimal('80'))self.assertEqual(decimal.Decimal(bmi['height']), decimal.Decimal('180'))- def test_automaitc_weight_entry(self):+ def test_automatic_weight_entry(self):'''Tests that weight entries are automatically created or updated'''diff --git a/wger/nutrition/tests/test_calories_calculator.py b/wger/nutrition/tests/test_calories_calculator.pyindex 5eceb72ba..6b8024694 100644--- a/wger/nutrition/tests/test_calories_calculator.py+++ b/wger/nutrition/tests/test_calories_calculator.py@@ -108,7 +108,7 @@ class CaloresCalculatorTestCase(WorkoutManagerTestCase):'weight': 80})self.assertEqual(response.status_code, 200)result = json.loads(response.content)- self.assertEqual(result, {'bmr': '1810'})+ self.assertEqual(result, {'bmr': '1780'})def test_total_calories_form(self):'''@@ -126,3 +126,49 @@ class CaloresCalculatorTestCase(WorkoutManagerTestCase):user = User.objects.get(username=self.current_user)self.assertEqual(user.userprofile.calories, 2000)++ def test_automatic_weight_entry(self):+ '''+ Tests that weight entries are automatically created or updated+ '''++ self.user_login('test')+ user = User.objects.get(username=self.current_user)++ # Existing weight entry is old, a new one is created+ entry1 = WeightEntry.objects.filter(user=user).latest()+ response = self.client.post(reverse('calories-calculate-bmr'),+ {'age': 30,+ 'height': 180,+ 'gender': 1,+ 'weight': 80})+ self.assertEqual(response.status_code, 200)+ entry2 = WeightEntry.objects.filter(user=user).latest()+ self.assertEqual(entry1.weight, 83)+ self.assertEqual(entry2.weight, 80)++ # Existing weight entry is from today, is updated+ entry2.delete()+ entry1.creation_date = datetime.date.today()+ entry1.save()+ response = self.client.post(reverse('calories-calculate-bmr'),+ {'age': 30,+ 'height': 180,+ 'gender': 1,+ 'weight': 80})+ self.assertEqual(response.status_code, 200)+ entry2 = WeightEntry.objects.filter(user=user).latest()+ self.assertEqual(entry1.pk, entry2.pk)+ self.assertEqual(entry2.weight, 80)++ # No existing entries+ WeightEntry.objects.filter(user=user).delete()+ response = self.client.post(reverse('calories-calculate-bmr'),+ {'age': 30,+ 'height': 180,+ 'gender': 1,+ 'weight': 80})+ self.assertEqual(response.status_code, 200)+ entry = WeightEntry.objects.filter(user=user).latest()+ self.assertEqual(entry.weight, 80)+ self.assertEqual(entry.creation_date, datetime.date.today())diff --git a/wger/nutrition/views/bmi.py b/wger/nutrition/views/bmi.pyindex 1a95b602e..ebc8570c9 100644--- a/wger/nutrition/views/bmi.py+++ b/wger/nutrition/views/bmi.py@@ -60,21 +60,7 @@ def calculate(request):form.save()# Create a new weight entry as needed- if (not WeightEntry.objects.filter(user=request.user).exists()- or (datetime.date.today()- - WeightEntry.objects.filter(user=request.user).latest().creation_date- > datetime.timedelta(1))):- entry = WeightEntry()- entry.weight = form.cleaned_data['weight']- entry.user = request.user- entry.creation_date = datetime.date.today()- entry.save()-- # Update the last entry- else:- entry = WeightEntry.objects.filter(user=request.user).latest()- entry.weight = form.cleaned_data['weight']- entry.save()+ request.user.userprofile.user_bodyweight(form.cleaned_data['weight'])bmi = request.user.userprofile.calculate_bmi()result = {'bmi': '{0:.2f}'.format(bmi),diff --git a/wger/nutrition/views/calculator.py b/wger/nutrition/views/calculator.pyindex 27947403f..f3065a404 100644--- a/wger/nutrition/views/calculator.py+++ b/wger/nutrition/views/calculator.py@@ -71,6 +71,9 @@ def calculate_bmr(request):if form.is_valid():form.save()+ # Create a new weight entry as needed+ request.user.userprofile.user_bodyweight(form.cleaned_data['weight'])+bmr = request.user.userprofile.calculate_basal_metabolic_rate()result = {'bmr': '{0:.0f}'.format(bmr)}data = json.dumps(result)