zinnia.templatetags.zcalendar
Covered: 50 lines
Missed: 0 lines
Skipped 13 lines
Percent: 100 %
 1
"""Calendar module for Zinnia templatetags"""
 2
from datetime import date
 3
from calendar import HTMLCalendar
 5
from django.utils.dates import MONTHS
 6
from django.utils.dates import WEEKDAYS_ABBR
 7
from django.utils.formats import get_format
 8
from django.core.urlresolvers import reverse
10
from zinnia.models import Entry
12
AMERICAN_TO_EUROPEAN_WEEK_DAYS = [6, 0, 1, 2, 3, 4, 5]
15
class ZinniaCalendar(HTMLCalendar):
16
    """Override of HTMLCalendar"""
18
    def __init__(self):
19
        """Retrieve and convert the localized first week day
20
        at initialization"""
21
        HTMLCalendar.__init__(self, AMERICAN_TO_EUROPEAN_WEEK_DAYS[
22
            get_format('FIRST_DAY_OF_WEEK')])
24
    def formatday(self, day, weekday):
25
        """Return a day as a table cell with a link
26
        if entries are published this day"""
27
        if day and day in self.day_entries:
28
            day_date = date(self.current_year, self.current_month, day)
29
            archive_day_url = reverse('zinnia_entry_archive_day',
30
                                      args=[day_date.strftime('%Y'),
31
                                            day_date.strftime('%m'),
32
                                            day_date.strftime('%d')])
33
            return '<td class="%s entry"><a href="%s" '\
34
                   'rel="archives">%d</a></td>' % (
35
                self.cssclasses[weekday], archive_day_url, day)
37
        return super(ZinniaCalendar, self).formatday(day, weekday)
39
    def formatmonth(self, theyear, themonth, withyear=True):
40
        """Return a formatted month as a table with
41
        new attributes computed for formatting a day"""
42
        self.current_year = theyear
43
        self.current_month = themonth
44
        self.day_entries = [entries.creation_date.day for entries in
45
                            Entry.published.filter(
46
                                creation_date__year=theyear,
47
                                creation_date__month=themonth)]
49
        return super(ZinniaCalendar, self).formatmonth(
50
            theyear, themonth, withyear)
52
    def formatweekday(self, day):
53
        """Return a weekday name translated
54
        as a table header."""
55
        return '<th class="%s">%s</th>' % (self.cssclasses[day],
56
                                           WEEKDAYS_ABBR[day].title())
58
    def formatmonthname(self, theyear, themonth, withyear=True):
59
        """Return a month name translated
60
        as a table row."""
61
        monthname = '%s %s' % (MONTHS[themonth].title(), theyear)
62
        return '<tr><th colspan="7" class="month">%s</th></tr>' % monthname