agdj/blog/archive.py
changeset 419 b16f4fab397a
child 426 2a86d85cd508
     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 +