forked from googleapis/oauth2client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue googleapis#142 CredentialField Python3 fix
- Loading branch information
Showing
2 changed files
with
50 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
"""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): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.assertIsNone(self.field.to_python(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.assertIsNone(value_str) | ||
|
||
|
||
class TestFlowField(unittest.TestCase): | ||
|