]> git.ipfire.org Git - people/shoehn/ipfire.org.git/commitdiff
planet: Enhance post composition/editing pages.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2013 10:23:24 +0000 (12:23 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 17 Oct 2013 10:23:24 +0000 (12:23 +0200)
templates/admin-planet-compose.html
templates/admin-planet.html
templates/planet/posting.html
webapp/__init__.py
webapp/backend/planet.py
webapp/handlers_admin.py

index edf8fab8114dac0f608c0631092bd6cd6c00ef80..54c791497990d34ff19d61901827fd98b412365e 100644 (file)
@@ -8,7 +8,7 @@
        <form class="form-horizontal" action="" method="POST" name="entry">
                {% raw xsrf_form_html() %}
 
-               {% if entry.id %}
+               {% if entry %}
                        <input type="hidden" name="id" value="{{ entry.id }}">
                {% end %}
 
                        <label class="control-label" for="inputTitle">{{ _("Title") }}</label>
                        <div class="controls">
                                <input class="input-block-level" type="text" name="title" id="inputTitle"
-                                       value="{{ entry.title }}" placeholder="{{ _("Title") }}">
+                                       {% if entry %}value="{{ entry.title }}"{% end %} placeholder="{{ _("Title") }}">
                        </div>
                </div>
 
                <div class="control-group">
                        <div class="controls">
                                <textarea class="input-block-level" name="markdown" rows="12" id="inputMarkdown"
-                                       placeholder="{{ _("Content") }}">{{ entry.markdown }}</textarea>
+                                       placeholder="{{ _("Content") }}">{% if entry %}{{ entry.markdown }}{% end %}</textarea>
                        </div>
                </div>
 
                        <label class="control-label" for="inputTags">{{ _("Tags") }}</label>
                        <div class="controls">
                                <input class="input-block-level planet-search-autocomplete" type="text"
-                                       name="tags" id="inputTags" value="{{ " ".join(entry.tags) }}"
+                                       name="tags" id="inputTags" {% if entry %}value="{{ " ".join(entry.tags) }}"{% end %}
                                        placeholder="{{ _("Tags") }}" autocomplete="off">
                        </div>
                </div>
+
+               <hr>
+
+               <div class="control-group">
+                       <label class="control-label" for="inputStatus">{{ _("Status") }}</label>
+                       <div class="controls">
+                               <label class="radio inline">
+                                       <input type="radio" name="inputStatus" value="draft" {% if not entry or not entry.is_published() %}checked{% end %}>
+                                       {{ _("Draft") }}
+                               </label>
+                               <label class="radio inline">
+                                       <input type="radio" name="inputStatus" value="published" {% if entry and entry.is_published() %}checked{% end %}>
+                                       {{ _("Published") }}
+                               </label>
+                       </div>
+               </div>
+
                <div class="form-actions">
                        <button type="submit" class="btn btn-primary">{{ _("Save") }}</button>
                        <a class="btn" href="javascript:preview();">{{ _("Preview") }}</a>
index def3f3da38eac6af8024fca40bea4921b579b810..a7efed449ec06b511e806491230385fd8ef1d993 100644 (file)
                        </tr>
                        {% for entry in entries %}
                                <tr>
-                                       <td>{{ entry.author.cn }}</td>
-                                       <td><a href="http://planet.ipfire.org/post/{{ entry.slug }}" target="_blank">{{ entry.title }}</a></td>
-                                       <td><a href="/planet/edit/{{ entry.id }}">{{ _("Edit") }}</a></td>
+                                       <td>
+                                               {{ entry.author.cn }}
+                                       </td>
+                                       <td>
+                                               <a href="http://planet.ipfire.org/post/{{ entry.slug }}" target="_blank">{{ entry.title }}</a>
+                                               {% if entry.is_draft() %}
+                                                       <span class="label label-warning">{{ _("Draft") }}</span>
+                                               {% end %}
+                                       </td>
+                                       <td>
+                                               <a href="/planet/edit/{{ entry.slug }}">{{ _("Edit") }}</a>
+                                               {% if entry.is_draft() %}
+                                                       &bull; <a href="/planet/publish/{{ entry.slug }}">{{ _("Publish") }}</a>
+                                               {% end %}
+                                       </td>
                                </tr>
                        {% end %}
                </table>
index 4d769e0c1e223cba252f88733f3dd8926dba6ac8..921ba408cf866da8127a67aa38ceca5794156d5a 100644 (file)
 
        <div class="row">
                <div class="span9">
+                       {% if entry.is_draft() %}
+                               <div class="alert">
+                                       <button type="button" class="close" data-dismiss="alert">&times;</button>
+                                       <strong>{{ _("Heads up!") }}</strong> {{ _("This post is a draft and has not been published, yet.") }}
+                               </div>
+                       {% end %}
+
                        {% raw entry.text %}
                </div>
 
index 30d83fb843f18d9448665b0e6b9594b5ede94c97..54bd5b7e312b04dae1319e6beeb85110b67eea84 100644 (file)
@@ -218,7 +218,8 @@ class Application(tornado.web.Application):
                        # Planet
                        (r"/planet", AdminPlanetHandler),
                        (r"/planet/compose", AdminPlanetComposeHandler),
-                       (r"/planet/edit/([0-9]+)", AdminPlanetEditHandler),
+                       (r"/planet/edit/(.*)", AdminPlanetEditHandler),
+                       (r"/planet/publish/(.*)", AdminPlanetPublishHandler),
                        # Mirrors
                        (r"/mirrors", AdminMirrorsHandler),
                        (r"/mirrors/create", AdminMirrorsCreateHandler),
index e8551865cfe6a6f557a1b25117e8e033e7c90eaa..c34898f0fe097191217cafd4b3ff09a2ab5c03a7 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/python
 
+import datetime
 import re
 import textile
 import tornado.database
@@ -24,17 +25,28 @@ class PlanetEntry(Object):
        def slug(self):
                return self.data.slug
 
-       @property
-       def title(self):
-               return self.data.title
+       def set_title(self, title):
+               if self.title == title:
+                       return
+
+               self.db.execute("UPDATE planet SET title = %s WHERE id = %s", title, self.id)
+               self.data["title"] = title
+
+       title = property(lambda s: s.data.title, set_title)
 
        @property
        def url(self):
                return "http://planet.ipfire.org/post/%s" % self.slug
 
-       @property
-       def published(self):
-               return self.data.published
+       def set_published(self, published):
+               if self.published == published:
+                       return
+
+               self.db.execute("UPDATE planet SET published = %s WHERE id = %s",
+                       published, self.id)
+               self.data["published"] = published
+
+       published = property(lambda s: s.data.published, set_published)
 
        @property
        def year(self):
@@ -48,10 +60,24 @@ class PlanetEntry(Object):
        def updated(self):
                return self.data.updated
 
-       @property
-       def markdown(self):
+       def get_markdown(self):
                return self.data.markdown
 
+       def set_markdown(self, markdown):
+               if self.markdown == markdown:
+                       return
+
+               markup = self.render(markdown)
+               self.db.execute("UPDATE planet SET markdown = %s, markup = %s WHERE id = %s",
+                       markdown, markup, self.id)
+
+               self.data.update({
+                       "markdown" : markdown,
+                       "markup"   : markup,
+               })
+
+       markdown = property(get_markdown, set_markdown)
+
        @property
        def markup(self):
                if self.data.markup:
@@ -78,6 +104,21 @@ class PlanetEntry(Object):
 
                return self.__author
 
+       def set_status(self, status):
+               if self.status == status:
+                       return
+
+               self.db.execute("UPDATE planet SET status = %s WHERE id = %s", status, self.id)
+               self.data["status"] = status
+
+       status = property(lambda s: s.data.status, set_status)
+
+       def is_draft(self):
+               return self.status == "draft"
+
+       def is_published(self):
+               return self.status == "published"
+
        # Tags
 
        def get_tags(self):
@@ -206,6 +247,24 @@ class Planet(Object):
 
                return slug
 
+       def create(self, title, markdown, author, status="published", tags=None, published=None):
+               slug = self._generate_slug(title)
+               markup = self.render(markdown)
+
+               if published is None:
+                       published = datetime.datetime.utcnow()
+
+               id = self.db.execute("INSERT INTO planet(author_id, slug, title, status, \
+                       markdown, markup, published) VALUES(%s, %s, %s, %s, %s, %s, %s)",
+                       author.uid, slug, title, status, markdown, markup, published)
+
+               entry = self.get_entry_by_id(id)
+
+               if tags:
+                       entry.tags = tags
+
+               return entry
+
        def update_entry(self, entry):
                self.db.execute("UPDATE planet SET title = %s, markdown = %s WHERE id = %s",
                        entry.title, entry.markdown, entry.id)
index 7310a43f314eb5a7de47f0757f659cf5239b1f2c..97562c766c552ee964352886e1096efa1ad37976 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/python
 
+import datetime
 import tornado.web
 
 from handlers_base import *
@@ -7,14 +8,6 @@ from handlers_base import *
 import backend
 
 class AdminBaseHandler(BaseHandler):
-       @property
-       def accounts(self):
-               return backend.Accounts()
-
-       @property
-       def planet(self):
-               return backend.Planet()
-
        @property
        def downloads(self):
                return backend.Downloads()
@@ -61,55 +54,72 @@ class AdminApiPlanetRenderMarkupHandler(AdminBaseHandler):
                output = {
                        "html" : self.planet.render(text),
                }
-               self.write(output)
-               self.finish()
+               self.finish(output)
 
 
 class AdminPlanetHandler(AdminBaseHandler):
        @tornado.web.authenticated
        def get(self):
-               entries = self.planet.get_entries(limit=100)
+               entries = self.planet.get_entries(status=None, limit=100)
 
                self.render("admin-planet.html", entries=entries)
 
 
 class AdminPlanetComposeHandler(AdminBaseHandler):
        @tornado.web.authenticated
-       def get(self, id=None):
-               entry = backend.PlanetEntry(self.planet.db)
+       def get(self, slug=None):
+               entry = None
 
-               if id:
-                       entry = self.planet.get_entry_by_id(id)
+               if slug:
+                       entry = self.planet.get_entry_by_slug(slug)
+                       if not entry:
+                               raise tornado.web.HTTPError(404)
 
                self.render("admin-planet-compose.html", entry=entry)
 
        @tornado.web.authenticated
-       def post(self, id=None):
-               id = self.get_argument("id", id)
+       def post(self):
+               title = self.get_argument("title")
+               markdown = self.get_argument("markdown")
+               tags = self.get_argument("tags", "")
 
-               entry = backend.PlanetEntry(self.planet.db)
+               status = self.get_argument("status", "draft")
 
-               if id:
-                       entry = self.planet.get_entry_by_id(id)
+               author = self.accounts.find(self.current_user)
 
-               entry.set("title", self.get_argument("title"))
-               entry.set("markdown", self.get_argument("markdown"))
-               entry.set("author_id", self.current_user)
+               entry = self.planet.create(title=title, markdown=markdown,
+                       author=author, status=status, tags=tags.split())
 
-               if id:
-                       self.planet.update_entry(entry)
-               else:
-                       id = self.planet.save_entry(entry)
-                       entry.id = id
+               self.redirect("/planet")
 
-               tags = self.get_argument("tags", "")
-               entry.tags = tags.split()
+
+class AdminPlanetEditHandler(AdminPlanetComposeHandler):
+       @tornado.web.authenticated
+       def post(self, slug):
+               entry = self.planet.get_entry_by_slug(slug)
+               if not entry:
+                       raise tornado.web.HTTPError(404)
+
+               entry.title = self.get_argument("title")
+               entry.markdown = self.get_argument("markdown")
+               entry.tags = self.get_argument("tags", "").split()
+
+               entry.status = self.get_argument("status", "draft")
 
                self.redirect("/planet")
 
 
-class AdminPlanetEditHandler(AdminPlanetComposeHandler):
-       pass
+class AdminPlanetPublishHandler(AdminBaseHandler):
+       @tornado.web.authenticated
+       def get(self, slug):
+               entry = self.planet.get_entry_by_slug(slug)
+               if not entry:
+                       raise tornado.web.HTTPError(404)
+
+               entry.status = "published"
+               entry.published = datetime.datetime.utcnow()
+
+               self.redirect("/planet")
 
 
 class AdminAccountsBaseHandler(AdminBaseHandler):