-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
264 lines (233 loc) · 8.68 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
# -*- coding: utf-8 -*-
#
# Author: jimin.huang
#
# Created Time: 2015年03月05日 星期四 09时49分35秒
#
'''
Handlers dealing with requests
处理请求的handlers
'''
import tornado.web
import tornado.template
import handlers
import logging
import database
import gen
import json
class BaseHandler(tornado.web.RequestHandler):
'''
base handler, the base class of other handlers
handle the requests which do not be matched by any rules in application
which raise an 404 error
基础handler,是其他handler的基类,将404错误即未被规则匹配的请求重定向到404网页
'''
def get(self):
self.write_error(404)
def write_error(self, status_code, **kwargs):
self.render('404.html', title="404")
class SafeHandler(BaseHandler):
'''
This handler is designed to define the get_current_user method
which is the default method called by methods
decorated by @tornado.web.authenticated
handler 用来定义get_current_user方法,该方法在其他方法请求检查用户是否登陆时被调用
'''
def get_current_user(self):
return self.get_secure_cookie("user_id")
class RegisterHandler(BaseHandler):
def get(self):
self.render(
"register.html",
title="企业信息服务平台-用户注册",
models=database.Model.selectall(),
)
def post(self):
user_name = self.get_argument("username")
#检查用户名是否存在
if not database.User.check(user_name):
self.write("用户名已存在!")
else:
password = self.get_argument("password")
user_model = self.get_argument("model")
keywords = self.get_argument("keyword")
user_id = database.User.new(user_name, password, user_model)
if keywords:
keywords = keywords.split(';')
for keyword in keywords:
database.Keyword.new(keyword, user_id)
gen.calculate(user_id).next()
self.write("Success")
class LoginHandler(BaseHandler):
def get(self):
self.render(
"login.html",
title="企业信息服务平台-用户登录",
)
def post(self):
user_name = self.get_argument('username')
user_password = self.get_argument('password')
if not database.User.login(user_name, user_password):
#密码错误
logging.error('密码错误')
self.redirect('/login')
else:
logging.error('密码无错误')
remember_me = self.get_argument('remember', None)
expires = 7 #cookie过期时间
if remember_me is None:
expires = None
self.set_secure_cookie(
'user_id',
str(database.User.select(user_name=user_name).user_id),
expires_days=expires,
)
self.redirect('/search')
class SearchHandler(SafeHandler):
@tornado.web.authenticated
def get(self):
self.render(
"search.html",
title="企业信息服务平台-搜索"
)
@tornado.web.authenticated
def post(self):
search_word = json.dumps(self.get_argument('keyword').split())
search_id = database.Search.new(search_word)
redirect_url =\
(
'/list/search?'
'search_id={search_id}'
).format(search_id=search_id)
self.redirect(redirect_url)
class ListHandler(SafeHandler):
list_actives = {
'search': 2,
'collected': 4,
}
@tornado.web.authenticated
def get(self, list_type):
user_id = self.get_current_user()
self.render(
"list.html",
title="企业信息服务平台",
active_index=self.list_actives[list_type],
)
@tornado.web.authenticated
def post(self, list_type):
user_id = self.get_secure_cookie('user_id')
page_index = int(self.get_argument('page_index'))
bunch_index = int(self.get_argument('bunch_index'))
page = database.Page(bunch_index=bunch_index, page_index=page_index)
collected_search = user_id
if list_type == 'search':
search_id = self.get_argument('search_id', None)
if not search_id:
data_range = None
else:
search_result = database.Search.select(search_id)
if search_result.search_status == 'Pending':
if not gen.search(search_id).next():
self.send_error(404)
return
search_result = database.Search.select(search_id)
if search_result.search_status != 'Success':
self.send_error(404)
data_range = json.loads(search_result.search_result)
if data_range:
def inter(listA, listB):
return list(set(listA).intersection(set(listB)))
data_range = reduce(inter, data_range.values())
if list_type == 'collected':
data_range = database.Action.selectmany(user_id, database.Action.COLLECT)
if data_range is not None:
data_range = [ action.data_id for action in data_range ]
page = page.paging(user_id, data_range=data_range)
page_data = database.Data.query(page, collected_search=collected_search)
def get_title(data):
data.data_title = json.loads(data.data_content)['title']
data.data_score = "{:.2f}".format(data.data_score)
map(get_title, page_data)
self.write(''.join(
[
self.render_string(
"module/articleItem.html",
article_item=data,
) for data in page_data
]
))
class MainHandler(SafeHandler):
@tornado.web.authenticated
def get(self):
self.redirect("/list/search")
class SettingsHandler(SafeHandler):
@tornado.web.authenticated
def get(self, null):
user_id = self.get_current_user()
user = database.User.select(user_id=user_id)
model = database.Model.select(model_id=user.user_model)
keywords = database.Keyword.select(user_id)
self.render(
"settings.html",
title="企业信息服务平台-用户设置",
user=user,
model=model,
keywords=keywords,
active_index=5,
)
@tornado.web.authenticated
def post(self, string):
keyword = self.get_argument('keyword')
user_id = self.get_current_user()
if string == '/delete_keyword':
database.Keyword.delete(keyword, user_id)
gen.calculate(user_id).next()
self.write("Success")
if string == '/new_keyword':
keywords = database.Keyword.select(user_id)
keywords = [ value["keyword"] for value in keywords ]
if keyword in keywords:
self.write("Error")
else:
database.Keyword.new(keyword, user_id)
gen.calculate(user_id).next()
self.write("Success")
class LogoutHandler(SafeHandler):
@tornado.web.authenticated
def post(self):
self.clear_all_cookies()
class ArticleHandler(SafeHandler):
@tornado.web.authenticated
def get(self, data_id):
user_id = self.get_current_user()
all_data = database.Data.select(data_id)
if all_data is None:
self.raise_error(404)
data = json.loads(all_data.data_content)
data['data_id'] = all_data.data_id
data["data_score"] = self.get_argument('data_score')
ifcollected =\
database.Action.check(
data_id,
user_id,
action_score=database.Action.COLLECT,
)
self.render(
'article.html',
title=data["title"],
article=data,
ifcollected=ifcollected,
active_index=None,
)
class CollectionHandler(SafeHandler):
@tornado.web.authenticated
def post(self):
user_id = self.get_current_user()
ifcollected = False\
if self.get_argument("collected") == 'false' else True
data_id = self.get_argument("data_id")
if self.get_argument("collected") == 'false':
database.Action.new(data_id, user_id, database.Action.COLLECT)
else:
database.Action.delete(data_id, user_id, database.Action.COLLECT)
self.write("success")