]>
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
=list(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
, published
=False)
123 raise tornado
.web
.HTTPError(404)
125 # Check if post is editable
126 if not post
.is_editable(self
.current_user
):
127 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
129 self
.render("blog/compose.html", post
=post
)
131 @tornado.web
.authenticated
132 def post(self
, slug
):
133 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
135 raise tornado
.web
.HTTPError(404)
137 # Check if post is editable
138 if not post
.is_editable(self
.current_user
):
139 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
141 # Save updated content
142 with self
.db
.transaction():
144 title
= self
.get_argument("title"),
145 text
= self
.get_argument("text"),
146 tags
= self
.get_argument("tags").split(),
149 # Return to blog if the post is already published
150 if post
.is_published():
151 self
.redirect("/post/%s" % post
.slug
)
154 # Otherwise return to drafts
155 self
.redirect("/drafts")
158 class HistoryNavigationModule(ui_modules
.UIModule
):
160 return self
.render_string("blog/modules/history-navigation.html",
161 years
=self
.backend
.blog
.years
)
164 class ListModule(ui_modules
.UIModule
):
165 def render(self
, posts
):
166 return self
.render_string("blog/modules/list.html", posts
=posts
)
169 class PostModule(ui_modules
.UIModule
):
170 def render(self
, post
):
171 return self
.render_string("blog/modules/post.html", post
=post
)
174 class PostsModule(ui_modules
.UIModule
):
175 def render(self
, posts
):
176 return self
.render_string("blog/modules/posts.html", posts
=list(posts
))