From 9fa06206686208b01618f6d59561c4a65bb035da Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 30 Oct 2018 09:35:14 +0000 Subject: [PATCH] blog: Add dialogue to publish posts at a certain time Signed-off-by: Michael Tremer --- Makefile.am | 1 + src/templates/blog/compose.html | 6 ++++ src/templates/blog/modules/post.html | 33 ++++++++++--------- src/templates/blog/publish.html | 48 ++++++++++++++++++++++++++++ src/web/blog.py | 29 +++++++++++++++-- 5 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 src/templates/blog/publish.html diff --git a/Makefile.am b/Makefile.am index c864037c..20f553a3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -117,6 +117,7 @@ templates_blog_DATA = \ src/templates/blog/feed.xml \ src/templates/blog/index.html \ src/templates/blog/post.html \ + src/templates/blog/publish.html \ src/templates/blog/search-results.html \ src/templates/blog/tag.html \ src/templates/blog/year.html diff --git a/src/templates/blog/compose.html b/src/templates/blog/compose.html index 481886de..8aad111c 100644 --- a/src/templates/blog/compose.html +++ b/src/templates/blog/compose.html @@ -29,6 +29,12 @@ + + {% if post %} + + {{ _("Delete") }} + + {% end %} diff --git a/src/templates/blog/modules/post.html b/src/templates/blog/modules/post.html index 1bb51aaf..397551c9 100644 --- a/src/templates/blog/modules/post.html +++ b/src/templates/blog/modules/post.html @@ -29,14 +29,25 @@ {% else %} {{ _("Not published") }} {% end %} - - {% if current_user and current_user == post.author %} - {{ _("Edit") }} - {{ _("Delete") }} - {% end %}

+ {% if not post.is_published() and post.is_editable(current_user) %} + + {% end %} +
{% raw post.html %}
@@ -60,16 +71,4 @@ {% end %} - - {% if not post.is_published() %} -
- {% raw xsrf_form_html() %} - -
- -
-
- {% end %} diff --git a/src/templates/blog/publish.html b/src/templates/blog/publish.html new file mode 100644 index 00000000..fa29b18c --- /dev/null +++ b/src/templates/blog/publish.html @@ -0,0 +1,48 @@ +{% extends "base.html" %} + +{% block title %}{{ _("Publish %s") % post.title }}{% end block %} + +{% block modal %} +
+
+
{{ _("Publish Post") }}
+
{{ post.title }}
+ +
+ {% raw xsrf_form_html() %} + + + +
+ + + +
+ + + {{ _("Cancel") }} +
+
+
+{% end block %} + +{% block javascript %} + +{% end %} diff --git a/src/web/blog.py b/src/web/blog.py index d39ef396..ad770da7 100644 --- a/src/web/blog.py +++ b/src/web/blog.py @@ -1,6 +1,7 @@ #!/usr/bin/python import datetime +import dateutil import email.utils import tornado.web @@ -69,21 +70,43 @@ class PostHandler(auth.CacheMixin, base.BaseHandler): class PublishHandler(auth.CacheMixin, base.BaseHandler): + @tornado.web.authenticated + def get(self, slug): + post = self.backend.blog.get_by_slug(slug, published=False) + if not post: + raise tornado.web.HTTPError(404) + + # Check if current_user is allowed to edit the post + if not post.is_editable(self.current_user): + raise tornado.web.HTTPError(403) + + # Is the post already published? + if post.is_published(): + raise tornado.web.HTTPError(400, "Post is already published") + + self.render("blog/publish.html", post=post) + @tornado.web.authenticated def post(self, slug): - post = self.backend.blog.get_by_slug(slug, published=not self.current_user) + post = self.backend.blog.get_by_slug(slug, published=False) if not post: raise tornado.web.HTTPError(404) + # Check if current_user is allowed to edit the post + if not post.is_editable(self.current_user): + raise tornado.web.HTTPError(403) + # Is the post already published? if post.is_published(): raise tornado.web.HTTPError(400, "Post is already published") - # XXX Check that we are only publishing our own posts + when = self.get_argument("when", None) + if when: + when = dateutil.parser.parse(when) # Publish the post with self.db.transaction(): - post.publish() + post.publish(when) self.redirect("/post/%s" % post.slug) -- 2.39.2