]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/blog.py
7 from . import handlers_base
as base
9 from . import ui_modules
11 class IndexHandler(base
.BaseHandler
):
13 posts
= self
.backend
.blog
.get_newest(limit
=3)
15 self
.render("blog/index.html", posts
=posts
)
18 class AuthorHandler(base
.BaseHandler
):
20 author
= self
.accounts
.get_by_uid(uid
)
22 raise tornado
.web
.HTTPError(404, "User is unknown")
24 # Get all posts from this author
25 posts
= self
.backend
.blog
.get_by_author(author
)
27 raise tornado
.web
.HTTPError(404, "User has no posts")
29 self
.render("blog/author.html", author
=author
, posts
=posts
)
32 class FeedHandler(base
.BaseHandler
):
34 posts
= self
.backend
.blog
.get_newest(limit
=50)
37 feed
= self
.render_string("blog/feed.xml", posts
=posts
,
38 now
=email
.utils
.formatdate())
40 # Allow this to be cached for 5 minutes
43 # Set correct content type
44 self
.set_header("Content-Type", "application/rss+xml")
50 class PostHandler(base
.BaseHandler
):
52 post
= self
.backend
.blog
.get_by_slug(slug
, published
=not self
.current_user
)
54 raise tornado
.web
.HTTPError(404)
56 self
.render("blog/post.html", post
=post
)
59 class PublishHandler(base
.BaseHandler
):
60 @tornado.web
.authenticated
62 post
= self
.backend
.blog
.get_by_slug(slug
, published
=not self
.current_user
)
64 raise tornado
.web
.HTTPError(404)
66 # Is the post already published?
67 if post
.is_published():
68 raise tornado
.web
.HTTPError(400, "Post is already published")
70 # XXX Check that we are only publishing our own posts
73 with self
.db
.transaction():
76 self
.redirect("/post/%s" % post
.slug
)
79 class DraftsHandler(base
.BaseHandler
):
80 @tornado.web
.authenticated
82 drafts
= self
.backend
.blog
.get_drafts(author
=self
.current_user
)
84 self
.render("blog/drafts.html", drafts
=drafts
)
87 class SearchHandler(base
.BaseHandler
):
89 q
= self
.get_argument("q")
91 posts
= self
.backend
.blog
.search(q
, limit
=50)
93 raise tornado
.web
.HTTPError(404, "Nothing found")
95 self
.render("blog/search-results.html", q
=q
, posts
=posts
)
98 class TagHandler(base
.BaseHandler
):
100 posts
= self
.backend
.blog
.get_by_tag(tag
)
102 raise tornado
.web
.HTTPError(404, "There are no posts with tag: %s" % tag
)
104 self
.render("blog/tag.html", posts
=list(posts
), tag
=tag
)
107 class YearHandler(base
.BaseHandler
):
109 posts
= self
.backend
.blog
.get_by_year(year
)
111 raise tornado
.web
.HTTPError(404, "There are no posts in %s" % year
)
113 self
.render("blog/year.html", posts
=posts
, year
=year
)
116 class ComposeHandler(base
.BaseHandler
):
117 @tornado.web
.authenticated
119 self
.render("blog/compose.html", post
=None)
121 @tornado.web
.authenticated
123 title
= self
.get_argument("title")
124 text
= self
.get_argument("text")
125 tags
= self
.get_argument("tags", None)
127 with self
.db
.transaction():
128 post
= self
.backend
.blog
.create_post(title
, text
,
129 author
=self
.current_user
, tags
=tags
)
131 self
.redirect("/drafts")
134 class EditHandler(base
.BaseHandler
):
135 @tornado.web
.authenticated
137 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
139 raise tornado
.web
.HTTPError(404)
141 # Check if post is editable
142 if not post
.is_editable(self
.current_user
):
143 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
145 self
.render("blog/compose.html", post
=post
)
147 @tornado.web
.authenticated
148 def post(self
, slug
):
149 post
= self
.backend
.blog
.get_by_slug(slug
, published
=False)
151 raise tornado
.web
.HTTPError(404)
153 # Check if post is editable
154 if not post
.is_editable(self
.current_user
):
155 raise tornado
.web
.HTTPError(403, "%s cannot edit %s" % (self
.current_user
, post
))
157 # Save updated content
158 with self
.db
.transaction():
160 title
= self
.get_argument("title"),
161 text
= self
.get_argument("text"),
162 tags
= self
.get_argument("tags").split(),
165 # Return to blog if the post is already published
166 if post
.is_published():
167 self
.redirect("/post/%s" % post
.slug
)
170 # Otherwise return to drafts
171 self
.redirect("/drafts")
174 class HistoryNavigationModule(ui_modules
.UIModule
):
176 return self
.render_string("blog/modules/history-navigation.html",
177 years
=self
.backend
.blog
.years
)
180 class ListModule(ui_modules
.UIModule
):
181 def render(self
, posts
):
182 return self
.render_string("blog/modules/list.html", posts
=posts
)
185 class PostModule(ui_modules
.UIModule
):
186 def render(self
, post
):
187 return self
.render_string("blog/modules/post.html", post
=post
)
190 class PostsModule(ui_modules
.UIModule
):
191 def render(self
, posts
):
192 return self
.render_string("blog/modules/posts.html", posts
=list(posts
))