]> git.ipfire.org Git - ipfire.org.git/commitdiff
blog: Add dialogue to publish posts at a certain time
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Oct 2018 09:35:14 +0000 (09:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Oct 2018 09:35:14 +0000 (09:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/templates/blog/compose.html
src/templates/blog/modules/post.html
src/templates/blog/publish.html [new file with mode: 0644]
src/web/blog.py

index c864037cdb9c34d6e2e76d64f71f30ec1e61f38f..20f553a3dba84735289f98d313d4334982daa3b3 100644 (file)
@@ -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
index 481886de2d17494dffbcb21facd42bb27f4926e2..8aad111c0188944cae4b05ad7d3697addc5b4612 100644 (file)
                                <button type="submit" class="btn btn-primary btn-block">
                                        {{ _("Save") }}
                                </button>
+
+                               {% if post %}
+                                       <a class="btn btn-outline-primary btn-block" href="/post/{{ post.slug }}/delete">
+                                               {{ _("Delete") }}
+                                       </a>
+                               {% end %}
                        </form>
                </div>
        </div>
index 1bb51aaf0fc99a2119c0a1140921f3b75c81648c..397551c9978d05187316d2e24a17a2b1d61782f8 100644 (file)
                        {% else %}
                                {{ _("Not published") }}
                        {% end %}
-
-                       {% if current_user and current_user == post.author %}
-                               <a href="/post/{{ post.slug }}/edit">{{ _("Edit") }}</a>
-                               <a href="/post/{{ post.slug }}/delete">{{ _("Delete") }}</a>
-                       {% end %}
                </p>
        </div>
 
+       {% if not post.is_published() and post.is_editable(current_user) %}
+               <div class="row">
+                       <div class="col-12 col-md-6 mb-3">
+                               <a class="btn btn-success btn-block" href="/post/{{ post.slug }}/edit">
+                                       <span class="fas fa-edit mr-2"></span> {{ _("Edit") }}
+                               </a>
+                       </div>
+
+                       <div class="col-12 col-md-6 mb-3">
+                               <a class="btn btn-primary btn-block" href="/post/{{ post.slug }}/publish">
+                                       <span class="fas fa-book-reader mr-2"></span> {{ _("Publish") }}
+                               </a>
+                       </div>
+               </div>
+       {% end %}
+
        <div class="blog-content">
                {% raw post.html %}
        </div>
                        </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>
diff --git a/src/templates/blog/publish.html b/src/templates/blog/publish.html
new file mode 100644 (file)
index 0000000..fa29b18
--- /dev/null
@@ -0,0 +1,48 @@
+{% extends "base.html" %}
+
+{% block title %}{{ _("Publish %s") % post.title }}{% end block %}
+
+{% block modal %}
+       <div class="card">
+               <div class="card-body">
+                       <h5 class="card-title mb-1">{{ _("Publish Post") }}</h5>
+                       <h6 class="card-subtitle text-muted mb-3">{{ post.title }}</h6>
+
+                       <form action="" method="POST">
+                               {% raw xsrf_form_html() %}
+
+                               <input type="hidden" name="when">
+
+                               <div class="form-group">
+                                       <label>{{ _("Time to Publish") }}</label>
+
+                                       <input type="datetime-local" class="form-control"
+                                               pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}" required>
+                               </div>
+
+                               <button type="submit" class="btn btn-primary btn-block">{{ _("Publish") }}</button>
+                               <a class="btn btn-secondary btn-block" href="/post/{{ post.slug }}">{{ _("Cancel") }}</a>
+                       </form>
+               </div>
+       </div>
+{% end block %}
+
+{% block javascript %}
+       <script type="text/javascript">
+               $(document).ready(function() {
+                       var when = $("input[name='when']");
+
+                       $("input[type='datetime-local']").on("change keyup", function(event) {
+                               var value = $(this).val();
+
+                               // Parse date and format with timezone
+                               if (value) {
+                                       var date = new Date(value);
+                                       value = date.toISOString();
+                               }
+
+                               when.val(value);
+                       }).change();
+               });
+       </script>
+{% end %}
index d39ef396446714328497f45457e40c2b329ebf4e..ad770da7ffca392c68d067c1f64b76a627f986da 100644 (file)
@@ -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)