From a3cbf51bc9dcb9494bb657ac5bc1749a80bc2973 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Mon, 6 Jun 2016 12:42:18 -0700 Subject: [PATCH] django 1.10 fix (#516) Fix django packages for 1.10, add docs noting django 1.8+ support. --- oauth2client/contrib/django_orm.py | 13 +++++++-- oauth2client/contrib/django_util/__init__.py | 1 + tests/contrib/test_django_orm.py | 28 ++++++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/oauth2client/contrib/django_orm.py b/oauth2client/contrib/django_orm.py index 64e6910..18e24f2 100644 --- a/oauth2client/contrib/django_orm.py +++ b/oauth2client/contrib/django_orm.py @@ -16,6 +16,9 @@ Utilities for using OAuth 2.0 in conjunction with the Django datastore. + + +Only Django versions 1.8+ are supported. """ import oauth2client @@ -31,7 +34,7 @@ from oauth2client.client import Storage as BaseStorage __author__ = 'jcgregorio@google.com (Joe Gregorio)' -class CredentialsField(six.with_metaclass(models.SubfieldBase, models.Field)): +class CredentialsField(models.Field): def __init__(self, *args, **kwargs): if 'null' not in kwargs: @@ -41,6 +44,9 @@ class CredentialsField(six.with_metaclass(models.SubfieldBase, models.Field)): def get_internal_type(self): return 'TextField' + def from_db_value(self, value, expression, connection, context): + return self.to_python(value) + def to_python(self, value): if value is None: return None @@ -68,7 +74,7 @@ class CredentialsField(six.with_metaclass(models.SubfieldBase, models.Field)): return self.get_prep_value(value) -class FlowField(six.with_metaclass(models.SubfieldBase, models.Field)): +class FlowField(models.Field): def __init__(self, *args, **kwargs): if 'null' not in kwargs: @@ -78,6 +84,9 @@ class FlowField(six.with_metaclass(models.SubfieldBase, models.Field)): def get_internal_type(self): return 'TextField' + def from_db_value(self, value, expression, connection, context): + return self.to_python(value) + def to_python(self, value): if value is None: return None diff --git a/oauth2client/contrib/django_util/__init__.py b/oauth2client/contrib/django_util/__init__.py index 8974f24..5aa12ac 100644 --- a/oauth2client/contrib/django_util/__init__.py +++ b/oauth2client/contrib/django_util/__init__.py @@ -20,6 +20,7 @@ that user credentials are available, and an ``oauth_enabled`` decorator to check if the user has authorized, and helper shortcuts to create the authorization URL otherwise. +Only Django versions 1.8+ are supported. Configuration ============= diff --git a/tests/contrib/test_django_orm.py b/tests/contrib/test_django_orm.py index 0346719..e15db08 100644 --- a/tests/contrib/test_django_orm.py +++ b/tests/contrib/test_django_orm.py @@ -74,8 +74,20 @@ class TestCredentialsField(unittest.TestCase): self.assertEquals(self.field.get_internal_type(), 'TextField') def test_field_unpickled(self): - self.assertTrue(isinstance(self.field.to_python(self.pickle_str), - Credentials)) + self.assertTrue( + isinstance(self.field.to_python(self.pickle_str), Credentials)) + + def test_field_already_unpickled(self): + self.assertTrue(isinstance( + self.field.to_python(self.credentials), Credentials)) + + def test_none_field_unpickled(self): + self.assertIsNone(self.field.to_python(None)) + + def test_from_db_value(self): + value = self.field.from_db_value( + self.pickle_str, None, None, None) + self.assertTrue(isinstance(value, Credentials)) def test_field_unpickled_none(self): self.assertEqual(self.field.to_python(None), None) @@ -120,6 +132,18 @@ class TestFlowField(unittest.TestCase): python_val = self.field.to_python(self.pickle_str) self.assertTrue(isinstance(python_val, Flow)) + def test_field_already_unpickled(self): + self.assertTrue( + isinstance(self.field.to_python(self.flow), Flow)) + + def test_none_field_unpickled(self): + self.assertIsNone(self.field.to_python(None)) + + def test_from_db_value(self): + python_val = self.field.from_db_value( + self.pickle_str, None, None, None) + self.assertTrue(isinstance(python_val, Flow)) + def test_field_pickled(self): prep_value = self.field.get_db_prep_value(self.flow, connection=None) self.assertEqual(prep_value, self.pickle_str)