Skip to content

Commit 7824af4

Browse files
committed
Default human readable names without underscores
Part of #32, #37
1 parent af26cbb commit 7824af4

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

README.rst

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Changelog
8686
* 3.0 (in developement, not yet released)
8787

8888
* Support django 1.11, 2.0
89+
* Change default human-readable timezone names to exclude underscores
90+
(`#32`__ & `#37`__)
8991

9092

9193
* 2.1 (2018-03-01)
@@ -158,6 +160,8 @@ __ http://pypi.python.org/pypi/pytz/
158160
__ http://pypi.python.org/pypi/django-timezone-field/
159161
__ http://www.pip-installer.org/
160162
__ https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
163+
__ https://github.com/mfogel/django-timezone-field/issues/32
164+
__ https://github.com/mfogel/django-timezone-field/issues/37
161165
__ https://github.com/mfogel/django-timezone-field/issues/38
162166
__ https://github.com/mfogel/django-timezone-field/issues/39
163167
__ https://github.com/mfogel/django-timezone-field/issues?q=milestone%3A1.3

tests/tests.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ def test_invalid_uncommon_tz(self):
6868
form = TestForm({'tz': UNCOMMON_TZ})
6969
self.assertFalse(form.is_valid())
7070

71+
def test_default_human_readable_choices_dont_have_underscores(self):
72+
form = TestForm()
73+
pst_choice = [c for c in form.fields['tz'].choices if c[0] == PST]
74+
self.assertEqual(pst_choice[0][1], 'America/Los Angeles')
75+
7176

7277
class TimeZoneFieldModelFormTestCase(TestCase):
7378

@@ -119,6 +124,11 @@ def test_invalid_uncommmon_tz(self):
119124
self.assertFalse(form.is_valid())
120125
self.assertTrue(any('choice' in e for e in form.errors['tz']))
121126

127+
def test_default_human_readable_choices_dont_have_underscores(self):
128+
form = TestModelForm()
129+
pst_choice = [c for c in form.fields['tz'].choices if c[0] == PST_tz]
130+
self.assertEqual(pst_choice[0][1], 'America/Los Angeles')
131+
122132

123133
class TimeZoneFieldTestCase(TestCase):
124134

@@ -238,6 +248,10 @@ def createField():
238248
TimeZoneField('a verbose name', 'a name', True, 42)
239249
self.assertRaises(ValueError, createField)
240250

251+
def test_default_human_readable_choices_dont_have_underscores(self):
252+
m = TestModel(tz=PST_tz)
253+
self.assertEqual(m.get_tz_display(), 'America/Los Angeles')
254+
241255

242256
class TimeZoneFieldLimitedChoicesTestCase(TestCase):
243257

@@ -368,12 +382,15 @@ def test_specifying_defaults_not_frozen(self):
368382
name, path, args, kwargs = field.deconstruct()
369383
self.assertNotIn('max_length', kwargs)
370384

371-
choices = [(pytz.timezone(tz), tz) for tz in pytz.common_timezones]
385+
choices = [
386+
(pytz.timezone(tz), tz.replace('_', ' '))
387+
for tz in pytz.common_timezones
388+
]
372389
field = TimeZoneField(choices=choices)
373390
name, path, args, kwargs = field.deconstruct()
374391
self.assertNotIn('choices', kwargs)
375392

376-
choices = [(tz, tz) for tz in pytz.common_timezones]
393+
choices = [(tz, tz.replace('_', ' ')) for tz in pytz.common_timezones]
377394
field = TimeZoneField(choices=choices)
378395
name, path, args, kwargs = field.deconstruct()
379396
self.assertNotIn('choices', kwargs)

timezone_field/fields.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class TimeZoneField(models.Field):
3636

3737
# NOTE: these defaults are excluded from migrations. If these are changed,
3838
# existing migration files will need to be accomodated.
39-
CHOICES = [(pytz.timezone(tz), tz.replace('_', ' ')) for tz in pytz.common_timezones]
39+
CHOICES = [
40+
(pytz.timezone(tz), tz.replace('_', ' '))
41+
for tz in pytz.common_timezones
42+
]
4043
MAX_LENGTH = 63
4144

4245
def __init__(self, *args, **kwargs):

timezone_field/forms.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ def coerce_to_pytz(val):
1414

1515
defaults = {
1616
'coerce': coerce_to_pytz,
17-
'choices': [(tz, tz) for tz in pytz.common_timezones],
17+
'choices': [
18+
(tz, tz.replace('_', ' ')) for tz in pytz.common_timezones
19+
],
1820
'empty_value': None,
1921
}
2022
defaults.update(kwargs)

0 commit comments

Comments
 (0)