Self hosted FLOSS fitness/workout, nutrition and weight tracker
Offer the timezone as a dropdown in the preferences
Roland Geiderb2881a33542b
diff --git a/wger/core/forms.py b/wger/core/forms.pyindex 780b84d67..03fbf1763 100644--- a/wger/core/forms.py+++ b/wger/core/forms.py@@ -15,7 +15,9 @@# You should have received a copy of the GNU Affero General Public License# Standard Library+from collections import defaultdictfrom datetime import date+from zoneinfo import available_timezones# Djangofrom django import forms@@ -128,6 +130,24 @@ class WgerSignupForm(AllauthSignupForm):)+def timezone_choices():+ """+ The IANA zones as grouped dropdown choices, e.g. ('Europe/Berlin', 'Berlin')++ Skips the bare abbreviations and the Etc/ area (whose numeric offsets are+ inverted). The validator on the model accepts every IANA name, so this only+ limits what the dropdown offers, not what the API stores.+ """+ groups = defaultdict(list)+ for name in sorted(available_timezones()):+ if '/' not in name or name.startswith('Etc/'):+ continue+ region, _, city = name.partition('/')+ groups[region].append((name, city.replace('_', ' ')))++ return [('', gettext_lazy('Not set')), ('UTC', 'UTC')] + sorted(groups.items())++class UserPreferencesForm(forms.ModelForm):first_name = forms.CharField(label=gettext_lazy('First name'), required=False)last_name = forms.CharField(label=gettext_lazy('Last name'), required=False)@@ -144,6 +164,16 @@ class UserPreferencesForm(forms.ModelForm):),)+ time_zone = forms.ChoiceField(+ label=gettext_lazy('Timezone'),+ required=False,+ choices=timezone_choices,+ help_text=gettext_lazy(+ 'Used to decide which day a workout counts for. '+ 'The mobile app reports it automatically.'+ ),+ )+class Meta:model = UserProfilefields = (@@ -151,6 +181,7 @@ class UserPreferencesForm(forms.ModelForm):'weight_unit','birthdate','height',+ 'time_zone',)def __init__(self, *args, **kwargs):@@ -182,6 +213,7 @@ class UserPreferencesForm(forms.ModelForm):_('Other settings'),'notification_language','weight_unit',+ 'time_zone',),ButtonHolder(Submit('submit', _('Save'), css_class='btn-success btn-block')),)diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.pyindex 63e5eb125..07720e113 100644--- a/wger/core/tests/test_preferences.py+++ b/wger/core/tests/test_preferences.py@@ -23,6 +23,7 @@ from django.urls import reversefrom django.utils import timezone# wger+from wger.core.forms import timezone_choicesfrom wger.core.tests.base_testcase import WgerTestCasefrom wger.measurements.models import Measurementfrom wger.utils.constants import TWOPLACES@@ -31,6 +32,53 @@ from wger.utils.constants import TWOPLACESlogger = logging.getLogger(__name__)+class TimezonePreferenceTestCase(WgerTestCase):+ """+ Test setting the timezone through the preferences form+ """++ FORM_DATA = {+ 'first_name': '',+ 'last_name': '',+ 'notification_language': 2,+ 'weight_unit': 'kg',+ 'birthdate': '02/25/1987',+ 'height': 180,+ }++ def post_preferences(self, time_zone):+ self.user_login('test')+ return self.client.post(+ reverse('core:user:preferences'),+ {**self.FORM_DATA, 'time_zone': time_zone},+ )++ def test_setting_the_zone(self):+ response = self.post_preferences('Pacific/Auckland')++ self.assertEqual(response.status_code, 302)+ profile = User.objects.get(username='test').userprofile+ self.assertEqual(profile.time_zone, 'Pacific/Auckland')++ def test_clearing_the_zone(self):+ profile = User.objects.get(username='test').userprofile+ profile.time_zone = 'Pacific/Auckland'+ profile.save()++ response = self.post_preferences('')++ self.assertEqual(response.status_code, 302)+ profile.refresh_from_db()+ self.assertEqual(profile.time_zone, '')++ def test_the_dropdown_offers_grouped_iana_names(self):+ choices = dict(timezone_choices())++ self.assertIn(('Europe/Berlin', 'Berlin'), choices['Europe'])+ self.assertNotIn('Etc', choices)+ self.assertEqual(choices['UTC'], 'UTC')++class PreferencesTestCase(WgerTestCase):"""Tests the preferences page