From 7e64f6a309f7a32aab6f6265c5ff4753eb74b346 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 18 Jul 2018 10:48:02 +0100 Subject: [PATCH] blog: Add history pages Signed-off-by: Michael Tremer --- Makefile.am | 5 +++- src/backend/blog.py | 24 ++++++++++++++++++- src/templates/blog/author.html | 9 +------ src/templates/blog/base.html | 2 ++ .../blog/modules/history-navigation.html | 13 ++++++++++ src/templates/blog/modules/list.html | 8 +++++++ src/templates/blog/year.html | 13 ++++++++++ src/web/__init__.py | 3 +++ src/web/blog.py | 20 ++++++++++++++++ src/web/ui_modules.py | 4 ++++ 10 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 src/templates/blog/modules/history-navigation.html create mode 100644 src/templates/blog/modules/list.html create mode 100644 src/templates/blog/year.html diff --git a/Makefile.am b/Makefile.am index 8b9d93bb..522d80ee 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,11 +112,14 @@ templates_blog_DATA = \ src/templates/blog/index.html \ src/templates/blog/post.html \ src/templates/blog/search-results.html \ - src/templates/blog/tag.html + src/templates/blog/tag.html \ + src/templates/blog/year.html templates_blogdir = $(templatesdir)/blog templates_blog_modules_DATA = \ + src/templates/blog/modules/history-navigation.html \ + src/templates/blog/modules/list.html \ src/templates/blog/modules/post.html \ src/templates/blog/modules/posts.html diff --git a/src/backend/blog.py b/src/backend/blog.py index 3b02acf1..a2874916 100644 --- a/src/backend/blog.py +++ b/src/backend/blog.py @@ -1,5 +1,7 @@ #!/usr/bin/python +import textile + from . import misc class Blog(misc.Object): @@ -43,6 +45,13 @@ class Blog(misc.Object): AND published_at <= NOW() \ ORDER BY published_at DESC LIMIT %s", uid, limit) + def get_by_year(self, year): + return self._get_posts("SELECT * FROM blog \ + WHERE EXTRACT(year FROM published_at) = %s \ + AND published_at IS NOT NULL \ + AND published_at <= NOW() \ + ORDER BY published_at DESC", year) + def search(self, query, limit=None): return self._get_posts("SELECT blog.* FROM blog \ LEFT JOIN blog_search_index search_index ON blog.id = search_index.post_id \ @@ -57,6 +66,15 @@ class Blog(misc.Object): """ self.db.execute("REFRESH MATERIALIZED VIEW blog_search_index") + @property + def years(self): + res = self.db.query("SELECT DISTINCT EXTRACT(year FROM published_at)::integer AS year \ + FROM blog WHERE published_at IS NOT NULL AND published_at <= NOW() \ + ORDER BY year DESC") + + for row in res: + yield row.year + class Post(misc.Object): def init(self, id, data=None): @@ -85,12 +103,16 @@ class Post(misc.Object): def published_at(self): return self.data.published_at + @property + def markdown(self): + return self.data.markdown + @property def html(self): """ Returns this post as rendered HTML """ - return self.data.html + return self.data.html or textile.textile(self.markdown.decode("utf-8")) @property def tags(self): diff --git a/src/templates/blog/author.html b/src/templates/blog/author.html index dc50dcaa..46ed84cf 100644 --- a/src/templates/blog/author.html +++ b/src/templates/blog/author.html @@ -7,14 +7,7 @@
{{ _("%s's posts") % author.name }}
- {% for post in posts %} - - {{ post.title }} - -

- {{ locale.format_date(post.published_at, shorter=True, relative=False) }} -

- {% end %} + {% module BlogList(posts) %}
{% end block %} diff --git a/src/templates/blog/base.html b/src/templates/blog/base.html index 2f336071..c6924145 100644 --- a/src/templates/blog/base.html +++ b/src/templates/blog/base.html @@ -39,6 +39,8 @@ + + {% module BlogHistoryNavigation() %}
diff --git a/src/templates/blog/modules/history-navigation.html b/src/templates/blog/modules/history-navigation.html new file mode 100644 index 00000000..1ca9e0f5 --- /dev/null +++ b/src/templates/blog/modules/history-navigation.html @@ -0,0 +1,13 @@ +

{{ _("History") }}

+ + diff --git a/src/templates/blog/modules/list.html b/src/templates/blog/modules/list.html new file mode 100644 index 00000000..37e122ff --- /dev/null +++ b/src/templates/blog/modules/list.html @@ -0,0 +1,8 @@ +{% for post in posts %} + + {{ post.title }} + +

+ {{ locale.format_date(post.published_at, shorter=True, relative=False) }} +

+{% end %} diff --git a/src/templates/blog/year.html b/src/templates/blog/year.html new file mode 100644 index 00000000..5d36007e --- /dev/null +++ b/src/templates/blog/year.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} + +{% block title %}{{ _("Posts in %s") % year }}{% end block %} + +{% block main %} +
+
+
{{ _("Posts in %s") % year }}
+ + {% module BlogList(posts) %} +
+
+{% end block %} diff --git a/src/web/__init__.py b/src/web/__init__.py index 48590ab5..1c619adf 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -39,6 +39,8 @@ class Application(tornado.web.Application): "format_month_name" : self.format_month_name, }, "ui_modules" : { + "BlogHistoryNavigation": blog.HistoryNavigationModule, + "BlogList" : blog.ListModule, "BlogPost" : blog.PostModule, "BlogPosts" : blog.PostsModule, @@ -121,6 +123,7 @@ class Application(tornado.web.Application): (r"/post/(.*)", blog.PostHandler), (r"/search", blog.SearchHandler), (r"/tags/([0-9a-z\-]+)", blog.TagHandler), + (r"/years/([0-9]+)", blog.YearHandler), # RSS Feed (r"/feed.xml", blog.FeedHandler), diff --git a/src/web/blog.py b/src/web/blog.py index 52540721..bb2994b1 100644 --- a/src/web/blog.py +++ b/src/web/blog.py @@ -80,6 +80,26 @@ class TagHandler(base.BaseHandler): self.render("blog/tag.html", posts=posts, tag=tag) +class YearHandler(base.BaseHandler): + def get(self, year): + posts = self.backend.blog.get_by_year(year) + if not posts: + raise tornado.web.HTTPError(404, "There are no posts in %s" % year) + + self.render("blog/year.html", posts=posts, year=year) + + +class HistoryNavigationModule(ui_modules.UIModule): + def render(self): + return self.render_string("blog/modules/history-navigation.html", + years=self.backend.blog.years) + + +class ListModule(ui_modules.UIModule): + def render(self, posts): + return self.render_string("blog/modules/list.html", posts=posts) + + class PostModule(ui_modules.UIModule): def render(self, post): return self.render_string("blog/modules/post.html", post=post) diff --git a/src/web/ui_modules.py b/src/web/ui_modules.py index 1295e72c..b05440b5 100644 --- a/src/web/ui_modules.py +++ b/src/web/ui_modules.py @@ -16,6 +16,10 @@ import unicodedata from .. import database class UIModule(tornado.web.UIModule): + @property + def backend(self): + return self.handler.backend + @property def accounts(self): return self.handler.accounts -- 2.39.2