# HG changeset patch # User Adam Gomaa # Date 1233474917 18000 # Node ID b16f4fab397ac772ce610afe45284b68b998bd0b # Parent c24ca37823b99e68086cf15284a2180e95bc058e Add per-month archive, only show 10 on the main page. diff -r c24ca37823b9 -r b16f4fab397a agdj/blog/archive.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agdj/blog/archive.py Sun Feb 01 02:55:17 2009 -0500 @@ -0,0 +1,79 @@ +"Module to define some objects in for use in the archive pages" + + +class Archive(object): + "An archive for a queryset of objects" + def __init__(self, queryset): + "Build an Archive object out of the passed queryset" + self.queryset = queryset + + def __iter__(self): + return iter(self.years) + + @property + def years(self): + if not hasattr(self, "_years"): + self.build_years() + return sorted(self._years.values(), key=lambda y: -y.year) + + def build_years(self): + self._years = {} + for entry in self.queryset: + year = entry.pub_date.year + month = entry.pub_date.month + + if year not in self._years: + self._years[year] = Year(year) + yearobj = self._years[year] + + if month not in yearobj.months: + yearobj.months[month] = Month(year, month) + monthobj = yearobj.months[month] + + monthobj.entries.append(entry) + + +class Year(object): + def __init__(self, year): + self.year = year + self.months = {} + + def __unicode__(self): + return unicode(self.year) + + def __iter__(self): + return iter(sorted(self.months.values(), key=lambda m: m.month)) + + @property + def count(self): + return sum([month.count for month in self]) + + +class Month(object): + def __init__(self, year, month): + self.year = year + self.month = month + self.entries = [] + + @property + def count(self): + return len(self.entries) + + def __iter__(self): + return iter(sorted(self.entries, key=lambda x: x.pub_date)) + + def __unicode__(self): + from datetime import date + return "%s %s" % (date(2000, self.month, 1).strftime("%B"), unicode(self.year)) + + @property + def abbrev(self): + from datetime import date + return date(2000, self.month, 1).strftime("%b").lower() + + @property + def url(self): + from django.core.urlresolvers import reverse + + return reverse("blog-archive-month", args=[self.year, self.abbrev]) + diff -r c24ca37823b9 -r b16f4fab397a agdj/blog/urls.py --- a/agdj/blog/urls.py Sun Feb 01 02:07:05 2009 -0500 +++ b/agdj/blog/urls.py Sun Feb 01 02:55:17 2009 -0500 @@ -4,17 +4,18 @@ from agdj.blog import feeds urlpatterns = patterns( - '', - (r'^$', views.post_list, {}, "blog-index"), - (r'^tag/(?P[-\w]+)/$', views.view_tag), + 'agdj.blog.views', + (r'^$', 'post_list', {}, "blog-index"), + (r'^tag/(?P[-\w]+)/$', 'view_tag'), (r'^tag/(?P[-\w]+)/feed/$', feeds.per_tag), (r'^feed/$', feeds.latest), (r'^feed/comments/$', feeds.comments), (r'^feed/latest/$', redirect_to, {"url":"/blog/feed/"}), - url(r'^(?P[-\w]+)/$', views.redirect_to_date_version, {}, "redirect-to-date-url"), + (r'^archive/$', 'archive_index', {}, "blog-archive"), + (r'^archive/(?P\d{4})/(?P\w+)/$', 'archive_month', {}, "blog-archive-month"), + url(r'^(?P[-\w]+)/$', 'redirect_to_date_version', {}, "redirect-to-date-url"), url(r'^(?P\d{4})/(?P\w+)/(?P\d+)/(?P[-\w]+)/$', - views.single_post, {}, - "view-blog-post"), + 'single_post', {}, "view-blog-post"), ) diff -r c24ca37823b9 -r b16f4fab397a agdj/blog/views.py --- a/agdj/blog/views.py Sun Feb 01 02:07:05 2009 -0500 +++ b/agdj/blog/views.py Sun Feb 01 02:55:17 2009 -0500 @@ -36,10 +36,10 @@ @use_template("blog/post_list.html") -def post_list(request): +def post_list(request, limit=10): "List all entries" - posts = models.Entry.objects.filter(public=True).order_by("-pub_date") + posts = models.Entry.objects.filter(public=True).order_by("-pub_date")[:limit] return dict(posts=posts) @@ -51,3 +51,24 @@ posts = models.Entry.objects.filter(public=True).order_by("-pub_date") posts = TaggedItem.objects.get_by_model(posts, tag) return dict(posts=posts, tag=tag) + + +@use_template("blog/archive.html") +def archive_index(request): + "Show the multi-year archive" + from agdj.blog import archive + + archive = archive.Archive(models.Entry.objects.all()) + + return {"archive": archive} + + +def archive_month(request, year, month): + "Show the multi-year archive" + from django.views.generic.date_based import archive_month + from agdj.blog import archive + return archive_month( + # request, year, month, qs, date_field + request, year, month, models.Entry.objects.all(), "pub_date", + template_name="blog/archive_month.html",) + diff -r c24ca37823b9 -r b16f4fab397a agdj/core/templates/blog/archive.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agdj/core/templates/blog/archive.html Sun Feb 01 02:55:17 2009 -0500 @@ -0,0 +1,20 @@ +{% extends "blog/blog.html" %} + +{% block title %}204 No Content Archive{% endblock %} + +{% block header %}

204 No Content + Archive

{% endblock %} + +{% block content %} +
    + {% for year in archive %} +
  • {{ year }} - {{ year.count }} total + {% for month in year %} + + {% endfor %} +
  • + {% endfor %} +
+{% endblock %} diff -r c24ca37823b9 -r b16f4fab397a agdj/core/templates/blog/archive_month.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agdj/core/templates/blog/archive_month.html Sun Feb 01 02:55:17 2009 -0500 @@ -0,0 +1,14 @@ +{% extends "blog/blog.html" %} + +{% block title %}204 No Content Archive - {{ month|date:"F Y" }}{% endblock %} + +{% block header %}

204 No Content + Archive - {{ month|date:"F Y" }}

{% endblock %} + +{% block content %} +
+ {% for object in object_list %} + {% include "blog/render_post_summary.html" %} + {% endfor %} +
+{% endblock %} diff -r c24ca37823b9 -r b16f4fab397a agdj/core/templates/blog/post_list.html --- a/agdj/core/templates/blog/post_list.html Sun Feb 01 02:07:05 2009 -0500 +++ b/agdj/core/templates/blog/post_list.html Sun Feb 01 02:55:17 2009 -0500 @@ -2,6 +2,11 @@ {% load cache %} {% block content %} + +
My 10 most recent posts are listed here. You can + view the archive for earlier + entries.
+ {% for object in posts %} {% include "blog/render_post_summary.html" %} {% endfor %}