Skip to content

Commit

Permalink
Issue googleapis#142 CredentialField Python3 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
apragacz committed Oct 4, 2015
1 parent af6d3eb commit 35fcc85
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
24 changes: 18 additions & 6 deletions oauth2client/django_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
import oauth2client
import base64
import pickle
import six

from django.db import models
from django.utils.encoding import smart_bytes, smart_text
from oauth2client.client import Storage as BaseStorage


__author__ = '[email protected] (Joe Gregorio)'


class CredentialsField(models.Field):

__metaclass__ = models.SubfieldBase
class CredentialsField(six.with_metaclass(models.SubfieldBase, models.Field)):

def __init__(self, *args, **kwargs):
if 'null' not in kwargs:
Expand All @@ -46,12 +46,24 @@ def to_python(self, value):
return None
if isinstance(value, oauth2client.client.Credentials):
return value
return pickle.loads(base64.b64decode(value))
return pickle.loads(base64.b64decode(smart_bytes(value)))

def get_db_prep_value(self, value, connection, prepared=False):
def get_prep_value(self, value):
if value is None:
return None
return base64.b64encode(pickle.dumps(value))
return smart_text(base64.b64encode(pickle.dumps(value)))

def value_to_string(self, obj):
"""Convert the field value from the provided model to a string.
Used during model serialization.
Args:
obj: db.Model, model object
Returns:
six.text_type
"""
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)


class FlowField(models.Field):
Expand Down
34 changes: 32 additions & 2 deletions tests/test_django_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
sys.modules['django_settings'] = django_settings = imp.new_module(
'django_settings')
django_settings.SECRET_KEY = 'xyzzy'
from django.db import models

from oauth2client.django_orm import CredentialsField
from oauth2client.django_orm import FlowField

from oauth2client._helpers import _from_bytes, _to_bytes

__author__ = '[email protected] (Conley Owens)'

Expand All @@ -58,10 +59,39 @@ def test_field_unpickled(self):
self.assertTrue(isinstance(self.field.to_python(self.pickle),
Credentials))

def test_field_unpickled_none(self):
self.assertEqual(self.field.to_python(None), None)

def test_field_pickled(self):
prep_value = self.field.get_db_prep_value(self.credentials,
connection=None)
self.assertEqual(prep_value, self.pickle)
self.assertEqual(_to_bytes(prep_value), self.pickle)


class TestCredentialsFieldViaModel(unittest.TestCase):

class TestModel(models.Model):
credentials = CredentialsField()

def setUp(self):
self.model = self.TestModel()
# using the meta api:
# https://docs.djangoproject.com/en/1.8/ref/models/meta/#field-access-api
self.field = self.model._meta.get_field('credentials')
self.credentials = Credentials()
self.pickle_str = _from_bytes(base64.b64encode(pickle.dumps(
self.credentials
)))

def test_field_value_to_string(self):
self.model.credentials = self.credentials
value_str = self.field.value_to_string(self.model)
self.assertEqual(value_str, self.pickle_str)

def test_field_value_to_string_none(self):
self.model.credentials = None
value_str = self.field.value_to_string(self.model)
self.assertEqual(value_str, None)


class TestFlowField(unittest.TestCase):
Expand Down

0 comments on commit 35fcc85

Please sign in to comment.