From 6f14456de221d151dd78a054442344fd5774ccdb Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Wed, 27 Aug 2014 13:56:06 -0700 Subject: [PATCH] Add /blog/archive endpoint --- rophako/modules/blog/__init__.py | 43 +++++++++++++++++++ .../modules/blog/templates/blog/archive.html | 18 ++++++++ 2 files changed, 61 insertions(+) create mode 100644 rophako/modules/blog/templates/blog/archive.html diff --git a/rophako/modules/blog/__init__.py b/rophako/modules/blog/__init__.py index 5fc323b..341bd5c 100644 --- a/rophako/modules/blog/__init__.py +++ b/rophako/modules/blog/__init__.py @@ -26,6 +26,49 @@ def index(): return template("blog/index.html") +@mod.route("/archive") +def archive(): + """List all blog posts over time on one page.""" + index = Blog.get_index() + + # Group by calendar month, and keep track of friendly versions of months. + groups = dict() + friendly_months = dict() + for post_id, data in index.items(): + time = datetime.datetime.fromtimestamp(data["time"]) + date = time.strftime("%Y-%m") + if not date in groups: + groups[date] = dict() + friendly = time.strftime("%B %Y") + friendly_months[date] = friendly + + # Get author's profile && Pretty-print the time. + data["profile"] = User.get_user(uid=data["author"]) + data["pretty_time"] = pretty_time(BLOG_TIME_FORMAT, data["time"]) + groups[date][post_id] = data + + # Sort by calendar month. + sort_months = sorted(groups.keys(), reverse=True) + + # Prepare the results. + result = list() + for month in sort_months: + data = dict( + month=month, + month_friendly=friendly_months[month], + posts=list() + ) + + # Sort the posts by time created, descending. + for post_id in sorted(groups[month].keys(), key=lambda x: groups[month][x]["time"], reverse=True): + data["posts"].append(groups[month][post_id]) + + result.append(data) + + g.info["archive"] = result + return template("blog/archive.html") + + @mod.route("/category/") def category(category): g.info["url_category"] = category diff --git a/rophako/modules/blog/templates/blog/archive.html b/rophako/modules/blog/templates/blog/archive.html new file mode 100644 index 0000000..e16a6d6 --- /dev/null +++ b/rophako/modules/blog/templates/blog/archive.html @@ -0,0 +1,18 @@ +{% extends "layout.html" %} +{% block title %}Blog Archive{% endblock %} +{% block content %} + +{% for date in archive %} +

{{ date["month_friendly"] }}

+ + +{% endfor %} + +{% endblock %} \ No newline at end of file