]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/blog.py
10 from . import ui_modules
12 class IndexHandler(auth
.CacheMixin
, base
.BaseHandler
):
14 posts
= self
.backend
.blog
.get_newest(limit
=3)
16 # Allow this to be cached for 5 minutes
17 if not self
.current_user
:
20 self
.render("blog/index.html", posts
=posts
)
23 class AuthorHandler(auth
.CacheMixin
, base
.BaseHandler
):
25 author
= self
.accounts
.get_by_uid(uid
)
27 raise tornado
.web
.HTTPError(404, "User is unknown")
29 # Get all posts from this author
30 posts
= self
.backend
.blog
.get_by_author(author
)
32 raise tornado
.web
.HTTPError(404, "User has no posts")
34 # Allow this to be cached for 10 minutes
35 if not self
.current_user
:
38 self
.render("blog/author.html", author
=author
, posts
=posts
)
41 class FeedHandler(base
.BaseHandler
):
43 posts
= self
.backend
.blog
.get_newest(limit
=10)
45 raise tornado
.web
.HTTPError(404)
47 # Allow this to be cached for 10 minutes
50 # Set correct content type
51 self
.set_header("Content-Type", "application/atom+xml")
54 self
.render("blog/feed.xml", posts
=posts
,
55 now
=datetime
.datetime
.now())
58 class PostHandler(auth
.CacheMixin
, base
.BaseHandler
):
60 post
= self
.backend
.blog
.get_by_slug(slug
, published
=not self
.current_user
)
62 raise tornado
.web
.HTTPError(404)
64 # Allow this to be cached for 10 minutes
65 if post
.is_published():
68 self
.render("blog/post.html", post
=post
)
71 class PublishHandler(auth
.CacheMixin
, base
.BaseHandler
):
72 @tornado.web
.authenticated
74 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
76 raise tornado
.web
.HTTPError(404)
78 # Check if current_user is allowed to edit the post
79 if not post
.is_editable(self
.current_user
):
80 raise tornado
.web
.HTTPError(403)
82 # Is the post already published?
83 if post
.is_published():
84 raise tornado
.web
.HTTPError(400, "Post is already published")
86 self
.render("blog/publish.html", post
=post
)
88 @tornado.web
.authenticated
90 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
92 raise tornado
.web
.HTTPError(404)
94 # Check if current_user is allowed to edit the post
95 if not post
.is_editable(self
.current_user
):
96 raise tornado
.web
.HTTPError(403)
98 # Is the post already published?
99 if post
.is_published():
100 raise tornado
.web
.HTTPError(400, "Post is already published")
102 when
= self
.get_argument("when", None)
104 when
= dateutil
.parser
.parse(when
)
107 with self
.db
.transaction():
110 self
.redirect("/post/%s" % post
.slug
)
113 class DraftsHandler(auth
.CacheMixin
, base
.BaseHandler
):
114 @tornado.web
.authenticated
116 drafts
= self
.backend
.blog
.get_drafts(author
=self
.current_user
)
118 self
.render("blog/drafts.html", drafts
=drafts
)
121 class SearchHandler(auth
.CacheMixin
, base
.BaseHandler
):
122 @base.ratelimit(minutes
=5, requests
=25)
124 q
= self
.get_argument("q")
126 posts
= self
.backend
.blog
.search(q
, limit
=50)
128 raise tornado
.web
.HTTPError(404, "Nothing found")
130 self
.render("blog/search-results.html", q
=q
, posts
=posts
)
133 class TagHandler(auth
.CacheMixin
, base
.BaseHandler
):
135 posts
= self
.backend
.blog
.get_by_tag(tag
)
137 raise tornado
.web
.HTTPError(404, "There are no posts with tag: %s" % tag
)
139 # Allow this to be cached for 10 minutes
140 self
.set_expires(600)
142 self
.render("blog/tag.html", posts
=list(posts
), tag
=tag
)
145 class YearHandler(auth
.CacheMixin
, base
.BaseHandler
):
147 posts
= self
.backend
.blog
.get_by_year(year
)
149 raise tornado
.web
.HTTPError(404, "There are no posts in %s" % year
)
151 # Allow this to be cached for 10 minutes
152 self
.set_expires(600)
154 self
.render("blog/year.html", posts
=posts
, year
=year
)
157 class ComposeHandler(auth
.CacheMixin
, base
.BaseHandler
):
158 @tornado.web
.authenticated
160 self
.render("blog/compose.html", post
=None)
162 @tornado.web
.authenticated
164 title
= self
.get_argument("title")
165 text
= self
.get_argument("text")
166 tags
= self
.get_argument("tags", "").split(" ")
168 with self
.db
.transaction():
169 post
= self
.backend
.blog
.create_post(title
, text
,
170 author
=self
.current_user
, tags
=tags
)
172 self
.redirect("/drafts")
175 class EditHandler(auth
.CacheMixin
, base
.BaseHandler
):
176 @tornado.web
.authenticated
178 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
180 raise tornado
.web
.HTTPError(404)
182 # Check if post is editable
183 if not post
.is_editable(self
.current_user
):
184 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
186 self
.render("blog/compose.html", post
=post
)
188 @tornado.web
.authenticated
189 def post(self
, slug
):
190 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
192 raise tornado
.web
.HTTPError(404)
194 # Check if post is editable
195 if not post
.is_editable(self
.current_user
):
196 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
198 # Save updated content
199 with self
.db
.transaction():
201 title
= self
.get_argument("title"),
202 text
= self
.get_argument("text"),
203 tags
= self
.get_argument("tags", "").split(" "),
206 # Return to blog if the post is already published
207 if post
.is_published():
208 self
.redirect("/post/%s" % post
.slug
)
211 # Otherwise return to drafts
212 self
.redirect("/drafts")
215 class DeleteHandler(auth
.CacheMixin
, base
.BaseHandler
):
216 @tornado.web
.authenticated
218 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
220 raise tornado
.web
.HTTPError(404)
222 # Check if post is editable
223 if not post
.is_editable(self
.current_user
):
224 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
226 self
.render("blog/delete.html", post
=post
)
228 @tornado.web
.authenticated
229 def post(self
, slug
):
230 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
232 raise tornado
.web
.HTTPError(404)
234 # Check if post is editable
235 if not post
.is_editable(self
.current_user
):
236 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
238 with self
.db
.transaction():
242 self
.redirect("/drafts")
245 class HistoryNavigationModule(ui_modules
.UIModule
):
247 return self
.render_string("blog/modules/history-navigation.html",
248 years
=self
.backend
.blog
.years
)
251 class ListModule(ui_modules
.UIModule
):
252 def render(self
, posts
):
253 return self
.render_string("blog/modules/list.html", posts
=posts
)
256 class PostModule(ui_modules
.UIModule
):
257 def render(self
, post
):
258 return self
.render_string("blog/modules/post.html", post
=post
)
261 class PostsModule(ui_modules
.UIModule
):
262 def render(self
, posts
):
263 return self
.render_string("blog/modules/posts.html", posts
=list(posts
))