agdj/blog/archive.py
author Adam Gomaa <code@adam.gomaa.us>
Sun Feb 01 02:55:17 2009 -0500
changeset 419 b16f4fab397a
child 426 2a86d85cd508
permissions -rw-r--r--
Add per-month archive, only show 10 on the main page.
code@419
     1
"Module to define some objects in for use in the archive pages"
code@419
     2
code@419
     3
code@419
     4
class Archive(object):
code@419
     5
    "An archive for a queryset of objects"
code@419
     6
    def __init__(self, queryset):
code@419
     7
        "Build an Archive object out of the passed queryset"
code@419
     8
        self.queryset = queryset
code@419
     9
code@419
    10
    def __iter__(self):
code@419
    11
        return iter(self.years)
code@419
    12
code@419
    13
    @property
code@419
    14
    def years(self):
code@419
    15
        if not hasattr(self, "_years"):
code@419
    16
            self.build_years()
code@419
    17
        return sorted(self._years.values(), key=lambda y: -y.year)
code@419
    18
code@419
    19
    def build_years(self):
code@419
    20
        self._years = {}
code@419
    21
        for entry in self.queryset:
code@419
    22
            year = entry.pub_date.year
code@419
    23
            month = entry.pub_date.month
code@419
    24
code@419
    25
            if year not in self._years:
code@419
    26
                self._years[year] = Year(year)
code@419
    27
            yearobj = self._years[year]
code@419
    28
code@419
    29
            if month not in yearobj.months:
code@419
    30
                yearobj.months[month] = Month(year, month)
code@419
    31
            monthobj = yearobj.months[month]
code@419
    32
code@419
    33
            monthobj.entries.append(entry)
code@419
    34
code@419
    35
code@419
    36
class Year(object):
code@419
    37
    def __init__(self, year):
code@419
    38
        self.year = year
code@419
    39
        self.months = {}
code@419
    40
code@419
    41
    def __unicode__(self):
code@419
    42
        return unicode(self.year)
code@419
    43
code@419
    44
    def __iter__(self):
code@419
    45
        return iter(sorted(self.months.values(), key=lambda m: m.month))
code@419
    46
code@419
    47
    @property
code@419
    48
    def count(self):
code@419
    49
        return sum([month.count for month in self])
code@419
    50
code@419
    51
code@419
    52
class Month(object):
code@419
    53
    def __init__(self, year, month):
code@419
    54
        self.year = year
code@419
    55
        self.month = month
code@419
    56
        self.entries = []
code@419
    57
code@419
    58
    @property
code@419
    59
    def count(self):
code@419
    60
        return len(self.entries)
code@419
    61
code@419
    62
    def __iter__(self):
code@419
    63
        return iter(sorted(self.entries, key=lambda x: x.pub_date))
code@419
    64
code@419
    65
    def __unicode__(self):
code@419
    66
        from datetime import date
code@419
    67
        return "%s %s" % (date(2000, self.month, 1).strftime("%B"), unicode(self.year))
code@419
    68
code@419
    69
    @property
code@419
    70
    def abbrev(self):
code@419
    71
        from datetime import date
code@419
    72
        return date(2000, self.month, 1).strftime("%b").lower()
code@419
    73
code@419
    74
    @property
code@419
    75
    def url(self):
code@419
    76
        from django.core.urlresolvers import reverse
code@419
    77
code@419
    78
        return reverse("blog-archive-month", args=[self.year, self.abbrev])
code@419
    79