Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Issue #142: CredentialField Python 3 fix #316

Merged
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
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):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"""Convert the field value from the provided model to a string.
Used during model serialization.

Args:
obj: db.Model, model object
Returns:
string, the serialized field value
"""
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)

This comment was marked as spam.

This comment was marked as spam.



class TestCredentialsFieldViaModel(unittest.TestCase):

This comment was marked as spam.


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

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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