From 08b850379f179a75f0aca5187b7218a1e256045b Mon Sep 17 00:00:00 2001 From: tschilling Date: Thu, 13 Apr 2017 13:35:27 -0500 Subject: [PATCH 1/2] Add tests for python3.6, django 1.10 and 1.11 --- tests/urls.py | 1 + tox.ini | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tests/urls.py diff --git a/tests/urls.py b/tests/urls.py new file mode 100644 index 0000000..637600f --- /dev/null +++ b/tests/urls.py @@ -0,0 +1 @@ +urlpatterns = [] diff --git a/tox.ini b/tox.ini index c2bf604..f3dcc4e 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] envlist = - {py27,py34,py35}-django{18,19}-{sqlite,postgres}, + {py27,py34,py35,py36}-django{18,19,110,111}-{sqlite,postgres}, flake8, coverage [testenv] @@ -8,9 +8,12 @@ basepython = py27: python2.7 py34: python3.4 py35: python3.5 + py36: python3.6 deps = django18: django>=1.8,<1.9 django19: django>=1.9,<1.10 + django110: django>=1.10,<1.11 + django111: django>=1.11,<1.12 postgres: psycopg2 setenv = PYTHONPATH = {toxinidir} @@ -31,5 +34,5 @@ commands = coverage run --include='*/timezone_field/*' {envbindir}/django-admin.py test tests deps = coverage - django>=1.8,<1.9 + django>=1.8,<1.12 psycopg2 From 2e021f45dd28480afd98e4e858e5d4bc8ca55d82 Mon Sep 17 00:00:00 2001 From: tschilling Date: Thu, 13 Apr 2017 14:09:13 -0500 Subject: [PATCH 2/2] Add support for handling a bytes values. Fixes #38 --- tests/models.py | 2 ++ tests/tests.py | 10 ++++++++++ timezone_field/fields.py | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/tests/models.py b/tests/models.py index a60a2ff..1657299 100644 --- a/tests/models.py +++ b/tests/models.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from django.db import models from timezone_field import TimeZoneField diff --git a/tests/tests.py b/tests/tests.py index c3ac6fa..f41cd30 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -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'), @@ -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 diff --git a/timezone_field/fields.py b/timezone_field/fields.py index adbeb50..28388ab 100644 --- a/timezone_field/fields.py +++ b/timezone_field/fields.py @@ -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 @@ -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)