-
Notifications
You must be signed in to change notification settings - Fork 430
Issue #142: CredentialField Python 3 fix #316
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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): | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
"""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): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)' | ||
|
||
|
@@ -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.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
|
||
class TestCredentialsFieldViaModel(unittest.TestCase): | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
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.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
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): | ||
|
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.