]>
Commit | Line | Data |
---|---|---|
12e5de7e MT |
1 | #!/usr/bin/python |
2 | ||
f0714277 | 3 | import email.utils |
12e5de7e MT |
4 | import tornado.web |
5 | ||
6 | import handlers_base as base | |
7 | ||
f91dfcc7 MT |
8 | from . import ui_modules |
9 | ||
8a897d25 MT |
10 | class IndexHandler(base.BaseHandler): |
11 | def get(self): | |
0a6875dc | 12 | posts = self.backend.blog.get_newest(limit=3) |
8a897d25 MT |
13 | |
14 | self.render("blog/index.html", posts=posts) | |
15 | ||
16 | ||
cfc0823a MT |
17 | class AuthorHandler(base.BaseHandler): |
18 | def get(self, uid): | |
19 | author = self.accounts.get_by_uid(uid) | |
20 | if not author: | |
21 | raise tornado.web.HTTPError(404, "User is unknown") | |
22 | ||
23 | # Get all posts from this author | |
0a6875dc | 24 | posts = self.backend.blog.get_by_author(author.uid) |
cfc0823a MT |
25 | if not posts: |
26 | raise tornado.web.HTTPError(404, "User has no posts") | |
27 | ||
28 | self.render("blog/author.html", author=author, posts=posts) | |
29 | ||
30 | ||
f0714277 MT |
31 | class FeedHandler(base.BaseHandler): |
32 | def get(self): | |
33 | cache_key = "%s-%s" % (self.request.host, self.request.path) | |
34 | ||
35 | # Get feed from cache if possible | |
36 | feed = self.memcached.get(cache_key) | |
37 | if not feed: | |
0a6875dc | 38 | posts = self.backend.blog.get_newest(limit=50) |
f0714277 MT |
39 | |
40 | # Render the feed | |
41 | feed = self.render_string("blog/feed.xml", posts=posts, | |
42 | now=email.utils.formatdate()) | |
43 | ||
44 | # Store in cache for 5min | |
45 | self.memcached.set(cache_key, feed, 300) | |
46 | ||
47 | # Set correct content type | |
48 | self.set_header("Content-Type", "application/rss+xml") | |
49 | ||
50 | # Deliver content | |
51 | self.finish(feed) | |
52 | ||
53 | ||
12e5de7e MT |
54 | class PostHandler(base.BaseHandler): |
55 | def get(self, slug): | |
0a6875dc MT |
56 | post = self.backend.blog.get_by_slug(slug) |
57 | if not post: | |
12e5de7e MT |
58 | raise tornado.web.HTTPError(404) |
59 | ||
0a6875dc | 60 | self.render("blog/post.html", post=post) |
f91dfcc7 MT |
61 | |
62 | ||
e6b18dce MT |
63 | class SearchHandler(base.BaseHandler): |
64 | def get(self): | |
65 | q = self.get_argument("q") | |
66 | ||
0a6875dc | 67 | posts = self.backend.blog.search(q, limit=50) |
e6b18dce MT |
68 | if not posts: |
69 | raise tornado.web.HTTPError(404, "Nothing found") | |
70 | ||
71 | self.render("blog/search-results.html", q=q, posts=posts) | |
72 | ||
73 | ||
4bde7f18 MT |
74 | class TagHandler(base.BaseHandler): |
75 | def get(self, tag): | |
76 | posts = self.backend.blog.get_by_tag(tag) | |
77 | if not posts: | |
78 | raise tornado.web.HTTPError(404, "There are no posts with tag: %s" % tag) | |
79 | ||
80 | self.render("blog/tag.html", posts=posts, tag=tag) | |
81 | ||
82 | ||
f91dfcc7 MT |
83 | class PostModule(ui_modules.UIModule): |
84 | def render(self, post): | |
85 | return self.render_string("blog/modules/post.html", post=post) | |
8a897d25 MT |
86 | |
87 | ||
88 | class PostsModule(ui_modules.UIModule): | |
89 | def render(self, posts): | |
0a6875dc | 90 | return self.render_string("blog/modules/posts.html", posts=list(posts)) |