11import base64
2- import six
32
4- from mongoengine import DictField , IntField , StringField , \
5- EmailField , BooleanField
3+ import six
4+ from mongoengine import BooleanField , DictField , EmailField , IntField , StringField
65from mongoengine .queryset import OperationError
6+ from social_core .storage import (
7+ AssociationMixin ,
8+ BaseStorage ,
9+ CodeMixin ,
10+ NonceMixin ,
11+ PartialMixin ,
12+ UserMixin ,
13+ )
714
8- from social_core .storage import UserMixin , AssociationMixin , NonceMixin , \
9- CodeMixin , PartialMixin , BaseStorage
10-
11-
12- UNUSABLE_PASSWORD = '!' # Borrowed from django 1.4
15+ UNUSABLE_PASSWORD = "!" # Borrowed from django 1.4
1316
1417
1518class MongoengineUserMixin (UserMixin ):
1619 """Social Auth association model"""
20+
1721 user = None
1822 provider = StringField (max_length = 32 )
19- uid = StringField (max_length = 255 , unique_with = ' provider' )
23+ uid = StringField (max_length = 255 , unique_with = " provider" )
2024 extra_data = DictField ()
2125
2226 def str_id (self ):
@@ -45,14 +49,14 @@ def username_max_length(cls):
4549
4650 @classmethod
4751 def username_field (cls ):
48- return getattr (cls .user_model (), ' USERNAME_FIELD' , ' username' )
52+ return getattr (cls .user_model (), " USERNAME_FIELD" , " username" )
4953
5054 @classmethod
5155 def create_user (cls , * args , ** kwargs ):
52- kwargs [' password' ] = UNUSABLE_PASSWORD
53- if ' email' in kwargs :
56+ kwargs [" password" ] = UNUSABLE_PASSWORD
57+ if " email" in kwargs :
5458 # Empty string makes email regex validation fail
55- kwargs [' email' ] = kwargs [' email' ] or None
59+ kwargs [" email" ] = kwargs [" email" ] or None
5660 return cls .user_model ().objects .create (* args , ** kwargs )
5761
5862 @classmethod
@@ -63,7 +67,7 @@ def allowed_to_disconnect(cls, user, backend_name, association_id=None):
6367 qs = cls .objects .filter (provider__ne = backend_name )
6468 qs = qs .filter (user = user )
6569
66- if hasattr (user , ' has_usable_password' ):
70+ if hasattr (user , " has_usable_password" ):
6771 valid_password = user .has_usable_password ()
6872 else :
6973 valid_password = True
@@ -88,8 +92,8 @@ def user_exists(cls, *args, **kwargs):
8892 Return True/False if a User instance exists with the given arguments.
8993 Arguments are directly passed to filter() manager method.
9094 """
91- if ' username' in kwargs :
92- kwargs [cls .username_field ()] = kwargs .pop (' username' )
95+ if " username" in kwargs :
96+ kwargs [cls .username_field ()] = kwargs .pop (" username" )
9397 return cls .user_model ().objects .filter (* args , ** kwargs ).count () > 0
9498
9599 @classmethod
@@ -119,19 +123,21 @@ def get_social_auth(cls, provider, uid):
119123
120124class MongoengineNonceMixin (NonceMixin ):
121125 """One use numbers"""
126+
122127 server_url = StringField (max_length = 255 )
123128 timestamp = IntField ()
124129 salt = StringField (max_length = 40 )
125130
126131 @classmethod
127132 def use (cls , server_url , timestamp , salt ):
128- return cls .objects .get_or_create (server_url = server_url ,
129- timestamp = timestamp ,
130- salt = salt )[1 ]
133+ return cls .objects .get_or_create (
134+ server_url = server_url , timestamp = timestamp , salt = salt
135+ )[1 ]
131136
132137
133138class MongoengineAssociationMixin (AssociationMixin ):
134139 """OpenId account association"""
140+
135141 server_url = StringField (max_length = 255 )
136142 handle = StringField (max_length = 255 )
137143 secret = StringField (max_length = 255 ) # Stored base64 encoded
@@ -143,11 +149,9 @@ class MongoengineAssociationMixin(AssociationMixin):
143149 def store (cls , server_url , association ):
144150 # Don't use get_or_create because issued cannot be null
145151 try :
146- assoc = cls .objects .get (server_url = server_url ,
147- handle = association .handle )
152+ assoc = cls .objects .get (server_url = server_url , handle = association .handle )
148153 except cls .DoesNotExist :
149- assoc = cls (server_url = server_url ,
150- handle = association .handle )
154+ assoc = cls (server_url = server_url , handle = association .handle )
151155 assoc .secret = base64 .encodestring (association .secret ).decode ()
152156 assoc .issued = association .issued
153157 assoc .lifetime = association .lifetime
@@ -206,5 +210,4 @@ class BaseMongoengineStorage(BaseStorage):
206210
207211 @classmethod
208212 def is_integrity_error (cls , exception ):
209- return exception .__class__ is OperationError and \
210- 'E11000' in exception .message
213+ return exception .__class__ is OperationError and "E11000" in exception .message
0 commit comments