2015-04-13 18:56:43 +02:00
|
|
|
from django.utils.html import conditional_escape as esc
|
|
|
|
from itertools import groupby
|
2016-07-14 01:58:52 +02:00
|
|
|
from calendar import HTMLCalendar
|
|
|
|
from datetime import date
|
|
|
|
|
2015-04-13 18:56:43 +02:00
|
|
|
|
|
|
|
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]:
|
2015-07-22 22:08:59 +02:00
|
|
|
body.append('<a href="/calendar/%s">' % ev.id)
|
2016-06-21 00:33:32 +02:00
|
|
|
body.append(esc(ev.nom))
|
2015-04-13 18:56:43 +02:00
|
|
|
body.append('</a><br/>')
|
2016-07-14 01:58:52 +02:00
|
|
|
return self.day_cell(cssclass,
|
|
|
|
'<div class="dayNumber">%d</div> %s'
|
|
|
|
% (day, ''.join(body)))
|
|
|
|
return self.day_cell(cssclass,
|
|
|
|
'<div class="dayNumber">%d</div>' % day)
|
2015-04-13 18:56:43 +02:00
|
|
|
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):
|
2016-07-14 01:58:52 +02:00
|
|
|
def field(ev): ev.date.day
|
2015-04-13 18:56:43 +02:00
|
|
|
return dict(
|
|
|
|
[(dat, list(items)) for dat, items in groupby(pEvents, field)]
|
|
|
|
)
|
|
|
|
|
|
|
|
def day_cell(self, cssclass, body):
|
|
|
|
return '<td class="%s">%s</td>' % (cssclass, body)
|