-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser.py
55 lines (42 loc) · 1.24 KB
/
user.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
import email.mime.text
import uuid
import multiprocessing.pool
import tornado.gen
import settings
class User(object):
collection = settings.db['users']
def __init__(self, login, **kwargs):
self.id = kwargs.get('_id')
self.login = login
self.data = kwargs.get('data')
self.token = kwargs.get('token', str(uuid.uuid4()))
self.gh_token = kwargs.get('gh_token')
def __lt__(self, other):
return self.login < other.login
@property
def admin(self):
return self.login in settings.ADMINS
@classmethod
@tornado.gen.coroutine
def get(cls, token=None, login=None):
query = {}
if token:
query['token'] = token
if login:
query['login'] = login
if not query:
return None
user = yield User.collection.find_one(query)
return cls(**user) if user else None
@tornado.gen.coroutine
def put(self):
saved = self.id is not None
user = {
'login': self.login,
'data': self.data,
'token': self.token,
'gh_token': self.gh_token,
}
if saved:
user['_id'] = self.id
self.id = yield User.collection.save(user)