]> git.ipfire.org Git - ipfire.org.git/commitdiff
blog: Add button to publish a post
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Sep 2018 20:52:28 +0000 (21:52 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 10 Sep 2018 20:52:28 +0000 (21:52 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/blog.py
src/templates/blog/modules/post.html
src/web/__init__.py
src/web/blog.py

index 3184b0f167dcd47636b34253772cfa473fcac0e5..f8d6144acd1e418cf8ba64f281eab41084d5a1cd 100644 (file)
@@ -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()
index d5a0126995194d8c0c8913598c7be321ff3b9667..e9b0abaeaf16eeda57a070abab06572b33ef9a27 100644 (file)
                        </a>
                {% end %}
        </div>
+
+       {% if not post.is_published() %}
+               <form action="/post/{{ post.slug }}/publish" method="POST">
+                       {% raw xsrf_form_html() %}
+
+                       <div class="btn-toolbar mt-5">
+                               <button type="submit" class="btn btn-primary btn-block btn-lg">
+                                       {{ _("Publish") }}
+                               </button>
+                       </div>
+               </form>
+       {% end %}
 </div>
index 7a7ca6f489bdd2ff1189946e16cf0d7a7fca18bc..2b1d75ea18214f222bb3bdbc28d886a36d2a9e99 100644 (file)
@@ -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),
index 8c24f3e5787f6ff85820936361441b86437e2b56..5660a8c20e92d1dfd3a2cbf3653f635fad1ae19a 100644 (file)
@@ -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):