Set birthdate and height to blank=False

This makes some of the forms etc simpler
This commit is contained in:
Roland Geider
2026-06-15 16:19:43 +02:00
parent 9337bcf8a3
commit ec380fbab5
5 changed files with 50 additions and 3 deletions

View File

@@ -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)'),
),
]

View File

@@ -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,
)

View File

@@ -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
},
)

View File

@@ -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',

View File

@@ -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