/
forum.py
174 lines (166 loc) · 6.44 KB
/
forum.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
# -*- coding: utf-8 -*-
# AUTHOR: Zeray Rice <fanzeyi1994@gmail.com>
# FILE: forum.py
# CREATED: 22:39:44 17/04/2012
# MODIFIED: 18:52:37 18/04/2012
import datetime
from tornado.web import HTTPError
from tornado.web import authenticated
from sqlalchemy.orm.exc import NoResultFound
from judge.db import Node
from judge.db import Reply
from judge.db import Topic
from judge.db import ForumDBMixin
from judge.base import BaseHandler
class ViewNodeHandler(BaseHandler, ForumDBMixin):
def get(self, link):
node = self.select_node_by_link(link.lower())
if not node:
raise HTTPError(404)
start = self.get_argument("start", default = 0)
try:
start = int(start)
except ValueError:
start = 0
topics = self.select_topic_by_node_id(node.id, start = 0)
breadcrumb = []
breadcrumb.append((self._('Home'), '/'))
breadcrumb.append((self._('Forum'), '/forum'))
breadcrumb.append((node.name, '/go/%s' % node.link))
title = node.name
for topic in topics:
topic.reply_count = self.count_reply_by_topic_id(topic.id)
self.render("topic_list.html", locals())
class ViewForumHandler(BaseHandler, ForumDBMixin):
def get(self):
start = self.get_argument("start", default = 0)
try:
start = int(start)
except ValueError:
start = 0
topics = self.select_lates_topic(start = 0)
breadcrumb = []
breadcrumb.append((self._('Home'), '/'))
breadcrumb.append((self._('Forum'), '/forum'))
title = self._("Forum")
index = True
for topic in topics:
topic.reply_count = self.count_reply_by_topic_id(topic.id)
self.render("topic_list.html", locals())
class CreateTopicHandler(BaseHandler, ForumDBMixin):
@authenticated
def get(self, link):
node = self.select_node_by_link(link.lower())
if not node:
raise HTTPError(404)
topic = None
if self.current_user.admin:
tid = self.get_argument("tid", default = 0)
try:
tid = int(tid)
except ValueError:
raise HTTPError(404)
topic = self.select_topic_by_id(tid)
breadcrumb = []
breadcrumb.append((self._('Home'), '/'))
breadcrumb.append((self._('Forum'), '/forum'))
breadcrumb.append((node.name, '/go/%s' % node.link))
breadcrumb.append((self._("Create Topic"), '/new/%s' % node.link))
title = self._("Create Topic")
self.render("topic_create.html", locals())
@authenticated
def post(self, link):
try:
node = self.select_node_by_link(link.lower())
except NoResultFound:
raise HTTPError(404)
title = self.get_argument("title", default = "")
content = self.get_argument("content", default = "")
error = []
topic = Topic()
if self.current_user.admin:
tid = self.get_argument("tid", default = 0)
try:
tid = int(tid)
except ValueError:
raise HTTPError(404)
if tid:
topic.id = tid
error.extend(self.check_text_value(title, self._("Topic Title"), required = True, max = 255))
error.extend(self.check_text_value(content, self._("Topic Content"), required = True, min = 5, max = 100000))
topic.title = self.xhtml_escape(title)
topic.content = self.xhtml_escape(content)
if error:
breadcrumb = []
breadcrumb.append((self._('Home'), '/'))
breadcrumb.append((self._('Forum'), '/forum'))
breadcrumb.append((node.name, '/go/%s' % node.link))
breadcrumb.append((self._("Create Topic"), '/new/%s' % node.link))
title = self._("Create Topic")
self.render("topic_create.html", locals())
return
topic.node_id = node.id
topic.member_id = self.current_user.id
if topic.id:
self.update_topic(topic)
else:
#self.insert_topic(topic)
topic.create = datetime.datetime.now()
topic.last_reply = datetime.datetime.now()
self.db.add(topic)
self.db.commit()
self.db.flush()
self.redirect("/t/%s" % topic.id)
class ViewTopicHandler(BaseHandler, ForumDBMixin):
def get(self, topic_id):
try:
topic_id = int(topic_id)
except ValueError:
raise HTTPError(404)
topic = self.select_topic_by_id(topic_id)
if not topic:
raise HTTPError(404)
replies = self.select_reply_by_topic_id(topic_id)
topic.reply_count = len(replies)
title = topic.title
breadcrumb = []
breadcrumb.append((self._('Home'), '/'))
breadcrumb.append((self._('Forum'), '/forum'))
breadcrumb.append((topic.node.name, '/go/%s' % topic.node.link))
breadcrumb.append((topic.title, '/t/%d' % topic.id))
self.render("topic.html", locals())
@authenticated
def post(self, topic_id):
try:
topic_id = int(topic_id)
except ValueError:
raise HTTPError(404)
topic = self.select_topic_by_id(topic_id)
if not topic:
raise HTTPError(404)
content = self.get_argument("content", default = "")
error = []
error.extend(self.check_text_value(content, self._("Reply Content"), required = True, min = 5))
if error:
replies = self.select_reply_by_topic_id(topic_id)
topic.reply_count = len(replies)
title = topic.title
content = self.xhtml_escape(content)
breadcrumb = []
breadcrumb.append((self._('Home'), '/'))
breadcrumb.append((self._('Forum'), '/forum'))
breadcrumb.append((topic.node.name, '/go/%s' % topic.node.link))
breadcrumb.append((topic.title, '/t/%d' % topic.id))
self.render("topic.html", locals())
return
reply = Reply()
reply.content = self.xhtml_escape(content)
reply.topic_id = topic.id
reply.member_id = self.current_user.id
reply.create = datetime.datetime.now()
self.db.add(reply)
self.db.commit()
topic.last_reply = datetime.datetime.now()
self.db.commit()
self.redirect("/t/%d" % topic_id)
__all__ = ["ViewNodeHandler", "CreateTopicHandler", "ViewTopicHandler", "ViewForumHandler"]