From: Michael Tremer Date: Thu, 17 Oct 2013 10:23:24 +0000 (+0200) Subject: planet: Enhance post composition/editing pages. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=67ab72b8f6b365f9f0ad208aa2b998e1c629d344;p=ipfire.org.git planet: Enhance post composition/editing pages. --- diff --git a/templates/admin-planet-compose.html b/templates/admin-planet-compose.html index edf8fab8..54c79149 100644 --- a/templates/admin-planet-compose.html +++ b/templates/admin-planet-compose.html @@ -8,7 +8,7 @@
{% raw xsrf_form_html() %} - {% if entry.id %} + {% if entry %} {% end %} @@ -16,14 +16,14 @@
+ {% if entry %}value="{{ entry.title }}"{% end %} placeholder="{{ _("Title") }}">
+ placeholder="{{ _("Content") }}">{% if entry %}{{ entry.markdown }}{% end %}
@@ -33,10 +33,27 @@
+ +
+ +
+ +
+ + +
+
+
{{ _("Preview") }} diff --git a/templates/admin-planet.html b/templates/admin-planet.html index def3f3da..a7efed44 100644 --- a/templates/admin-planet.html +++ b/templates/admin-planet.html @@ -16,9 +16,21 @@ {% for entry in entries %} - {{ entry.author.cn }} - {{ entry.title }} - {{ _("Edit") }} + + {{ entry.author.cn }} + + + {{ entry.title }} + {% if entry.is_draft() %} + {{ _("Draft") }} + {% end %} + + + {{ _("Edit") }} + {% if entry.is_draft() %} + • {{ _("Publish") }} + {% end %} + {% end %} diff --git a/templates/planet/posting.html b/templates/planet/posting.html index 4d769e0c..921ba408 100644 --- a/templates/planet/posting.html +++ b/templates/planet/posting.html @@ -11,6 +11,13 @@
+ {% if entry.is_draft() %} +
+ + {{ _("Heads up!") }} {{ _("This post is a draft and has not been published, yet.") }} +
+ {% end %} + {% raw entry.text %}
diff --git a/webapp/__init__.py b/webapp/__init__.py index 30d83fb8..54bd5b7e 100644 --- a/webapp/__init__.py +++ b/webapp/__init__.py @@ -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), diff --git a/webapp/backend/planet.py b/webapp/backend/planet.py index e8551865..c34898f0 100644 --- a/webapp/backend/planet.py +++ b/webapp/backend/planet.py @@ -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) diff --git a/webapp/handlers_admin.py b/webapp/handlers_admin.py index 7310a43f..97562c76 100644 --- a/webapp/handlers_admin.py +++ b/webapp/handlers_admin.py @@ -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):