]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/blog.py
65a73a6d736d5dde779ab32a272f9125e37fae2b
9 from . import ui_modules
11 class IndexHandler(auth
.CacheMixin
, base
.BaseHandler
):
13 posts
= self
.backend
.blog
.get_newest(limit
=3)
15 # Allow this to be cached for 5 minutes
16 if not self
.current_user
:
19 self
.render("blog/index.html", posts
=posts
)
22 class AuthorHandler(auth
.CacheMixin
, base
.BaseHandler
):
24 author
= self
.accounts
.get_by_uid(uid
)
26 raise tornado
.web
.HTTPError(404, "User is unknown")
28 # Get all posts from this author
29 posts
= self
.backend
.blog
.get_by_author(author
)
31 raise tornado
.web
.HTTPError(404, "User has no posts")
33 # Allow this to be cached for 10 minutes
34 if not self
.current_user
:
37 self
.render("blog/author.html", author
=author
, posts
=posts
)
40 class FeedHandler(base
.BaseHandler
):
42 posts
= self
.backend
.blog
.get_newest(limit
=50)
45 feed
= self
.render_string("blog/feed.xml", posts
=posts
,
46 now
=email
.utils
.formatdate())
48 # Allow this to be cached for 10 minutes
51 # Set correct content type
52 self
.set_header("Content-Type", "application/rss+xml")
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
=not self
.current_user
)
76 raise tornado
.web
.HTTPError(404)
78 # Is the post already published?
79 if post
.is_published():
80 raise tornado
.web
.HTTPError(400, "Post is already published")
82 # XXX Check that we are only publishing our own posts
85 with self
.db
.transaction():
88 self
.redirect("/post/%s" % post
.slug
)
91 class DraftsHandler(auth
.CacheMixin
, base
.BaseHandler
):
92 @tornado.web
.authenticated
94 drafts
= self
.backend
.blog
.get_drafts(author
=self
.current_user
)
96 self
.render("blog/drafts.html", drafts
=drafts
)
99 class SearchHandler(auth
.CacheMixin
, base
.BaseHandler
):
101 q
= self
.get_argument("q")
103 posts
= self
.backend
.blog
.search(q
, limit
=50)
105 raise tornado
.web
.HTTPError(404, "Nothing found")
107 self
.render("blog/search-results.html", q
=q
, posts
=posts
)
110 class TagHandler(auth
.CacheMixin
, base
.BaseHandler
):
112 posts
= self
.backend
.blog
.get_by_tag(tag
)
114 raise tornado
.web
.HTTPError(404, "There are no posts with tag: %s" % tag
)
116 # Allow this to be cached for 10 minutes
117 self
.set_expires(600)
119 self
.render("blog/tag.html", posts
=list(posts
), tag
=tag
)
122 class YearHandler(auth
.CacheMixin
, base
.BaseHandler
):
124 posts
= self
.backend
.blog
.get_by_year(year
)
126 raise tornado
.web
.HTTPError(404, "There are no posts in %s" % year
)
128 # Allow this to be cached for 10 minutes
129 self
.set_expires(600)
131 self
.render("blog/year.html", posts
=posts
, year
=year
)
134 class ComposeHandler(auth
.CacheMixin
, base
.BaseHandler
):
135 @tornado.web
.authenticated
137 self
.render("blog/compose.html", post
=None)
139 @tornado.web
.authenticated
141 title
= self
.get_argument("title")
142 text
= self
.get_argument("text")
143 tags
= self
.get_argument("tags", "").split(" ")
145 with self
.db
.transaction():
146 post
= self
.backend
.blog
.create_post(title
, text
,
147 author
=self
.current_user
, tags
=tags
)
149 self
.redirect("/drafts")
152 class EditHandler(auth
.CacheMixin
, base
.BaseHandler
):
153 @tornado.web
.authenticated
155 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
157 raise tornado
.web
.HTTPError(404)
159 # Check if post is editable
160 if not post
.is_editable(self
.current_user
):
161 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
163 self
.render("blog/compose.html", post
=post
)
165 @tornado.web
.authenticated
166 def post(self
, slug
):
167 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
169 raise tornado
.web
.HTTPError(404)
171 # Check if post is editable
172 if not post
.is_editable(self
.current_user
):
173 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
175 # Save updated content
176 with self
.db
.transaction():
178 title
= self
.get_argument("title"),
179 text
= self
.get_argument("text"),
180 tags
= self
.get_argument("tags", "").split(" "),
183 # Return to blog if the post is already published
184 if post
.is_published():
185 self
.redirect("/post/%s" % post
.slug
)
188 # Otherwise return to drafts
189 self
.redirect("/drafts")
192 class HistoryNavigationModule(ui_modules
.UIModule
):
194 return self
.render_string("blog/modules/history-navigation.html",
195 years
=self
.backend
.blog
.years
)
198 class ListModule(ui_modules
.UIModule
):
199 def render(self
, posts
):
200 return self
.render_string("blog/modules/list.html", posts
=posts
)
203 class PostModule(ui_modules
.UIModule
):
204 def render(self
, post
):
205 return self
.render_string("blog/modules/post.html", post
=post
)
208 class PostsModule(ui_modules
.UIModule
):
209 def render(self
, posts
):
210 return self
.render_string("blog/modules/posts.html", posts
=list(posts
))