-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
models.py
209 lines (163 loc) · 5.49 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import base64
from social.storage.base import UserMixin, NonceMixin, AssociationMixin, \
CodeMixin, BaseStorage
class BaseModel(object):
@classmethod
def next_id(cls):
cls.NEXT_ID += 1
return cls.NEXT_ID - 1
@classmethod
def get(cls, key):
return cls.cache.get(key)
@classmethod
def reset_cache(cls):
cls.cache = {}
class User(BaseModel):
NEXT_ID = 1
cache = {}
_is_active = True
def __init__(self, username, email=None, **extra_user_fields):
self.id = User.next_id()
self.username = username
self.email = email
self.password = None
self.slug = None
self.social = []
self.extra_data = {}
self.extra_user_fields = extra_user_fields
self.save()
def is_active(self):
return self._is_active
@classmethod
def set_active(cls, is_active=True):
cls._is_active = is_active
def set_password(self, password):
self.password = password
def save(self):
User.cache[self.username] = self
class TestUserSocialAuth(UserMixin, BaseModel):
NEXT_ID = 1
cache = {}
cache_by_uid = {}
def __init__(self, user, provider, uid, extra_data=None):
self.id = TestUserSocialAuth.next_id()
self.user = user
self.provider = provider
self.uid = uid
self.extra_data = extra_data or {}
self.user.social.append(self)
TestUserSocialAuth.cache_by_uid[uid] = self
def save(self):
pass
@classmethod
def reset_cache(cls):
cls.cache = {}
cls.cache_by_uid = {}
@classmethod
def changed(cls, user):
pass
@classmethod
def get_username(cls, user):
return user.username
@classmethod
def user_model(cls):
return User
@classmethod
def username_max_length(cls):
return 1024
@classmethod
def allowed_to_disconnect(cls, user, backend_name, association_id=None):
return user.password or len(user.social) > 1
@classmethod
def disconnect(cls, entry):
cls.cache.pop(entry.id, None)
entry.user.social = [s for s in entry.user.social if entry != s]
@classmethod
def user_exists(cls, username):
return User.cache.get(username) is not None
@classmethod
def create_user(cls, username, email=None, **extra_user_fields):
return User(username=username, email=email, **extra_user_fields)
@classmethod
def get_user(cls, pk):
for username, user in User.cache.items():
if user.id == pk:
return user
@classmethod
def get_social_auth(cls, provider, uid):
social_user = cls.cache_by_uid.get(uid)
if social_user and social_user.provider == provider:
return social_user
@classmethod
def get_social_auth_for_user(cls, user, provider=None, id=None):
return [usa for usa in user.social
if provider in (None, usa.provider) and
id in (None, usa.id)]
@classmethod
def create_social_auth(cls, user, uid, provider):
return cls(user=user, provider=provider, uid=uid)
@classmethod
def get_users_by_email(cls, email):
return [user for user in User.cache.values() if user.email == email]
class TestNonce(NonceMixin, BaseModel):
NEXT_ID = 1
cache = {}
def __init__(self, server_url, timestamp, salt):
self.id = TestNonce.next_id()
self.server_url = server_url
self.timestamp = timestamp
self.salt = salt
@classmethod
def use(cls, server_url, timestamp, salt):
nonce = TestNonce(server_url, timestamp, salt)
TestNonce.cache[server_url] = nonce
return nonce
class TestAssociation(AssociationMixin, BaseModel):
NEXT_ID = 1
cache = {}
def __init__(self, server_url, handle):
self.id = TestAssociation.next_id()
self.server_url = server_url
self.handle = handle
def save(self):
TestAssociation.cache[(self.server_url, self.handle)] = self
@classmethod
def store(cls, server_url, association):
assoc = TestAssociation.cache.get((server_url, association.handle))
if assoc is None:
assoc = TestAssociation(server_url=server_url,
handle=association.handle)
assoc.secret = base64.encodestring(association.secret)
assoc.issued = association.issued
assoc.lifetime = association.lifetime
assoc.assoc_type = association.assoc_type
assoc.save()
@classmethod
def get(cls, server_url=None, handle=None):
result = []
for assoc in TestAssociation.cache.values():
if server_url and assoc.server_url != server_url:
continue
if handle and assoc.handle != handle:
continue
result.append(assoc)
return result
@classmethod
def remove(cls, ids_to_delete):
assoc = filter(lambda a: a.id in ids_to_delete,
TestAssociation.cache.values())
for a in list(assoc):
TestAssociation.cache.pop((a.server_url, a.handle), None)
class TestCode(CodeMixin, BaseModel):
NEXT_ID = 1
cache = {}
@classmethod
def get_code(cls, code):
for c in cls.cache.values():
if c.code == code:
return c
class TestStorage(BaseStorage):
user = TestUserSocialAuth
nonce = TestNonce
association = TestAssociation
code = TestCode