-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
319 lines (273 loc) · 9.99 KB
/
app.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import datetime
import logging
import os
import re
import html5lib
import tornado.web
import tornado.wsgi
from tornado.web import url
from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.api import users
from google.appengine.ext import blobstore
from google.appengine.ext import db
from google.appengine.ext import deferred
from google.appengine.ext.webapp.util import run_wsgi_app
import forms
import models
import tasks
import uimodules
# Constants
IS_DEV = os.environ['SERVER_SOFTWARE'].startswith('Dev') # Development server
class Application(tornado.wsgi.WSGIApplication):
def __init__(self):
handlers = [
url(r'/', IndexHandler, name='index'),
url(r'/mine', HomeHandler, name='home'),
url(r'/new', NewBookmarkHandler, name='new_bookmark'),
url(r'/bookmarks/([^/]+)', ListBookmarksHandler, name='list'),
url(r'/edit', EditBookmarkHandler, name='edit'),
url(r'/update', UpdateBookmarkHandler, name='update'),
(r'/upload', UploadHandler),
(r'/later', ReadLaterHandler),
(r'/autocomplete', AutocompleteHandler),
# Task handlers
(r'/tasks/create_compute_tags', CreateComputeTagsTasksHandler),
(r'/tasks/create_check_bookmarks', CreateCheckBookmarksTasksHandler),
]
settings = dict(
debug=IS_DEV,
template_path=os.path.join(os.path.dirname(__file__), 'templates'),
xsrf_cookies=True,
cookie_secret="zxccczxi123ijasdj9123asjdzcnjjl0j123jas9d0123asd",
ui_modules=uimodules,
)
tornado.wsgi.WSGIApplication.__init__(self, handlers, **settings)
class BaseHandler(tornado.web.RequestHandler):
# I don't know why
def initialize(self):
self.xsrf_token
def get_current_user(self):
user = users.get_current_user()
if user:
user.admin = users.is_current_user_admin()
account = models.Account.get_account_for_user(user)
self.current_account = account
return user
def get_login_url(self):
return users.create_login_url(self.request.uri)
def render_string(self, template, **kwds):
return tornado.web.RequestHandler.render_string(
self, template, users=users, IS_DEV=IS_DEV,
current_account=getattr(self, 'current_account', None),
**kwds)
def get_integer(self, name, default, min_value=None, max_value=None):
value = self.get_argument(name, '')
if not isinstance(value, (int, long)):
try:
value = int(value)
except (TypeError, ValueError), err:
value = default
if min_value is not None:
value = max(min_value, value)
if max_value is not None:
value = min(value, max_value)
return value
class IndexHandler(BaseHandler):
def get(self):
self.render('index.html')
class HomeHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
if self.current_account.fresh:
self.current_account.fresh = False
self.current_account.put()
self.render('fresh.html')
return
query = models.Bookmark.all() \
.filter('account =', self.current_account) \
.order('-created')
tag = self.get_arguments('tag', None)
if tag is not None:
tag = tag[:2]
for tag in tag:
query = query.filter('tags =', tag)
offset = self.get_integer('offset', 0, 0)
limit = self.get_integer('limit', 25, 1, 100)
bookmarks = query.fetch(limit + 1, offset)
tags = self.current_account.get_popular_tags(20)
self.render('list.html', bookmarks=bookmarks, tags=tags)
class ListBookmarksHandler(BaseHandler):
def get(self, nickname):
account = models.Account.get_account_for_nickname(nickname)
if account is None:
raise tornado.web.HTTPError(404)
if self.current_user and account.key() == self.current_account.key():
query = models.Bookmark.all().filter('account =', self.current_account)
else:
query = models.Bookmark.all() \
.filter('account =', account) \
.filter('is_private =', False)
query = query.order('-created')
# Pagination
offset = self.get_integer('offset', 0, 0)
limit = self.get_integer('limit', 25, 1, 100)
params = {
'limit': limit,
'first': offset + 1,
}
bookmarks = query.fetch(limit + 1, offset)
tags = account.get_popular_tags(20)
self.render('list.html', bookmarks=bookmarks, tags=tags)
class NewBookmarkHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
# Check if popup
is_popup = self.get_argument('p', None) == '1'
is_unread = self.get_argument('unread', None) == '1'
if is_popup:
bookmark = self.current_account.get_bookmark_for_uri(
self.get_argument('uri'))
if bookmark is None:
form = forms.BookmarkForm(self)
else:
self.redirect(self.reverse_url('edit') +
'?&p=1&id=' + bookmark.uri_digest +
'&description=' + self.get_argument('description', ''))
return
else:
form = forms.BookmarkForm()
self.render('bookmark-form.html', form=form, is_popup=is_popup)
@tornado.web.authenticated
def post(self):
is_popup = self.get_argument('p', None) == '1'
form = forms.BookmarkForm(self)
if form.validate():
account = self.current_account
account_key_name = account.key().name()
uri_digest = models.Bookmark.get_digest_for_uri(form.uri.data)
key = '%s:%s' % (account_key_name, uri_digest)
bookmark = models.Bookmark(
key_name=key,
account=self.current_account,
uri_digest=uri_digest,
**form.data)
bookmark.put()
if is_popup:
self.write('<script>window.close()</script>')
else:
self.redirect(self.reverse_url('home'))
else:
self.render('bookmark-form.html', form=form)
class ReadLaterHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
bookmark = self.current_account.get_bookmark_for_uri(
self.get_argument('uri'))
if bookmark is None:
bookmark = models.Bookmark(uri=self.get_argument('uri'))
bookmark.account = self.current_account
bookmark.uri_digest = models.Bookmark.get_digest_for_uri(bookmark.uri)
bookmark.title = self.get_argument('title', bookmark.uri)
bookmark.description = self.get_argument('description', '')
bookmark.key_name = '%s:%s' % (self.current_account.key().name(),
bookmark.uri_digest)
bookmark.is_unread = True
bookmark.put()
self.write('<script>window.blur();window.close()</script>')
class EditBookmarkHandler(BaseHandler):
def get(self):
form = forms.BookmarkForm(obj=self.bookmark)
form.description.data = self.get_argument(
'description', self.bookmark.description)
self.render('bookmark-form.html', form=form)
def post(self):
form = forms.BookmarkForm(self, obj=self.bookmark)
if form.validate():
form.populate_obj(self.bookmark)
self.bookmark.put()
if self.get_argument('p', None):
self.write('<script>window.close()</script>')
else:
self.render('module-bookmark.html', bookmark=self.bookmark)
else:
self.render('bookmark-form.html', form=form)
@tornado.web.authenticated
def prepare(self):
id = self.get_argument('id')
bookmark = self.current_account.get_bookmark_for_digest(id)
if bookmark is None:
raise tornado.web.HTTPError(404)
if bookmark.account.key() != self.current_account.key():
raise tornado.web.HTTPError(403)
self.bookmark = bookmark
class UploadHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render('upload.html',
upload_url=blobstore.create_upload_url('/upload'))
@tornado.web.authenticated
def post(self):
if IS_DEV:
blob_key = re.findall(r'blob-key="*(\S+)"', self.request.body)[0]
else:
blob_key = re.findall(r'blob-key=(.+)', self.request.body)[0]
new_import = models.Import(account=self.current_account,
blob=blob_key)
new_import.put()
deferred.defer(tasks.ImportBookmarks, new_import.key())
self.redirect(self.reverse_url('home'))
class UpdateBookmarkHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
id = self.get_argument('id')
action = self.get_argument('action')
bookmark = self.current_account.get_bookmark_for_digest(id)
if bookmark is None:
raise tornado.web.HTTPError(404)
if bookmark.account.key() != self.current_account.key():
raise tornado.web.HTTPError(403)
if action == 'star':
bookmark.is_starred = True
elif action == 'unstar':
bookmark.is_starred = False
elif action == 'read':
bookmark.is_unread = False
elif action == 'unread':
bookmark.is_unread = True
bookmark.put()
self.render('module-bookmark.html', bookmark=bookmark)
class AutocompleteHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
q = self.get_argument('q').strip()
if len(q) < 2:
self.finish()
return
tags_cache_key = "%s:tags" % self.current_account.key()
tags = memcache.get(tags_cache_key)
if tags is None:
# TODO What if user has got 1000's of tags?
tags = set([
tag.name
for tag in models.Tag.all().filter('account =', self.current_account)])
if not memcache.add(tags_cache_key, tags):
logging.error("Cannot set account tags in memcache")
records = [tag for tag in tags if tag.startswith(q)]
self.write(dict(records=records))
# Cron handlers
class BaseTaskHandler(BaseHandler):
def initialize(self):
self.application.settings['xsrf_cookies'] = False
class CreateComputeTagsTasksHandler(BaseTaskHandler):
def get(self):
for account in models.Account.all():
deferred.defer(tasks.ComputeTagCounts, account.key())
class CreateCheckBookmarksTasksHandler(BaseTaskHandler):
def get(self):
for account in models.Account.all():
deferred.defer(tasks.CheckBookmarks, account.key())
def main():
run_wsgi_app(Application())
if __name__ == '__main__':
main()