OSDN Git Service

Userdel corrected
[pybbs/pybbs.git] / index.py
1
2 import os.path
3 import shutil,copy
4 import tornado.auth
5 import tornado.escape
6 import tornado.web
7 import tornado.wsgi
8 from tinydb import TinyDB,Query,where
9 from tinydb.operations import delete
10 from datetime import datetime
11
12 class BaseHandler(tornado.web.RequestHandler):
13     def get_current_user(self):
14         user = self.get_secure_cookie('admin_user')
15         return tornado.escape.utf8(user)
16     
17     def set_current_user(self,username):
18         self.set_secure_cookie('admin_user',username)
19         
20     def clear_current_user(self):
21         self.clear_cookie('admin_user')
22
23 class IndexHandler(BaseHandler):
24     def get(self,dbname,page='0'):
25         params = self.application.db.get(where('kinds') == 'conf')
26         if params['mentenance'] == True:
27             self.render('mentenance.htm',title=params['title'],db=dbname)
28         if self.application.collection(dbname) == False:
29             if self.current_user == b'admin':
30                 self.application.db.table(dbname)
31             else:
32                 raise tornado.web.HTTPError(404)
33                 return
34         i = params['count']      
35         na = tornado.escape.url_unescape(self.get_cookie("username",u"誰かさん"))
36         pos = self.application.gpos(dbname,page)
37         table = self.application.db.table(dbname)
38         start = (pos-1)*i
39         if start < 0:
40             start = len(table)-i
41             if start < 0:
42                 start = 0
43         rec = sorted(table.all(),key=lambda x: x['number'])[start:start+i]
44         if len(table) >= 10*i:
45             self.render('modules/full.htm',position=pos,records=rec,data=params,db=dbname)
46             return
47         self.render('modules/index.htm',position=pos,records=rec,data=params,username=na,db=dbname)
48         
49 class LoginHandler(BaseHandler):
50     def get(self):
51         self.render('login.htm')
52         
53     def post(self):
54         pw = self.application.db.get(where('kinds') == 'conf')
55         if self.get_argument('password') == pw['password']:
56             self.set_current_user('admin')
57         dbname = self.get_argument('record')
58         self.redirect('/'+dbname+'/admin/0/')
59         
60 class LogoutHandler(BaseHandler):
61     def get(self):
62         self.clear_current_user()
63         self.redirect('/login')
64         
65 class NaviHandler(tornado.web.RequestHandler):
66     def get(self):
67         self.render('top.htm',coll=sorted(self.name()),full=self.full)
68         
69     def name(self):
70         for x in self.application.db.tables():
71             if x != '_default':
72                 yield x
73                 
74     def full(self,dbname):
75         if dbname in self.application.db.tables():
76             i = 10*self.application.db.get(where('kinds') == 'conf')['count']
77             table = self.application.db.table(dbname)
78             if len(table) >= i:
79                 return True
80         return False
81
82 class TitleHandler(NaviHandler):
83     def get(self):
84         rec = sorted(self.title(),key=lambda x: x['date2'])
85         self.render('title.htm',coll=rec,full=self.full)  
86         
87     def title(self):
88         for x in self.name():
89             item = {}
90             item['name'] = x
91             table = self.application.db.table(x)
92             i = len(table)
93             item['count'] = i            
94             if table.contains(where('number') == 1) == True:
95                 s = table.get(where('number') == 1)['title']
96             else:
97                 s = ''
98             item['title'] = s   
99             if i == 0:
100                 item['date'] = ''
101                 item['date2'] = 0
102             else:
103                 rec = sorted(table.all(),key=lambda k: k['number'])
104                 s = rec[i-1]['date']
105                 item['date'] = s
106                 i = datetime.strptime(s,'%Y/%m/%d %H:%M')
107                 year = datetime.now().year-i.year
108                 if year == 0:
109                     j = 800
110                 elif year == 1:
111                     j = 400
112                 else:
113                     j = 0
114                 item['date2'] = j+31*(i.month-1)+i.day
115             yield item
116         
117 class RegistHandler(tornado.web.RequestHandler):
118     def post(self,dbname):
119         if self.application.collection(dbname) == False:
120             raise tornado.web.HTTPError(404)
121             return
122         rec = self.application.db.get(where('kinds') == 'conf')
123         words = rec['bad_words']
124         out = rec['out_words']
125         na = self.get_argument('name')
126         sub = self.get_argument('title')
127         com = self.get_argument('comment')
128         text = ''
129         i = 0
130         error = ''
131         for word in out:
132             if word in com:
133                 error = error + u'禁止ワード.'
134                 break
135         for line in com.splitlines(True):
136             for word in words:
137                 if word in line.lower():
138                     error = error + u'タグ違反.('+word+')'       
139             i += len(line)
140             text = text+'<p>'+self.link(line)+'<br></p>'
141         pw = self.get_argument('password')
142         if sub == '':
143             sub = u'タイトルなし.'
144         if i == 0:
145             error = error + u'本文がありません.'
146         elif i > 1000:
147             error = error +u'文字数が1,000をこえました.'
148         article = self.application.db.table(dbname)
149         if len(article) == 0:
150             no = 1
151         else:
152             item = sorted(article.all(),key=lambda x: x['number'])[len(article)-1]
153             no = item['number']+1
154         if error == '':
155             s = datetime.now()
156             reg = {'number':no,'name':na,'title':sub,'comment':text,'raw':com,'password':pw,'date':s.strftime('%Y/%m/%d %H:%M')}
157             article.insert(reg)
158             self.set_cookie('username',tornado.escape.url_escape(na))
159             self.redirect('/'+dbname+'#article')
160         else:
161             self.render('regist.htm',content=error)
162     
163     def link(self,command):
164         y = ''
165         i = 0
166         text = ''
167         for x in command.split():
168             if (y == '>>')and(x.isdecimal() == True):
169                 s = '<a href=#'+x+'>'+x+'</a>'
170                 while -1 < command.find(x,i):
171                     j = command.find(x,i)
172                     tmp = command[i:j]
173                     i = j+len(x)
174                     k = tmp.rsplit(None,1)
175                     if ((len(k) > 1)and(k[1] == y))or(k[0] == y):
176                         text = text+tmp+s                                                                       
177                         break
178                     else:
179                         text = text+tmp+x                        
180             y = x    
181         if text == '':
182             return command
183         else:
184             if len(command) > i:
185                 return text+command[i:]
186             else:
187                 return text
188     
189 class AdminHandler(BaseHandler):
190     @tornado.web.authenticated               
191     def get(self,dbname,page='0'):
192         if dbname == '':
193             dbname = self.get_argument('record','')
194         if self.application.collection(dbname) == False:
195             raise tornado.web.HTTPError(404)
196             return
197         table = self.application.db.table(dbname) 
198         rec = sorted(table.all(),key=lambda x: x['number'])                   
199         mente = self.application.db.get(where('kinds') == 'conf')
200         if mente['mentenance'] == True:
201             check = 'checked=checked'
202         else:
203             check = ''
204         pos = self.application.gpos(dbname,page)
205         i = mente['count']
206         start = (pos-1)*i
207         if start < 0:
208             start = len(table)-i
209             if start < 0:
210                 start = 0
211         self.render('modules/admin.htm',position=pos,records=rec[start:start+i],mente=check,password=mente['password'],db=dbname)
212
213 class AdminConfHandler(BaseHandler):
214     @tornado.web.authenticated
215     def post(self,dbname,func):
216         if func == 'set':
217             param = self.application.db.get(where('kinds') == 'conf')['mentenance']
218             if self.get_argument('mente','') == 'on':
219                 mente = True
220                 if param != mente:
221                     self.store()
222             else:
223                 mente = False  
224                 if param != mente:
225                     self.restore()
226             word = self.get_argument('pass','')
227             if word == '':
228                 self.render('regist.htm',content='パスワードを設定してください')
229                 return
230             else:
231                 self.application.db.update({'mentenance':mente,'password':word},where('kinds') == 'conf')  
232         elif func == 'del':
233             table = self.application.db.table(dbname)
234             for x in self.get_arguments('item'):
235                 table.remove(where('number') == int(x))
236         self.redirect('/'+dbname+'/admin/0/')
237         
238     def store(self):
239         self.application.db.close()
240         shutil.copy(st.json,st.bak)
241         self.application.db = TinyDB(st.json)
242         
243     def restore(self):
244         database = self.application.db
245         bak = TinyDB(st.bak)
246         for x in database.tables():
247             if self.application.collection(x) == True:
248                 database.purge_table(x)
249                 if x in bak.tables():
250                     table = database.table(x)
251                     table.insert_multiple(bak.table(x).all())
252           
253 class UserHandler(tornado.web.RequestHandler):
254     def post(self,dbname):
255         num = self.get_argument('number')
256         if num.isdigit() == True:
257             num = int(num)
258             pas = self.get_argument('password')
259             table = self.application.db.table(dbname)
260             qwr = Query()
261             obj = table.get(qwr.number == num)
262             if obj and(obj['password'] == pas):
263                 table.remove(qwr.number == num)
264         self.redirect('/'+dbname)
265       
266 class SearchHandler(tornado.web.RequestHandler):       
267     def post(self,dbname):
268         self.word = tornado.escape.url_unescape(self.get_argument('word1'))
269         self.radiobox = self.get_argument('filter')
270         self.set_cookie('search',tornado.escape.url_escape(self.word))         
271         rec = sorted(self.search(dbname),key=lambda x: x['number'])
272         self.render('modules/search.htm',records=rec,word1=self.word,db=dbname)
273     
274     def get(self,dbname):
275         if self.application.collection(dbname) == False:
276             raise tornado.web.HTTPError(404)
277             return
278         word = self.get_cookie('search','')
279         word = tornado.escape.url_unescape(word)
280         self.render('modules/search.htm',records=[],word1=word,db=dbname)
281         
282     def search(self,dbname):
283         table = self.application.db.table(dbname)    
284         element = self.word.split()
285         if len(element) == 0:
286             element = ['']
287         while len(element) < 3:
288             element.append(element[0])
289         if self.radiobox == 'comment':
290             query = (Query().raw.search(element[0])) | (Query().raw.search(element[1])) | (Query().raw.search(element[2]))
291         else:
292             query = (Query().name == element[0]) | (Query().name == element[1]) | (Query().name == element[2])
293         if self.radiobox == 'comment':    
294             for x in table.search(query):
295                 com = ''
296                 for text in x['raw'].splitlines(True):                  
297                     for word in self.word.split():                        
298                         if text.find(word) > -1:
299                             com = com +'<p style=background-color:yellow>'+text+'<br></p>'  
300                             break                          
301                     else:
302                         com = com+'<p>'+text+'<br></p>'
303                 x['comment'] = com
304                 yield x       
305         else:
306             for x in table.search(query):
307                 yield x
308                                         
309 class FooterModule(tornado.web.UIModule):
310     def render(self,number,url,link):
311         return self.render_string('modules/footer.htm',index=number,url=url,link=link)
312     
313 class Application(tornado.web.Application):    
314     def __init__(self):
315         self.db = TinyDB(st.json)
316         handlers = [(r'/',NaviHandler),(r'/login',LoginHandler),(r'/logout',LogoutHandler),(r'/title',TitleHandler),
317                     (r'/([a-zA-Z0-9_]+)',IndexHandler),(r'/([a-zA-Z0-9_]+)/([0-9]+)/',IndexHandler),
318                     (r'/([a-zA-Z0-9_]+)/admin/([0-9]+)/',AdminHandler),(r'/([a-zA-Z0-9_]+)/admin/([a-z]+)/',AdminConfHandler),(r'/([a-zA-Z0-9_]+)/userdel',UserHandler),
319                     (r'/([a-zA-Z0-9_]+)/search',SearchHandler),(r'/([a-zA-Z0-9_]+)/regist',RegistHandler)]
320         settings = {'template_path':os.path.join(os.path.dirname(__file__),'pybbs'),
321                         'static_path':os.path.join(os.path.dirname(__file__),'static'),
322                         'ui_modules':{'Footer':FooterModule},
323                         'cookie_secret':'bZJc2sWbQLKos6GkHn/VB9oXwQt8SOROkRvJ5/xJ89E=',
324                         'xsrf_cookies':True,
325                         #'debug':True,
326                         'login_url':'/login'
327                         }
328         tornado.web.Application.__init__(self,handlers,**settings)
329  
330     def gpos(self,dbname,page):
331         params = self.db.get(where('kinds') == 'conf')
332         pos = int(page)
333         if pos <= 0:
334             pos = 0
335         elif (pos-1)*params['count'] >= len(self.db.table(dbname)):
336             pos = 0
337         return pos
338     
339     def collection(self,name):
340         if name in self.db.tables():
341             return True
342         else:
343             return False
344
345 class static():
346     json = 'static/db/db.json'
347     bak = 'static/db/bak.json'
348
349 st = static()
350 app = tornado.wsgi.WSGIAdapter(Application())