/
app.py
246 lines (198 loc) · 7.53 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
from bottle import route, run, redirect, request, static_file, jinja2_template
import bottle
import re
@route('/')
def bbs_top():
bbs = Bbs()
data = bbs.get_all_threads()
settings_dict = {
'autoescape': False,
}
return jinja2_template('bbs.html', data=data, template_settings=settings_dict)
@route('/create_thread', method='POST')
def create_thread():
thread_name = request.forms.get('thread_name')
thread_name = escape_special_chars(thread_name)
name = request.forms.get('name')
if not name:
name = 'nanashi'
else:
name = escape_special_chars(name)
content = request.forms.get('content')
content = escape_special_chars(content)
content = replace_lf_to_br_tag(content)
content = link(content)
content = anchor(content)
bbs = Bbs()
thread_id = bbs.create_new_thread(thread_name)
if bbs.add_new_post(thread_id, name, content) == False:
redirect('/')
redirect('/', 302)
@route('/<thread_id:int>/')
def thread(thread_id):
bbs = Bbs()
data = bbs.get_all_posts_in_thread(thread_id)
title = bbs.get_thread_title(thread_id)
settings_dict = {
'autoescape': False,
}
return jinja2_template('thread.html', title=title, data=data, thread_id=thread_id, template_settings=settings_dict)
@route('/add_post', method='POST')
def add_post():
thread_id = request.forms.get('thread_id')
name = request.forms.get('name')
if not name:
name = 'nanashi'
else:
name = escape_special_chars(name)
content = request.forms.get('content')
content = escape_special_chars(content)
content = replace_lf_to_br_tag(content)
content = link(content)
content = anchor(content)
bbs = Bbs()
if bbs.add_new_post(thread_id, name, content) == False:
redirect('/')
bbs.update_thread_status(thread_id)
redirect_path = '/%s/' % thread_id
redirect(redirect_path, 302)
@route('/static/<filepath:path>')
def static(filepath):
return static_file(filepath, root='./static')
def escape_special_chars(content):
content = content.replace('<', '<')
content = content.replace('>', '>')
content = content.replace('\'', ''')
content = content.replace('"', '"')
return content
def replace_lf_to_br_tag(content):
content = content.replace('\n', '<br>')
return content
def link(content):
pattern = re.compile(r'(?P<url>http://[A-Za-z0-9\'~+\-=_.,/%\?!;:@#\*&\(\)]+)')
content = pattern.sub(r'<a href="\g<url>">\g<url></a>', content)
return content
def anchor(content):
pattern = re.compile(r'>>(?P<number>[0-9]+)')
content = pattern.sub(r'<a href="#\g<number>">>>\g<number></a>', content)
return content
from pycassa.pool import ConnectionPool
from pycassa.columnfamily import ColumnFamily
from pycassa.system_manager import SystemManager, UTF8_TYPE
import sys, random, datetime
class Bbs(object):
def __init__(self):
self.conn = ConnectionPool('bbs', ['localhost:9160'], pool_size=1)
def _create_cf(self, cf_name):
sys_mng = SystemManager('localhost:9160')
validators = {'name': UTF8_TYPE, 'content': UTF8_TYPE, 'post_time': UTF8_TYPE}
sys_mng.create_column_family(
'bbs',
cf_name,
super=False,
comparator_type=UTF8_TYPE,
key_validation_class=UTF8_TYPE,
column_validation_classes=validators
)
sys_mng.close()
def _drop_cf(self, cf_name):
sys_mng = SystemManager('localhost:9160')
sys_mng.drop_column_family('bbs',cf_name)
sys_mng.close()
def _get_oldest_thread(self):
threads = ColumnFamily(self.conn, 'threads')
# get all row keys
row_keys = []
ret = list(threads.get_range())
for v in ret:
row_keys.append(v[0])
result = []
ret = threads.multiget(row_keys)
for key in row_keys:
row = {}
row['thread_id'] = int(key)
row['thread_name'] = int(key)
row['post_count'] = ret[key]['post_count']
row['create_time'] = ret[key]['create_time']
row['update_time'] = ret[key]['update_time']
result.append(row)
result.sort(cmp=lambda x,y: cmp(x['update_time'], y['update_time']))
return result[0]
def create_new_thread(self, thread_name):
threads = ColumnFamily(self.conn, 'threads')
ret = list(threads.get_range())
if len(ret) > 99:
oldest_thread = self._get_oldest_thread()
oldest_thread_id = str(oldest_thread['thread_id'])
threads.remove(oldest_thread_id)
self._drop_cf(oldest_thread_id)
thread_id = '%s' % random.randint(1,sys.maxint)
dt = datetime.datetime.today()
str_dt = dt.strftime('%Y-%m-%d %H:%M:%S')
threads.insert(thread_id, {'thread_name': thread_name, 'post_count': '1', 'create_time': str_dt, 'update_time': str_dt})
self._create_cf(thread_id)
return thread_id
def add_new_post(self, thread_id, name, content):
post = ColumnFamily(self.conn, thread_id)
ret = list(post.get_range())
if len(ret) > 1000:
return False
key = len(ret) + 1
key = '%s' % key
dt = datetime.datetime.today()
str_dt = dt.strftime('%Y-%m-%d %H:%M:%S')
post.insert(key, {'name': name, 'content': content, 'post_time': str_dt})
return True
def get_all_threads(self):
threads = ColumnFamily(self.conn, 'threads')
# get all row keys
row_keys = []
ret = list(threads.get_range())
for v in ret:
row_keys.append(v[0])
# get all row data
result = []
ret = threads.multiget(row_keys)
for key in row_keys:
row = {}
row['thread_id'] = int(key)
row['thread_name'] = ret[key]['thread_name']
row['post_count'] = ret[key]['post_count']
row['create_time'] = ret[key]['create_time']
row['update_time'] = ret[key]['update_time']
result.append(row)
result.sort(cmp=lambda x,y: cmp(y['update_time'], x['update_time']))
return result
def get_all_posts_in_thread(self, thread_id):
posts = ColumnFamily(self.conn, str(thread_id))
# get all row keys
row_keys = []
ret = list(posts.get_range())
for v in ret:
row_keys.append(v[0])
# get all row data
result = []
ret = posts.multiget(row_keys)
for key in row_keys:
row = {}
row['key'] = int(key)
row['name'] = ret[key]['name']
row['content'] = ret[key]['content']
row['post_time'] = ret[key]['post_time']
result.append(row)
result.sort(cmp=lambda x,y: cmp(x['key'], y['key']))
return result
def update_thread_status(self, thread_id):
threads = ColumnFamily(self.conn, 'threads')
dt = datetime.datetime.today()
str_dt = dt.strftime('%Y-%m-%d %H:%M:%S')
ret = threads.get(str(thread_id), columns=['post_count'])
post_count = int(ret['post_count']) + 1
threads.insert(thread_id, {'post_count': str(post_count), 'update_time': str_dt})
def get_thread_title(self, thread_id):
threads = ColumnFamily(self.conn, 'threads')
ret = threads.get(str(thread_id), columns=['thread_name'])
return ret['thread_name']
if __name__ == '__main__':
run(host='0.0.0.0', port=8000, debug=True, reloader=True)
app = bottle.default_app()