]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/blog.py
9 from . import ui_modules
11 class IndexHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
15 # Fetch the search query
16 q
= self
.get_argument("q", None)
18 # If the user is searching, perform the search
20 posts
= self
.backend
.blog
.search(q
)
22 # Otherwise fetch the latest posts
24 posts
= self
.backend
.blog
.get_newest(limit
=10)
26 # Extract the latest post
27 latest_post
= posts
.pop(0)
29 self
.render("blog/index.html", q
=q
, posts
=posts
, latest_post
=latest_post
)
32 class FeedHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
34 posts
= self
.backend
.blog
.get_newest(limit
=10)
36 raise tornado
.web
.HTTPError(404)
38 # Allow this to be cached for 10 minutes
41 # Set correct content type
42 self
.set_header("Content-Type", "application/atom+xml")
45 self
.render("blog/feed.xml", posts
=posts
,
46 now
=datetime
.datetime
.now())
49 class PostHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
51 post
= self
.backend
.blog
.get_by_slug(slug
)
53 raise tornado
.web
.HTTPError(404)
55 self
.render("blog/post.html", post
=post
)
58 class PublishHandler(base
.BaseHandler
):
59 @tornado.web
.authenticated
61 # Check if the user has permissions
62 if not self
.current_user
.is_blog_author():
63 raise tornado
.web
.HTTPError(403)
65 @tornado.web
.authenticated
67 post
= self
.backend
.blog
.get_by_slug(slug
)
69 raise tornado
.web
.HTTPError(404)
71 # Check if current_user is allowed to edit the post
72 if not post
.is_editable(self
.current_user
):
73 raise tornado
.web
.HTTPError(403)
75 # Is the post already published?
76 if post
.is_published():
77 raise tornado
.web
.HTTPError(400, "Post is already published")
79 self
.render("blog/publish.html", post
=post
)
81 @tornado.web
.authenticated
83 post
= self
.backend
.blog
.get_by_slug(slug
)
85 raise tornado
.web
.HTTPError(404)
87 # Check if current_user is allowed to edit the post
88 if not post
.is_editable(self
.current_user
):
89 raise tornado
.web
.HTTPError(403)
91 # Is the post already published?
92 if post
.is_published():
93 raise tornado
.web
.HTTPError(400, "Post is already published")
95 when
= self
.get_argument("when", None)
97 when
= dateutil
.parser
.parse(when
)
100 with self
.db
.transaction():
103 self
.redirect("/blog/%s" % post
.slug
)
106 class DraftsHandler(base
.BaseHandler
):
107 @tornado.web
.authenticated
109 # Check if the user has permissions
110 if not self
.current_user
.is_blog_author():
111 raise tornado
.web
.HTTPError(403)
113 @tornado.web
.authenticated
115 drafts
= self
.backend
.blog
.get_drafts(author
=self
.current_user
)
117 self
.render("blog/drafts.html", drafts
=drafts
)
120 class YearHandler(base
.AnalyticsMixin
, base
.BaseHandler
):
122 posts
= self
.backend
.blog
.get_by_year(year
)
124 raise tornado
.web
.HTTPError(404, "There are no posts in %s" % year
)
126 self
.render("blog/year.html", posts
=posts
, year
=year
)
129 class WriteHandler(base
.BaseHandler
):
130 @tornado.web
.authenticated
132 # Check if the user has permissions
133 if not self
.current_user
.is_blog_author():
134 raise tornado
.web
.HTTPError(403)
136 @tornado.web
.authenticated
138 self
.render("blog/write.html", post
=None)
140 @tornado.web
.authenticated
142 title
= self
.get_argument("title")
143 text
= self
.get_argument("text")
144 tags
= self
.get_argument("tags", "").split(" ")
146 with self
.db
.transaction():
147 post
= self
.backend
.blog
.create_post(title
, text
,
148 author
=self
.current_user
, tags
=tags
)
150 # Redirect to the new post
151 self
.redirect("/blog/%s" % post
.slug
)
154 class EditHandler(base
.BaseHandler
):
155 @tornado.web
.authenticated
157 post
= self
.backend
.blog
.get_by_slug(slug
)
159 raise tornado
.web
.HTTPError(404)
161 # Check if post is editable
162 if not post
.is_editable(self
.current_user
):
163 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
165 self
.render("blog/write.html", post
=post
)
167 @tornado.web
.authenticated
168 def post(self
, slug
):
169 post
= self
.backend
.blog
.get_by_slug(slug
)
171 raise tornado
.web
.HTTPError(404, "Could not find post %s" % slug
)
173 # Check if post is editable
174 if not post
.is_editable(self
.current_user
):
175 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
177 # Save updated content
178 with self
.db
.transaction():
180 title
= self
.get_argument("title"),
181 text
= self
.get_argument("text"),
182 tags
= self
.get_argument("tags", "").split(" "),
185 # Redirect to the post
186 self
.redirect("/blog/%s" % post
.slug
)
189 class DeleteHandler(base
.BaseHandler
):
190 @tornado.web
.authenticated
192 post
= self
.backend
.blog
.get_by_slug(slug
)
194 raise tornado
.web
.HTTPError(404)
196 # Check if post is editable
197 if not post
.is_editable(self
.current_user
):
198 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
200 self
.render("blog/delete.html", post
=post
)
202 @tornado.web
.authenticated
203 def post(self
, slug
):
204 post
= self
.backend
.blog
.get_by_slug(slug
)
206 raise tornado
.web
.HTTPError(404)
208 # Check if post is editable
209 if not post
.is_editable(self
.current_user
):
210 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
212 with self
.db
.transaction():
216 self
.redirect("/drafts")
219 class DebugEmailHandler(base
.BaseHandler
):
220 @tornado.web
.authenticated
222 if not self
.current_user
.is_staff():
223 raise tornado
.web
.HTTPError(403)
226 post
= self
.backend
.blog
.get_by_slug(slug
)
228 raise tornado
.web
.HTTPError(404, "Could not find post %s" % slug
)
230 self
.render("blog/messages/announcement.html", account
=self
.current_user
, post
=post
)
233 class HistoryNavigationModule(ui_modules
.UIModule
):
235 return self
.render_string("blog/modules/history-navigation.html",
236 years
=self
.backend
.blog
.years
)
239 class ListModule(ui_modules
.UIModule
):
240 def render(self
, posts
, relative
=False, show_author
=True):
241 return self
.render_string("blog/modules/list.html",
242 posts
=posts
, relative
=relative
, show_author
=show_author
)