-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
executable file
·110 lines (87 loc) · 2.88 KB
/
webapp.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import locale, sys
locale.setlocale(locale.LC_ALL, 'ru_RU.UTF-8')
reload(sys)
sys.setdefaultencoding('utf-8')
import os
import config
import tornado.httpserver
import tornado.ioloop
import tornado.web
from redis import Redis
from urllib import unquote
class Posts(tornado.web.RequestHandler):
def get(self, page=None):
""" render posts list page (10 by page) """
if page is None:
page = 1
else:
page = int(page)
if page < 1:
raise tornado.web.HTTPError(404)
offset = (page - 1) * 10
while True:
try:
R.send_command("WATCH last_post_id")
R.send_command("MULTI")
post_id = R.incr("last_post_id")
R.send_command("EXEC")
break
except Exception, e:
print(str(e))
self.write(str(post_id))
# self.redirect("")
# self.render("posts.html", page=page, url=self.request.uri)
def post(self):
""" save new post """
title = self.get_argument("title")
body = self.get_argument("body")
tags = self.get_argument("tags")
post_id = R.incr("last_post_id")
self.write(str(post_id))
# self.redirect("")
# raise NotImplementedYet()
class OnePost(tornado.web.RequestHandler):
def get(self, id):
""" render one post with comments tree"""
raise NotImplementedYet()
class Tags(tornado.web.RequestHandler):
def get(self, id):
""" render tag cloud """
raise NotImplementedYet()
class OneTag(tornado.web.RequestHandler):
def get(self, id):
""" render posts list by tag """
raise NotImplementedYet()
class Search(tornado.web.RequestHandler):
def get(self, expression):
""" render search results """
unquoted = unquote(expression)
raise NotImplementedYet()
class PostComments(tornado.web.RequestHandler):
def post(self, id):
""" save post comment """
body = self.get_argument("body")
parent_id = self.get_argument("parent_id", None)
self.redirect("/posts/%d"%id)
raise NotImplementedYet()
class NotImplementedYet(Exception):
pass
application = tornado.web.Application([
("/posts", Posts),
("/", Posts),
(r"/posts/page/([0-9]+)", Posts),
(r"/posts/([0-9]+)", OnePost),
(r"/posts/([0-9]+)/comments", PostComments),
(r"/tags", Tags),
(r"/tags/([0-9]+)", OneTag),
(r"/search/(.+)", Search),
], debug=config.debug, template_path=os.path.join(os.path.dirname(__file__), 'templates'))
if __name__ == "__main__":
R = Redis(config.db_host)
R.select(config.db_idx)
http_server = tornado.httpserver.HTTPServer(application)
http_server.bind(config.port, config.host)
http_server.start(0)
tornado.ioloop.IOLoop.instance().start()