Skip to content

Commit

Permalink
Merge pull request #39 from tim-schilling/handle-bytes-value
Browse files Browse the repository at this point in the history
Handle bytes value
  • Loading branch information
mfogel committed Mar 1, 2018
2 parents 0dcf8db + c8f25f9 commit 83b7b0f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

from django.db import models

from timezone_field import TimeZoneField
Expand Down
10 changes: 10 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class TimeZoneFieldDeconstructTestCase(TestCase):

test_fields = (
TimeZoneField(),
TimeZoneField(default='UTC'),
TimeZoneField(max_length=42),
TimeZoneField(choices=[
(pytz.timezone('US/Pacific'), 'US/Pacific'),
Expand Down Expand Up @@ -316,6 +317,15 @@ def test_full_serialization(self):
# ensuring the following call doesn't throw an error
MigrationWriter.serialize(field)

def test_from_db_value(self):
"""
Verify that the field can handle data coming back as bytes from the
db.
"""
field = TimeZoneField()
value = field.from_db_value(b'UTC', None, None, None)
self.assertEqual(pytz.UTC, value)

def test_default_kwargs_not_frozen(self):
"""
Ensure the deconstructed representation of the field does not contain
Expand Down
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
urlpatterns = []
5 changes: 5 additions & 0 deletions timezone_field/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.utils import six
from django.utils.encoding import force_text

from timezone_field.utils import is_pytz_instance

Expand Down Expand Up @@ -114,4 +115,8 @@ def _get_python_and_db_repr(self, value):
return (pytz.timezone(value), value)
except pytz.UnknownTimeZoneError:
pass
try:
return (pytz.timezone(force_text(value)), force_text(value))
except pytz.UnknownTimeZoneError:
pass
raise ValidationError("Invalid timezone '%s'" % value)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ commands = coverage erase
[testenv:coverage-report]
commands =
coverage report
coverage html
coverage html

0 comments on commit 83b7b0f

Please sign in to comment.