Here it is an example of overiding HTMLCalendar to display a queryset.
class QuerysetCalendar(HTMLCalendar):
def __init__(self, queryset, field):
self.field = field
super(QuerysetCalendar, self).__init__()
self.queryset_by_date = self.group_by_day(queryset)
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
cssclass += ' today'
if day in self.queryset_by_date:
cssclass += ' filled'
body = ['<ul>']
for item in self.queryset_by_date[day]:
body.append('<li>')
body.append('<a href="%s">' % item.get_absolute_url())
body.append(esc(item))
body.append('</a></li>')
body.append('</ul>')
return self.day_cell(cssclass, '%d %s' % (day, ''.join(body)))
return self.day_cell(cssclass, day)
return self.day_cell('noday', ' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(QuerysetCalendar, self).formatmonth(year, month)
def group_by_day(self, queryset):
field = lambda item: getattr(item, self.field).day
return dict(
[(day, list(items)) for day, items in groupby(queryset, field)]
)
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)