ernestophone.ens.fr/calendrier/calend.py

45 lines
1.6 KiB
Python
Raw Normal View History

from calendar import HTMLCalendar
from datetime import date
2021-04-29 00:27:33 +02:00
from itertools import groupby
2021-04-29 00:27:33 +02:00
from django.utils.html import conditional_escape as esc
from django.utils.translation import gettext_lazy as _
2015-04-13 18:56:43 +02:00
2021-04-29 00:27:33 +02:00
class EventCalendar(HTMLCalendar):
2015-04-13 18:56:43 +02:00
def __init__(self, pEvents):
super(EventCalendar, self).__init__()
2021-04-29 00:27:33 +02:00
self.events = self.group_by_day(pEvents.order_by("date"))
2015-04-13 18:56:43 +02:00
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
2021-04-29 00:27:33 +02:00
cssclass += " today"
2015-04-13 18:56:43 +02:00
if day in self.events:
2021-04-29 00:27:33 +02:00
cssclass += " filled"
2015-04-13 18:56:43 +02:00
body = []
for ev in self.events[day]:
body.append(_('<a href="/agenda/') + '%s">' % ev.id)
body.append(esc(ev.nom))
2021-04-29 00:27:33 +02:00
body.append("</a><br/>")
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)
return self.day_cell("noday", "&nbsp;")
2015-04-13 18:56:43 +02:00
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(EventCalendar, self).formatmonth(year, month)
def group_by_day(self, pEvents):
2021-04-29 00:27:33 +02:00
def field(ev):
return ev.date.day
return dict([(dat, list(items)) for dat, items in groupby(pEvents, field)])
2015-04-13 18:56:43 +02:00
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)