Skip to content

Commit

Permalink
Default human readable names without underscores
Browse files Browse the repository at this point in the history
Part of #32, #37
  • Loading branch information
mfogel committed Apr 19, 2018
1 parent af26cbb commit 7824af4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Changelog
* 3.0 (in developement, not yet released)

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


* 2.1 (2018-03-01)
Expand Down Expand Up @@ -158,6 +160,8 @@ __ http://pypi.python.org/pypi/pytz/
__ http://pypi.python.org/pypi/django-timezone-field/
__ http://www.pip-installer.org/
__ https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
__ https://github.com/mfogel/django-timezone-field/issues/32
__ https://github.com/mfogel/django-timezone-field/issues/37
__ https://github.com/mfogel/django-timezone-field/issues/38
__ https://github.com/mfogel/django-timezone-field/issues/39
__ https://github.com/mfogel/django-timezone-field/issues?q=milestone%3A1.3
Expand Down
21 changes: 19 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def test_invalid_uncommon_tz(self):
form = TestForm({'tz': UNCOMMON_TZ})
self.assertFalse(form.is_valid())

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


class TimeZoneFieldModelFormTestCase(TestCase):

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

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


class TimeZoneFieldTestCase(TestCase):

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

def test_default_human_readable_choices_dont_have_underscores(self):
m = TestModel(tz=PST_tz)
self.assertEqual(m.get_tz_display(), 'America/Los Angeles')


class TimeZoneFieldLimitedChoicesTestCase(TestCase):

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

choices = [(pytz.timezone(tz), tz) for tz in pytz.common_timezones]
choices = [
(pytz.timezone(tz), tz.replace('_', ' '))
for tz in pytz.common_timezones
]
field = TimeZoneField(choices=choices)
name, path, args, kwargs = field.deconstruct()
self.assertNotIn('choices', kwargs)

choices = [(tz, tz) for tz in pytz.common_timezones]
choices = [(tz, tz.replace('_', ' ')) for tz in pytz.common_timezones]
field = TimeZoneField(choices=choices)
name, path, args, kwargs = field.deconstruct()
self.assertNotIn('choices', kwargs)
5 changes: 4 additions & 1 deletion timezone_field/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ class TimeZoneField(models.Field):

# NOTE: these defaults are excluded from migrations. If these are changed,
# existing migration files will need to be accomodated.
CHOICES = [(pytz.timezone(tz), tz.replace('_', ' ')) for tz in pytz.common_timezones]
CHOICES = [
(pytz.timezone(tz), tz.replace('_', ' '))
for tz in pytz.common_timezones
]
MAX_LENGTH = 63

def __init__(self, *args, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion timezone_field/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def coerce_to_pytz(val):

defaults = {
'coerce': coerce_to_pytz,
'choices': [(tz, tz) for tz in pytz.common_timezones],
'choices': [
(tz, tz.replace('_', ' ')) for tz in pytz.common_timezones
],
'empty_value': None,
}
defaults.update(kwargs)
Expand Down

0 comments on commit 7824af4

Please sign in to comment.