Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bytes value #39

Merged
merged 3 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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