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 Sep 30, 2015
1 parent af6d3eb commit 1434a74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions oauth2client/django_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import oauth2client
import base64
import pickle
import six

from django.db import models
from oauth2client.client import Storage as BaseStorage
Expand All @@ -29,9 +30,7 @@
__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 +45,18 @@ def to_python(self, value):
return None
if isinstance(value, oauth2client.client.Credentials):
return value
if isinstance(value, six.text_type):
value = value.encode('utf-8')
return pickle.loads(base64.b64decode(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 base64.b64encode(pickle.dumps(value)).decode('utf-8')

def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_prep_value(value)


class FlowField(models.Field):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_django_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_field_unpickled(self):
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(prep_value.encode('utf-8'), self.pickle)


class TestFlowField(unittest.TestCase):
Expand Down

0 comments on commit 1434a74

Please sign in to comment.