Merge pull request #676 from chripede/django-jsonpickle
Use jsonpickle in django contrib
This commit is contained in:
@@ -37,6 +37,7 @@ class CommunicationError(Error):
|
|||||||
class NoDevshellServer(Error):
|
class NoDevshellServer(Error):
|
||||||
"""Error when no Developer Shell server can be contacted."""
|
"""Error when no Developer Shell server can be contacted."""
|
||||||
|
|
||||||
|
|
||||||
# The request for credential information to the Developer Shell client socket
|
# The request for credential information to the Developer Shell client socket
|
||||||
# is always an empty PBLite-formatted JSON object, so just define it as a
|
# is always an empty PBLite-formatted JSON object, so just define it as a
|
||||||
# constant.
|
# constant.
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import pickle
|
|||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import encoding
|
from django.utils import encoding
|
||||||
|
import jsonpickle
|
||||||
|
|
||||||
import oauth2client
|
import oauth2client
|
||||||
|
|
||||||
@@ -48,7 +49,12 @@ class CredentialsField(models.Field):
|
|||||||
elif isinstance(value, oauth2client.client.Credentials):
|
elif isinstance(value, oauth2client.client.Credentials):
|
||||||
return value
|
return value
|
||||||
else:
|
else:
|
||||||
return pickle.loads(base64.b64decode(encoding.smart_bytes(value)))
|
try:
|
||||||
|
return jsonpickle.decode(
|
||||||
|
base64.b64decode(encoding.smart_bytes(value)).decode())
|
||||||
|
except ValueError:
|
||||||
|
return pickle.loads(
|
||||||
|
base64.b64decode(encoding.smart_bytes(value)))
|
||||||
|
|
||||||
def get_prep_value(self, value):
|
def get_prep_value(self, value):
|
||||||
"""Overrides ``models.Field`` method. This is used to convert
|
"""Overrides ``models.Field`` method. This is used to convert
|
||||||
@@ -58,7 +64,8 @@ class CredentialsField(models.Field):
|
|||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return encoding.smart_text(base64.b64encode(pickle.dumps(value)))
|
return encoding.smart_text(
|
||||||
|
base64.b64encode(jsonpickle.encode(value).encode()))
|
||||||
|
|
||||||
def value_to_string(self, obj):
|
def value_to_string(self, obj):
|
||||||
"""Convert the field value from the provided model to a string.
|
"""Convert the field value from the provided model to a string.
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ def _CreateArgumentParser():
|
|||||||
help='Set the logging level of detail.')
|
help='Set the logging level of detail.')
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
# argparser is an ArgumentParser that contains command-line options expected
|
# argparser is an ArgumentParser that contains command-line options expected
|
||||||
# by tools.run(). Pass it in as part of the 'parents' argument to your own
|
# by tools.run(). Pass it in as part of the 'parents' argument to your own
|
||||||
# ArgumentParser.
|
# ArgumentParser.
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ import base64
|
|||||||
import pickle
|
import pickle
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import jsonpickle
|
||||||
|
|
||||||
from oauth2client import _helpers
|
from oauth2client import _helpers
|
||||||
from oauth2client import client
|
from oauth2client import client
|
||||||
from oauth2client.contrib.django_util import models
|
from oauth2client.contrib.django_util import models
|
||||||
@@ -36,6 +38,8 @@ class TestCredentialsField(unittest.TestCase):
|
|||||||
self.credentials = client.Credentials()
|
self.credentials = client.Credentials()
|
||||||
self.pickle_str = _helpers._from_bytes(
|
self.pickle_str = _helpers._from_bytes(
|
||||||
base64.b64encode(pickle.dumps(self.credentials)))
|
base64.b64encode(pickle.dumps(self.credentials)))
|
||||||
|
self.jsonpickle_str = _helpers._from_bytes(
|
||||||
|
base64.b64encode(jsonpickle.encode(self.credentials).encode()))
|
||||||
|
|
||||||
def test_field_is_text(self):
|
def test_field_is_text(self):
|
||||||
self.assertEqual(self.field.get_internal_type(), 'BinaryField')
|
self.assertEqual(self.field.get_internal_type(), 'BinaryField')
|
||||||
@@ -44,6 +48,10 @@ class TestCredentialsField(unittest.TestCase):
|
|||||||
self.assertIsInstance(
|
self.assertIsInstance(
|
||||||
self.field.to_python(self.pickle_str), client.Credentials)
|
self.field.to_python(self.pickle_str), client.Credentials)
|
||||||
|
|
||||||
|
def test_field_jsonunpickled(self):
|
||||||
|
self.assertIsInstance(
|
||||||
|
self.field.to_python(self.jsonpickle_str), client.Credentials)
|
||||||
|
|
||||||
def test_field_already_unpickled(self):
|
def test_field_already_unpickled(self):
|
||||||
self.assertIsInstance(
|
self.assertIsInstance(
|
||||||
self.field.to_python(self.credentials), client.Credentials)
|
self.field.to_python(self.credentials), client.Credentials)
|
||||||
@@ -62,12 +70,12 @@ class TestCredentialsField(unittest.TestCase):
|
|||||||
def test_field_pickled(self):
|
def test_field_pickled(self):
|
||||||
prep_value = self.field.get_db_prep_value(self.credentials,
|
prep_value = self.field.get_db_prep_value(self.credentials,
|
||||||
connection=None)
|
connection=None)
|
||||||
self.assertEqual(prep_value, self.pickle_str)
|
self.assertEqual(prep_value, self.jsonpickle_str)
|
||||||
|
|
||||||
def test_field_value_to_string(self):
|
def test_field_value_to_string(self):
|
||||||
self.fake_model.credentials = self.credentials
|
self.fake_model.credentials = self.credentials
|
||||||
value_str = self.fake_model_field.value_to_string(self.fake_model)
|
value_str = self.fake_model_field.value_to_string(self.fake_model)
|
||||||
self.assertEqual(value_str, self.pickle_str)
|
self.assertEqual(value_str, self.jsonpickle_str)
|
||||||
|
|
||||||
def test_field_value_to_string_none(self):
|
def test_field_value_to_string_none(self):
|
||||||
self.fake_model.credentials = None
|
self.fake_model.credentials = None
|
||||||
|
|||||||
@@ -160,8 +160,8 @@ class _AuthReferenceServer(threading.Thread):
|
|||||||
s.recv(to_read, socket.MSG_WAITALL))
|
s.recv(to_read, socket.MSG_WAITALL))
|
||||||
if resp_buffer != devshell.CREDENTIAL_INFO_REQUEST_JSON:
|
if resp_buffer != devshell.CREDENTIAL_INFO_REQUEST_JSON:
|
||||||
self.bad_request = True
|
self.bad_request = True
|
||||||
l = len(self.response)
|
response_len = len(self.response)
|
||||||
s.sendall('{0}\n{1}'.format(l, self.response).encode())
|
s.sendall('{0}\n{1}'.format(response_len, self.response).encode())
|
||||||
finally:
|
finally:
|
||||||
# Will fail if s is None, but these tests never encounter
|
# Will fail if s is None, but these tests never encounter
|
||||||
# that scenario.
|
# that scenario.
|
||||||
|
|||||||
@@ -369,6 +369,7 @@ class ServiceAccountCredentialsTests(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(credentials.access_token, token2)
|
self.assertEqual(credentials.access_token, token2)
|
||||||
|
|
||||||
|
|
||||||
TOKEN_LIFE = service_account._JWTAccessCredentials._MAX_TOKEN_LIFETIME_SECS
|
TOKEN_LIFE = service_account._JWTAccessCredentials._MAX_TOKEN_LIFETIME_SECS
|
||||||
T1 = 42
|
T1 = 42
|
||||||
T1_DATE = datetime.datetime(1970, 1, 1, second=T1)
|
T1_DATE = datetime.datetime(1970, 1, 1, second=T1)
|
||||||
|
|||||||
Reference in New Issue
Block a user