From ec380fbab5a0bee4599aeae4fe4fa7787bfe8f06 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Mon, 15 Jun 2026 16:19:43 +0200 Subject: [PATCH] Set birthdate and height to blank=False This makes some of the forms etc simpler --- ...file_birthdate_alter_userprofile_height.py | 25 +++++++++++++++++++ wger/core/models/profile.py | 4 +-- wger/core/tests/test_preferences.py | 13 +++++++++- wger/nutrition/forms.py | 4 +++ .../tests/test_calories_calculator.py | 7 ++++++ 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 wger/core/migrations/0026_alter_userprofile_birthdate_alter_userprofile_height.py diff --git a/wger/core/migrations/0026_alter_userprofile_birthdate_alter_userprofile_height.py b/wger/core/migrations/0026_alter_userprofile_birthdate_alter_userprofile_height.py new file mode 100644 index 000000000..33f2f661e --- /dev/null +++ b/wger/core/migrations/0026_alter_userprofile_birthdate_alter_userprofile_height.py @@ -0,0 +1,25 @@ +# Generated by Django 6.0.5 on 2026-06-15 13:42 + +import django.core.validators +import wger.core.models.profile +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0025_remove_unused_fields_in_userprofile'), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='birthdate', + field=models.DateField(blank=True, null=True, validators=[wger.core.models.profile.birthdate_validator], verbose_name='Date of Birth'), + ), + migrations.AlterField( + model_name='userprofile', + name='height', + field=models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(140), django.core.validators.MaxValueValidator(230)], verbose_name='Height (cm)'), + ), + ] diff --git a/wger/core/models/profile.py b/wger/core/models/profile.py index a57e5a151..7863231a6 100644 --- a/wger/core/models/profile.py +++ b/wger/core/models/profile.py @@ -198,7 +198,7 @@ class UserProfile(models.Model): birthdate = models.DateField( verbose_name=_('Date of Birth'), - blank=False, + blank=True, null=True, validators=[birthdate_validator], ) @@ -206,7 +206,7 @@ class UserProfile(models.Model): height = IntegerField( verbose_name=_('Height (cm)'), - blank=False, + blank=True, validators=[MinValueValidator(140), MaxValueValidator(230)], null=True, ) diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.py index 389086c77..042484b1b 100644 --- a/wger/core/tests/test_preferences.py +++ b/wger/core/tests/test_preferences.py @@ -97,6 +97,17 @@ class PreferencesTestCase(WgerTestCase): self.assertEqual(profile.workout_reminder, 22) self.assertEqual(profile.weight_unit, 'lb') + def test_height_is_optional(self): + """The preferences form saves without a height.""" + self.user_login('test') + response = self.client.post( + reverse('core:user:preferences'), + {**self.form_data, 'height': ''}, + ) + + self.assertEqual(response.status_code, 302) + self.assertIsNone(User.objects.get(username='test').userprofile.height) + def test_email_is_not_editable_from_preferences(self): """ Email management was moved to allauth's EmailView: the preferences @@ -194,7 +205,7 @@ class PreferencesTestCase(WgerTestCase): 'first_name': 'Brand', 'last_name': 'New', 'birthdate': '01/01/2000', - 'height': '', # required field left blank + 'height': 999, # above the valid 140-230 range }, ) diff --git a/wger/nutrition/forms.py b/wger/nutrition/forms.py index 61dc2b1aa..81a6c27ed 100644 --- a/wger/nutrition/forms.py +++ b/wger/nutrition/forms.py @@ -119,6 +119,10 @@ class BmrForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(BmrForm, self).__init__(*args, **kwargs) + + # height is optional on the profile, but the BMR calculation needs it + self.fields['height'].required = True + 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.py index 6fdc67769..9c9876002 100644 --- a/wger/nutrition/tests/test_calories_calculator.py +++ b/wger/nutrition/tests/test_calories_calculator.py @@ -24,6 +24,7 @@ from django.utils import timezone # wger from wger.core.tests.base_testcase import WgerTestCase +from wger.nutrition.forms import BmrForm from wger.utils.constants import TWOPLACES from wger.weight.models import WeightEntry @@ -83,6 +84,12 @@ class CaloriesCalculatorTestCase(WgerTestCase): result = json.loads(response.content.decode('utf8')) self.assertEqual(result, {'bmr': '1780'}) + def test_bmr_requires_height(self): + """The BMR form requires a height, even though it is optional on the profile.""" + form = BmrForm(data={'age': 30, 'gender': 1, 'weight': 80}) + self.assertFalse(form.is_valid()) + self.assertIn('height', form.errors) + def test_automatic_weight_entry_bmr(self): """ Tests that weight entries are automatically created or updated