-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbs_main.py
229 lines (177 loc) · 6.94 KB
/
bbs_main.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
# encoding: utf-8
import urllib.parse
import os
import tornado.httpserver
import tornado.web
import tornado.ioloop
import tornado.websocket
from tornado.options import define, options
import system
import database
'''
用户认证的基类
'''
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
if not self.get_cookie("_xsrf"):
self.clear_all_cookies()
return None
username = self.get_secure_cookie("name")
if username:
return username.decode()
return None
class MainPageHandler(tornado.web.RequestHandler):
def get(self):
self.render('main.html', parts=parts,
page={'title': 'welcome to BMBBS!'})
class PartHandler(BaseHandler):
def get(self):
partname = urllib.parse.unquote(self.get_argument('partname'))
notice = system.Part.get_notice(partname)
topics = system.Part.load_topics(partname)
self.render('part.html', parts=parts, topics=topics, notice=notice,
page={'title': partname})
@tornado.web.authenticated
def post(self):
user = self.current_user
partname = urllib.parse.unquote(self.get_argument('partname'))
title = self.get_argument("title")
content = self.get_argument('content')
system.Part.new_topic(partname, user, title, content)
self.write("succeed")
class TopicHelper(object):
callbacks = []
def register(self, callback):
self.callbacks.append(callback)
def unregister(self, callback):
self.callbacks.remove(callback)
def add_comment(self, new_comment):
for callback in self.callbacks:
callback(new_comment)
class TopicWebSocket(tornado.websocket.WebSocketHandler):
#users =user()
def open(self):
self.application.topic_helper.register(self.callback)
def on_close(self):
self.application.topic_helper.unregister(self.callback)
def on_message(self):
pass
def callback(self, comment):
self.write_message(comment)
class TopicHandler(BaseHandler):
def get(self):
topic_id = urllib.parse.unquote(self.get_argument('id'))
topic = system.Topic.load_topic(topic_id)
comments = system.Topic.load_comments(topic_id)
self.render('topic.html', parts=parts, topic=topic, comments=comments,
page={'title': topic["title"]})
@tornado.web.authenticated
def post(self):
user = self.current_user
topic_id = urllib.parse.unquote(self.get_argument('id'))
content = self.get_argument('content')
new_comment = system.Topic.new_comment(topic_id, user, content)
self.application.topic_helper.add_comment(new_comment)
self.write("succeed")
'''
登陆注册如果成功会进入用户界面, 失败页面会刷新
'''
class LoginHandler(BaseHandler):
def get(self):
if self.current_user:
self.redirect("/user")
else:
self.render("login.html")
def post(self):
name = self.get_argument("name")
psw = self.get_argument("password")
rep_psw = self.get_argument("repPassword", None)
if rep_psw:
if rep_psw != psw:
self.redirect("/login")
elif system.User.register(name, psw):
self.set_secure_cookie("name", name)
self.set_secure_cookie("psw", psw)
self.redirect("/user")
else:
self.redirect("/login")
else:
if system.User.login(name, psw) == 0:
self.set_secure_cookie("name", name)
self.set_secure_cookie("psw", psw)
self.redirect("/user")
else:
self.redirect("/login")
class UserHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
user_info = system.User.get_user_info(self.current_user)
if user_info:
topics = system.User.get_user_topic(self.current_user)
self.render("user.html", parts=parts, user=user_info, topics=topics,
page={"title": self.current_user, })
else:
self.clear_all_cookies()
self.redirect("/login")
@tornado.web.authenticated
def post(self):
topic_id = self.get_argument("id")
status = self.get_argument("status")
if status == "0":
system.Topic.delete_topic(topic_id)
elif status == "1":
content = system.Topic.get_topic_content(topic_id)
self.write(content)
elif status == "2":
content = self.get_argument("content")
system.Topic.edit_topic(topic_id, content)
class IconHandler(BaseHandler):
def get(self):
self.redirect("/user")
@tornado.web.authenticated
def post(self):
name = self.current_user
icon = self.get_argument("icon")
system.User.set_icon(name, icon)
class TopicModule(tornado.web.UIModule):
def render(self, topic, part):
return self.render_string("modules/topicTuple.html", topic=topic, partname=part)
class CommentModule(tornado.web.UIModule):
def render(self, comment):
return self.render_string("modules/commentTuple.html", comment=comment)
class UserTopicModule(tornado.web.UIModule):
def render(self, topic):
return self.render_string("modules/userTopicTuple.html", topic=topic)
class Application(tornado.web.Application):
def __init__(self):
self.topic_helper = TopicHelper()
handlers = [
(r'/', MainPageHandler),
(r'/part/', PartHandler),
(r'/topic/', TopicHandler),
(r'/login', LoginHandler),
(r'/user', UserHandler),
(r'/icon', IconHandler),
(r'/topic/websocket', TopicWebSocket)
]
settings = {
"cookie_secret": "bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E=",
"template_path": os.path.join(os.path.dirname(__file__), 'templates'),
"static_path": os.path.join(os.path.dirname(__file__), "static"),
"ui_modules": {"Topic": TopicModule,
"Comment": CommentModule,
"UserTopic": UserTopicModule},
"login_url": "/login",
"xsrf_cookies": True,
"autoescape": None,
"debug": True
}
tornado.web.Application.__init__(self, handlers, **settings)
define("port", default=8000, help="run on the given port", type=int)
if __name__ == "__main__":
options.parse_command_line()
parts = database.get_partname()
app = Application()
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()