Self hosted FLOSS fitness/workout, nutrition and weight tracker
Keep an out-of-dropdown timezone across a form save
Roland Geiderc3d495ee40fa
diff --git a/wger/core/forms.py b/wger/core/forms.pyindex 03fbf1763..5dbe61c56 100644--- a/wger/core/forms.py+++ b/wger/core/forms.py@@ -187,6 +187,14 @@ class UserPreferencesForm(forms.ModelForm):def __init__(self, *args, **kwargs):super(UserPreferencesForm, self).__init__(*args, **kwargs)+ # A zone set through the API can lie outside the dropdown (e.g.+ # Etc/GMT+5). Without its option the browser submits the empty choice+ # and the next save of the form silently clears the field.+ tz_field = self.fields['time_zone']+ current = self.instance.time_zone+ if current and not tz_field.valid_value(current):+ tz_field.choices = [*tz_field.choices, (current, current)]+hattrs = self.fields['height'].widget.attrshattrs.setdefault('type', 'number')hattrs.setdefault('step', '1')diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.pyindex 07720e113..0d3ccb84d 100644--- a/wger/core/tests/test_preferences.py+++ b/wger/core/tests/test_preferences.py@@ -71,6 +71,23 @@ class TimezonePreferenceTestCase(WgerTestCase):profile.refresh_from_db()self.assertEqual(profile.time_zone, '')+ def test_a_zone_outside_the_dropdown_survives_the_form(self):+ """An API-set zone the dropdown does not offer is kept, not silently cleared"""++ profile = User.objects.get(username='test').userprofile+ profile.time_zone = 'Etc/GMT+5'+ profile.save()++ self.user_login('test')+ response = self.client.get(reverse('core:user:preferences'))+ self.assertContains(response, 'Etc/GMT+5')++ response = self.post_preferences('Etc/GMT+5')++ self.assertEqual(response.status_code, 302)+ profile.refresh_from_db()+ self.assertEqual(profile.time_zone, 'Etc/GMT+5')+def test_the_dropdown_offers_grouped_iana_names(self):choices = dict(timezone_choices())