Add per-month archive, only show 10 on the main page.
authorAdam Gomaa <code@adam.gomaa.us>
Sun Feb 01 02:55:17 2009 -0500
changeset 419b16f4fab397a
parent 418 c24ca37823b9
child 420 818f476bc716
Add per-month archive, only show 10 on the main page.
agdj/blog/archive.py
agdj/blog/urls.py
agdj/blog/views.py
agdj/core/templates/blog/archive.html
agdj/core/templates/blog/archive_month.html
agdj/core/templates/blog/post_list.html
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/agdj/blog/archive.py	Sun Feb 01 02:55:17 2009 -0500
     1.3 @@ -0,0 +1,79 @@
     1.4 +"Module to define some objects in for use in the archive pages"
     1.5 +
     1.6 +
     1.7 +class Archive(object):
     1.8 +    "An archive for a queryset of objects"
     1.9 +    def __init__(self, queryset):
    1.10 +        "Build an Archive object out of the passed queryset"
    1.11 +        self.queryset = queryset
    1.12 +
    1.13 +    def __iter__(self):
    1.14 +        return iter(self.years)
    1.15 +
    1.16 +    @property
    1.17 +    def years(self):
    1.18 +        if not hasattr(self, "_years"):
    1.19 +            self.build_years()
    1.20 +        return sorted(self._years.values(), key=lambda y: -y.year)
    1.21 +
    1.22 +    def build_years(self):
    1.23 +        self._years = {}
    1.24 +        for entry in self.queryset:
    1.25 +            year = entry.pub_date.year
    1.26 +            month = entry.pub_date.month
    1.27 +
    1.28 +            if year not in self._years:
    1.29 +                self._years[year] = Year(year)
    1.30 +            yearobj = self._years[year]
    1.31 +
    1.32 +            if month not in yearobj.months:
    1.33 +                yearobj.months[month] = Month(year, month)
    1.34 +            monthobj = yearobj.months[month]
    1.35 +
    1.36 +            monthobj.entries.append(entry)
    1.37 +
    1.38 +
    1.39 +class Year(object):
    1.40 +    def __init__(self, year):
    1.41 +        self.year = year
    1.42 +        self.months = {}
    1.43 +
    1.44 +    def __unicode__(self):
    1.45 +        return unicode(self.year)
    1.46 +
    1.47 +    def __iter__(self):
    1.48 +        return iter(sorted(self.months.values(), key=lambda m: m.month))
    1.49 +
    1.50 +    @property
    1.51 +    def count(self):
    1.52 +        return sum([month.count for month in self])
    1.53 +
    1.54 +
    1.55 +class Month(object):
    1.56 +    def __init__(self, year, month):
    1.57 +        self.year = year
    1.58 +        self.month = month
    1.59 +        self.entries = []
    1.60 +
    1.61 +    @property
    1.62 +    def count(self):
    1.63 +        return len(self.entries)
    1.64 +
    1.65 +    def __iter__(self):
    1.66 +        return iter(sorted(self.entries, key=lambda x: x.pub_date))
    1.67 +
    1.68 +    def __unicode__(self):
    1.69 +        from datetime import date
    1.70 +        return "%s %s" % (date(2000, self.month, 1).strftime("%B"), unicode(self.year))
    1.71 +
    1.72 +    @property
    1.73 +    def abbrev(self):
    1.74 +        from datetime import date
    1.75 +        return date(2000, self.month, 1).strftime("%b").lower()
    1.76 +
    1.77 +    @property
    1.78 +    def url(self):
    1.79 +        from django.core.urlresolvers import reverse
    1.80 +
    1.81 +        return reverse("blog-archive-month", args=[self.year, self.abbrev])
    1.82 +
     2.1 --- a/agdj/blog/urls.py	Sun Feb 01 02:07:05 2009 -0500
     2.2 +++ b/agdj/blog/urls.py	Sun Feb 01 02:55:17 2009 -0500
     2.3 @@ -4,17 +4,18 @@
     2.4  from agdj.blog import feeds
     2.5  
     2.6  urlpatterns = patterns(
     2.7 -    '',
     2.8 -    (r'^$', views.post_list, {}, "blog-index"),
     2.9 -    (r'^tag/(?P<tag>[-\w]+)/$', views.view_tag),
    2.10 +    'agdj.blog.views',
    2.11 +    (r'^$', 'post_list', {}, "blog-index"),
    2.12 +    (r'^tag/(?P<tag>[-\w]+)/$', 'view_tag'),
    2.13      (r'^tag/(?P<tag>[-\w]+)/feed/$', feeds.per_tag),
    2.14      (r'^feed/$', feeds.latest),
    2.15      (r'^feed/comments/$', feeds.comments),
    2.16      (r'^feed/latest/$', redirect_to, {"url":"/blog/feed/"}),
    2.17 -    url(r'^(?P<slug>[-\w]+)/$', views.redirect_to_date_version, {}, "redirect-to-date-url"),
    2.18 +    (r'^archive/$', 'archive_index', {}, "blog-archive"),
    2.19 +    (r'^archive/(?P<year>\d{4})/(?P<month>\w+)/$', 'archive_month', {}, "blog-archive-month"),
    2.20 +    url(r'^(?P<slug>[-\w]+)/$', 'redirect_to_date_version', {}, "redirect-to-date-url"),
    2.21      url(r'^(?P<year>\d{4})/(?P<month>\w+)/(?P<day>\d+)/(?P<slug>[-\w]+)/$',
    2.22 -        views.single_post, {},
    2.23 -        "view-blog-post"),
    2.24 +        'single_post', {}, "view-blog-post"),
    2.25  )
    2.26  
    2.27  
     3.1 --- a/agdj/blog/views.py	Sun Feb 01 02:07:05 2009 -0500
     3.2 +++ b/agdj/blog/views.py	Sun Feb 01 02:55:17 2009 -0500
     3.3 @@ -36,10 +36,10 @@
     3.4  
     3.5  
     3.6  @use_template("blog/post_list.html")
     3.7 -def post_list(request):
     3.8 +def post_list(request, limit=10):
     3.9      "List all entries"
    3.10  
    3.11 -    posts = models.Entry.objects.filter(public=True).order_by("-pub_date")
    3.12 +    posts = models.Entry.objects.filter(public=True).order_by("-pub_date")[:limit]
    3.13      return dict(posts=posts)
    3.14  
    3.15  
    3.16 @@ -51,3 +51,24 @@
    3.17      posts = models.Entry.objects.filter(public=True).order_by("-pub_date")
    3.18      posts = TaggedItem.objects.get_by_model(posts, tag)
    3.19      return dict(posts=posts, tag=tag)
    3.20 +
    3.21 +
    3.22 +@use_template("blog/archive.html")
    3.23 +def archive_index(request):
    3.24 +    "Show the multi-year archive"
    3.25 +    from agdj.blog import archive
    3.26 +
    3.27 +    archive = archive.Archive(models.Entry.objects.all())
    3.28 +
    3.29 +    return {"archive": archive}
    3.30 +
    3.31 +
    3.32 +def archive_month(request, year, month):
    3.33 +    "Show the multi-year archive"
    3.34 +    from django.views.generic.date_based import archive_month
    3.35 +    from agdj.blog import archive
    3.36 +    return archive_month(
    3.37 +        # request, year, month, qs, date_field
    3.38 +        request, year, month, models.Entry.objects.all(), "pub_date",
    3.39 +        template_name="blog/archive_month.html",)
    3.40 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/agdj/core/templates/blog/archive.html	Sun Feb 01 02:55:17 2009 -0500
     4.3 @@ -0,0 +1,20 @@
     4.4 +{% extends "blog/blog.html" %}
     4.5 +
     4.6 +{% block title %}204 No Content Archive{% endblock %}
     4.7 +
     4.8 +{% block header %}<h1><a href="{% url blog-index %}">204 No Content</a>
     4.9 +    <a href="{% url blog-archive %}">Archive</a></h1>{% endblock %}
    4.10 +
    4.11 +{% block content %}
    4.12 +<ul class="years">
    4.13 +    {% for year in archive %}
    4.14 +    <li>{{ year }} - {{ year.count }} total
    4.15 +        {% for month in year %}
    4.16 +        <ul class="months">
    4.17 +            <li><a href="{{ month.url }}">{{ month }} ({{ month.count }})</a></li>
    4.18 +        </ul>
    4.19 +        {% endfor %}
    4.20 +    </li>
    4.21 +    {% endfor %}
    4.22 +</ul>
    4.23 +{% endblock %}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/agdj/core/templates/blog/archive_month.html	Sun Feb 01 02:55:17 2009 -0500
     5.3 @@ -0,0 +1,14 @@
     5.4 +{% extends "blog/blog.html" %}
     5.5 +
     5.6 +{% block title %}204 No Content Archive - {{ month|date:"F Y" }}{% endblock %}
     5.7 +
     5.8 +{% block header %}<h1><a href="{% url blog-index %}">204 No Content</a>
     5.9 +    <a href="{% url blog-archive %}">Archive</a> - {{ month|date:"F Y" }}</h1>{% endblock %}
    5.10 +
    5.11 +{% block content %}
    5.12 +<div class="month">
    5.13 +    {% for object in object_list %}
    5.14 +    {% include "blog/render_post_summary.html" %}
    5.15 +    {% endfor %}
    5.16 +</div>
    5.17 +{% endblock %}
     6.1 --- a/agdj/core/templates/blog/post_list.html	Sun Feb 01 02:07:05 2009 -0500
     6.2 +++ b/agdj/core/templates/blog/post_list.html	Sun Feb 01 02:55:17 2009 -0500
     6.3 @@ -2,6 +2,11 @@
     6.4  {% load cache %}
     6.5  
     6.6  {% block content %}
     6.7 +
     6.8 +<div>My 10 most recent posts are listed here. You can 
     6.9 +    <a href="{% url blog-archive %}">view the archive</a> for earlier
    6.10 +    entries.</div>
    6.11 +
    6.12  {% for object in posts %}
    6.13  {% include "blog/render_post_summary.html" %}
    6.14  {% endfor %}