from django.utils.html import conditional_escape as esc from itertools import groupby from django.utils.safestring import mark_safe from calendar import HTMLCalendar, monthrange from datetime import * class EventCalendar(HTMLCalendar): def __init__(self, pEvents): super(EventCalendar, self).__init__() self.events = self.group_by_day(pEvents) 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.events: cssclass += ' filled' body = [] for ev in self.events[day]: body.append('' % ev.id) body.append(esc(ev.nom)) body.append('
') return self.day_cell(cssclass, '
%d
%s' % (day, ''.join(body))) return self.day_cell(cssclass, '
%d
' % day) return self.day_cell('noday', ' ') def formatmonth(self, year, month): self.year, self.month = year, month return super(EventCalendar, self).formatmonth(year, month) def group_by_day(self, pEvents): field = lambda ev: ev.date.day return dict( [(dat, list(items)) for dat, items in groupby(pEvents, field)] ) def day_cell(self, cssclass, body): return '%s' % (cssclass, body)