From: Michael Tremer Date: Mon, 10 Sep 2018 20:52:28 +0000 (+0100) Subject: blog: Add button to publish a post X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9ea64cef55c9ba1dec8c7a0f87a85671d173c2eb;p=ipfire.org.git blog: Add button to publish a post Signed-off-by: Michael Tremer --- diff --git a/src/backend/blog.py b/src/backend/blog.py index 3184b0f1..f8d6144a 100644 --- a/src/backend/blog.py +++ b/src/backend/blog.py @@ -256,12 +256,12 @@ class Post(misc.Object): """ return self.published_at and self.published_at <= datetime.datetime.now() - def publish(self): + def publish(self, when=None): if self.is_published(): return - self.db.execute("UPDATE blog SET published_at = NOW() \ - WHERE id = %s", self.id) + self.db.execute("UPDATE blog SET published_at = COALESCE(%s, CURRENT_TIMESTAMP) \ + WHERE id = %s", when, self.id) # Update search indices self.backend.blog.refresh() diff --git a/src/templates/blog/modules/post.html b/src/templates/blog/modules/post.html index d5a01269..e9b0abae 100644 --- a/src/templates/blog/modules/post.html +++ b/src/templates/blog/modules/post.html @@ -59,4 +59,16 @@ {% end %} + + {% if not post.is_published() %} +
+ {% raw xsrf_form_html() %} + +
+ +
+
+ {% end %} diff --git a/src/web/__init__.py b/src/web/__init__.py index 7a7ca6f4..2b1d75ea 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -128,6 +128,7 @@ class Application(tornado.web.Application): (r"/drafts", blog.DraftsHandler), (r"/post/([0-9a-z\-\.]+)", blog.PostHandler), (r"/post/([0-9a-z\-\.]+)/edit", blog.EditHandler), + (r"/post/([0-9a-z\-\.]+)/publish", blog.PublishHandler), (r"/search", blog.SearchHandler), (r"/tags/([0-9a-z\-\.]+)", blog.TagHandler), (r"/years/([0-9]+)", blog.YearHandler), diff --git a/src/web/blog.py b/src/web/blog.py index 8c24f3e5..5660a8c2 100644 --- a/src/web/blog.py +++ b/src/web/blog.py @@ -1,5 +1,6 @@ #!/usr/bin/python +import datetime import email.utils import tornado.web @@ -60,6 +61,24 @@ class PostHandler(base.BaseHandler): self.render("blog/post.html", post=post) +class PublishHandler(base.BaseHandler): + @tornado.web.authenticated + def post(self, slug): + post = self.backend.blog.get_by_slug(slug, published=not self.current_user) + if not post: + raise tornado.web.HTTPError(404) + + # Is the post already published? + if post.is_published(): + raise tornado.web.HTTPError(400, "Post is already published") + + # Publish the post + with self.db.transaction(): + post.publish() + + self.redirect("/post/%s" % post.slug) + + class DraftsHandler(base.BaseHandler): @tornado.web.authenticated def get(self):