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
11 additions
and
6 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,6 +21,7 @@ | |
import oauth2client | ||
import base64 | ||
import pickle | ||
import six | ||
|
||
from django.db import models | ||
from oauth2client.client import Storage as BaseStorage | ||
|
@@ -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: | ||
|
@@ -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): | ||
|
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