-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
298 lines (231 loc) · 8.64 KB
/
handlers.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# -*- coding: utf8 -*-
import os
import json
from mongoengine import DoesNotExist
from tornado.web import RequestHandler, authenticated, HTTPError
from tornado import gen
from auth import QQOAuth2Mixin
from utils import ComplexEncoder
from models import *
__all__ = (
'ArchivedCardsHandler', 'ArchivedListsHandler', 'AttachmentHandler',
'ClosedBoardsHandler', 'InvitedBoardsHandler', 'MyBoardsHandler',
'Http404Handler', 'LoginHandler', 'LogoutHandler', 'MainHandler',
'MyCardsHandler', 'NewBoardHandler', 'OrderCardHandler',
'PublicBoardsHandler', 'QQLoginHandler', 'SingleBoardHandler',
'SingleCardHandler', 'StandaloneHandler', 'WelcomeHandler',
)
class BaseHandler(RequestHandler):
""" Abstruct RequestHandler for all others """
def get_current_user(self):
user_id = self.get_cookie("oid")
try:
if user_id:
user = User.objects.get(id=user_id)
setattr(self, 'user', user)
if user.isFirstLogin:
self.redirect('/welcome')
user.isFirstLogin = False
user.save()
return user
except DoesNotExist:
pass
return None
def set_current_user(self, user):
if user:
self.set_cookie("oid", user._id)
else:
self.clear_cookie("oid")
def render(self, *args, **kwargs):
if self.current_user:
kwargs['user'] = self.current_user
kwargs['debug'] = self.settings.get('debug', False)
super(BaseHandler, self).render(*args, **kwargs)
def json(self, data):
self.set_header("Content-Type", "application/json; charset=UTF-8")
self.finish(json.dumps(data, cls=ComplexEncoder))
class QQLoginHandler(BaseHandler, QQOAuth2Mixin):
""" QQ Oauth2 login handler """
@gen.coroutine
def get(self):
redirect_uri = 'http://cantas.chifruit.com/auth/qq'
if self.get_argument('code', None):
qq_user = yield self.get_authenticated_user(
redirect_uri=redirect_uri,
client_id=self.settings['qq_oauth']['key'],
client_secret=self.settings['qq_oauth']['secret'],
code=self.get_argument('code'))
try:
user = User.objects.get(openId=qq_user['openid'])
except DoesNotExist:
user = User.objects.create(
username=qq_user['nickname'],
openId=qq_user['openid']
)
self.set_current_user(user)
self.redirect('/')
else:
yield self.authorize_redirect(
client_id=self.settings['qq_oauth']['key'],
redirect_uri=redirect_uri
)
class LoginHandler(BaseHandler):
""" email/password login handler, used only in dev """
def get(self):
self.render('login.html', message='')
def post(self):
username = self.get_argument("username", "")
password = self.get_argument("password", "")
if self.check_permission(password, username):
_email = '[email protected]'
try:
user = User.objects.get(email=_email)
except DoesNotExist:
user = User.objects.create(
username=username,
password=password,
email=_email
)
self.set_current_user(user)
self.redirect('/')
else:
self.redirect('/login')
def check_permission(self, password, username):
return username == password == "admin"
class LogoutHandler(BaseHandler):
""" logout handler """
def get(self, *args, **kwargs):
self.clear_all_cookies()
self.redirect('/')
class MainHandler(BaseHandler):
""" index page handler """
@authenticated
def get(self, *args, **kwargs):
self.render('application.html')
class BoardsHandler(BaseHandler):
""" base handler for those return a list of boards """
@authenticated
def get(self, *args, **kwargs):
self.json(self.get_boards().values())
def get_boards(self):
raise NotImplemented
class MyBoardsHandler(BoardsHandler):
""" get my boards and invited boards """
def get_boards(self):
return Board.objects(
creatorId=self.user.id, isClosed=False
) + BoardMemberRelation.get_invited_boards_by_member(self.user.id)
class PublicBoardsHandler(BoardsHandler):
""" get all public boards """
def get_boards(self):
return Board.objects(isClosed=False,
isPublic=True).order_by('updated')
class ClosedBoardsHandler(BoardsHandler):
""" get my closed boards """
def get_boards(self):
return Board.objects(creatorId=self.user.id,
isClosed=True).order_by('updated')
class InvitedBoardsHandler(BoardsHandler):
""" get boards inviting me """
def get_boards(self):
return BoardMemberRelation.get_invited_boards_by_member(self.user.id)
class NewBoardHandler(BaseHandler):
""" new board handler """
@authenticated
def get(self, *args, **kwargs):
user = self.user
board = Board.create_default(creator_id=user.id)
activity_data = {
'content': "This board is created by %s" % user.username,
'creatorId': user.id,
'boardId': board.id
}
Activity.objects.create(**activity_data)
BoardMemberRelation.objects.create(
boardId=board.id,
userId=user.id,
)
self.json({'boardId': board.id})
class MyCardsHandler(BaseHandler):
""" get all my cards """
@authenticated
def get(self, *args, **kwargs):
cards = Card.objects(isArchived=False, creatorId=self.user.id).values()
for card in cards:
card['boardId'] = card.boardId.to_dict()
card['listId'] = card.listId.to_dict()
self.json(cards)
class ArchivedCardsHandler(BaseHandler):
""" get archived cards of given board """
@authenticated
def get(self, board_id, *args, **kwargs):
self.json(Card.objects(isArchived=True, boardId=board_id).values())
class ArchivedListsHandler(BaseHandler):
""" get archived lists of given board """
@authenticated
def get(self, board_id, *args, **kwargs):
self.json(List.objects(isArchived=True, boardId=board_id).values())
class OrderCardHandler(BaseHandler):
""" get unarchived cards of given board """
@authenticated
def get(self, list_id, *args, **kwargs):
self.json(Card.objects(listId=list_id, isArchived=False).values())
class SingleBoardHandler(BaseHandler):
@authenticated
def get(self, board_id, *args, **kwargs):
try:
Board.objects.get(id=board_id)
self.render('application.html')
except:
raise HTTPError(404)
class SingleCardHandler(BaseHandler):
@authenticated
def get(self, card_id, *args, **kwargs):
try:
Card.objects.get(id=card_id)
self.render('application.html')
except:
raise HTTPError(404)
class AttachmentHandler(BaseHandler):
attachment_dir = os.path.join(os.path.dirname(__file__),
'static/attachments')
@authenticated
def get(self, *args, **kwargs):
"""handle downloading attachment"""
pass
@authenticated
def post(self, card_id, *args, **kwargs):
"""handle uploading attachment"""
# TODO: generate thumb img
img = self.request.files['attachment'][0]
file_name, extension = os.path.splitext(img['filename'])
file_name += extension
card_dir = os.path.join(self.attachment_dir, card_id)
try:
os.mkdir(card_dir)
except OSError:
pass
with open(os.path.join(card_dir, file_name), 'w') as output_file:
output_file.write(img['body'])
size = os.path.getsize(output_file.name)
attachment_data = dict(
cardId=card_id,
uploaderId=self.user.id,
name=file_name,
size=size,
fileType='picture',
path=file_name,
cardThumbPath=file_name,
cardDetailThumbPath=file_name
)
self.json({'attachment': attachment_data})
class StandaloneHandler(BaseHandler):
def get(self, *args, **kwargs):
self.render('standalone-help.html')
class WelcomeHandler(BaseHandler):
@authenticated
def get(self, *args, **kwargs):
self.render('application.html')
class Http404Handler(BaseHandler):
def get(self, *args, **kwargs):
self.render('404.html')