]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/blog.py
6 import handlers_base
as base
8 from . import ui_modules
10 class IndexHandler(base
.BaseHandler
):
12 posts
= self
.backend
.blog
.get_newest(limit
=3)
14 self
.render("blog/index.html", posts
=posts
)
17 class AuthorHandler(base
.BaseHandler
):
19 author
= self
.accounts
.get_by_uid(uid
)
21 raise tornado
.web
.HTTPError(404, "User is unknown")
23 # Get all posts from this author
24 posts
= self
.backend
.blog
.get_by_author(author
)
26 raise tornado
.web
.HTTPError(404, "User has no posts")
28 self
.render("blog/author.html", author
=author
, posts
=posts
)
31 class FeedHandler(base
.BaseHandler
):
33 cache_key
= "%s-%s" % (self
.request
.host
, self
.request
.path
)
35 # Get feed from cache if possible
36 feed
= self
.memcached
.get(cache_key
)
38 posts
= self
.backend
.blog
.get_newest(limit
=50)
41 feed
= self
.render_string("blog/feed.xml", posts
=posts
,
42 now
=email
.utils
.formatdate())
44 # Store in cache for 5min
45 self
.memcached
.set(cache_key
, feed
, 300)
47 # Set correct content type
48 self
.set_header("Content-Type", "application/rss+xml")
54 class PostHandler(base
.BaseHandler
):
56 post
= self
.backend
.blog
.get_by_slug(slug
, published
=not self
.current_user
)
58 raise tornado
.web
.HTTPError(404)
60 self
.render("blog/post.html", post
=post
)
63 class DraftsHandler(base
.BaseHandler
):
64 @tornado.web
.authenticated
66 drafts
= self
.backend
.blog
.get_drafts(author
=self
.current_user
)
68 self
.render("blog/drafts.html", drafts
=drafts
)
71 class SearchHandler(base
.BaseHandler
):
73 q
= self
.get_argument("q")
75 posts
= self
.backend
.blog
.search(q
, limit
=50)
77 raise tornado
.web
.HTTPError(404, "Nothing found")
79 self
.render("blog/search-results.html", q
=q
, posts
=posts
)
82 class TagHandler(base
.BaseHandler
):
84 posts
= self
.backend
.blog
.get_by_tag(tag
)
86 raise tornado
.web
.HTTPError(404, "There are no posts with tag: %s" % tag
)
88 self
.render("blog/tag.html", posts
=posts
, tag
=tag
)
91 class YearHandler(base
.BaseHandler
):
93 posts
= self
.backend
.blog
.get_by_year(year
)
95 raise tornado
.web
.HTTPError(404, "There are no posts in %s" % year
)
97 self
.render("blog/year.html", posts
=posts
, year
=year
)
100 class ComposeHandler(base
.BaseHandler
):
101 @tornado.web
.authenticated
103 self
.render("blog/compose.html", post
=None)
105 @tornado.web
.authenticated
107 title
= self
.get_argument("title")
108 text
= self
.get_argument("text")
109 tags
= self
.get_argument("tags", None)
111 with self
.db
.transaction():
112 post
= self
.backend
.blog
.create_post(title
, text
,
113 author
=self
.current_user
, tags
=tags
)
115 self
.redirect("/drafts")
118 class EditHandler(base
.BaseHandler
):
119 @tornado.web
.authenticated
121 post
= self
.backend
.blog
.get_by_slug(slug
)
123 raise tornado
.web
.HTTPError(404)
125 # XXX check if post is editable
127 self
.render("blog/compose.html", post
=post
)
129 @tornado.web
.authenticated
130 def post(self
, slug
):
131 post
= self
.backend
.blog
.get_by_slug(slug
)
133 raise tornado
.web
.HTTPError(404)
135 # XXX check if post is editable
137 with self
.db
.transaction():
139 post
.title
= self
.get_argument("title")
142 post
.text
= self
.get_argument("text")
145 post
.tags
= (t
.strip() for t
in self
.get_argument("tags", "").split(","))
147 # Mark post as updated
150 # Return to blog if the post is already published
151 if post
.is_published():
152 self
.redirect("/post/%s" % post
.slug
)
155 # Otherwise return to drafts
156 self
.redirect("/drafts")
159 class HistoryNavigationModule(ui_modules
.UIModule
):
161 return self
.render_string("blog/modules/history-navigation.html",
162 years
=self
.backend
.blog
.years
)
165 class ListModule(ui_modules
.UIModule
):
166 def render(self
, posts
):
167 return self
.render_string("blog/modules/list.html", posts
=posts
)
170 class PostModule(ui_modules
.UIModule
):
171 def render(self
, post
):
172 return self
.render_string("blog/modules/post.html", post
=post
)
175 class PostsModule(ui_modules
.UIModule
):
176 def render(self
, posts
):
177 return self
.render_string("blog/modules/posts.html", posts
=list(posts
))