[gen] Calendar: added a method for retrieving events from a calendar.

This commit is contained in:
Gaetan Delannay 2012-11-08 22:39:22 +01:00
parent 0dd870c042
commit 6cf29778b6
2 changed files with 84 additions and 2 deletions

View file

@ -238,6 +238,86 @@ class Calendar(Type):
if not events: return
return events[0].eventType
def getEventsByType(self, obj, eventType, minDate=None, maxDate=None,
sorted=True, groupSpanned=False):
'''Returns all the events of a given p_eventType. If p_eventType is
None, it returns events of all types. The return value is a list of
2-tuples whose 1st elem is a DateTime instance and whose 2nd elem is
the event.
If p_sorted is True, the list is sorted in chronological order. Else,
the order is random, but the result is computed faster.
If p_minDate and/or p_maxDate is/are specified, it restricts the
search interval accordingly.
If p_groupSpanned is True, events spanned on several days are
grouped into a single event. In this case, tuples in the result
are 3-tuples: (DateTime_startDate, DateTime_endDate, event).
'''
# Prevent wrong combinations of parameters
if groupSpanned and not sorted:
raise Exception('Events must be sorted if you want to get ' \
'spanned events to be grouped.')
obj = obj.o # Ensure p_obj is not a wrapper.
res = []
if not hasattr(obj, self.name): return res
# Compute "min" and "max" tuples
if minDate:
minYear = minDate.year()
minMonth = (minYear, minDate.month())
minDay = (minYear, minDate.month(), minDate.day())
if maxDate:
maxYear = maxDate.year()
maxMonth = (maxYear, maxDate.month())
maxDay = (maxYear, maxDate.month(), maxDate.day())
# Browse years
years = getattr(obj, self.name)
for year in years.keys():
# Don't take this year into account if outside interval
if minDate and (year < minYear): continue
if maxDate and (year > maxYear): continue
months = years[year]
# Browse this year's months
for month in months.keys():
# Don't take this month into account if outside interval
thisMonth = (year, month)
if minDate and (thisMonth < minMonth): continue
if maxDate and (thisMonth > maxMonth): continue
days = months[month]
# Browse this month's days
for day in days.keys():
# Don't take this day into account if outside interval
thisDay = (year, month, day)
if minDate and (thisDay < minDay): continue
if maxDate and (thisDay > maxDay): continue
events = days[day]
# Browse this day's events
for event in events:
# Filter unwanted events
if eventType and (event.eventType != eventType):
continue
# We have found a event.
date = DateTime('%d/%d/%d 12:00' % (year,month,day))
if groupSpanned:
singleRes = [date, None, event]
else:
singleRes = (date, event)
res.append(singleRes)
# Sort the result if required
if sorted: res.sort(lambda x,y: cmp(x[0], y[0]))
# Group events spanned on several days if required
if groupSpanned:
# Browse events in reverse order and merge them when appropriate
i = len(res) - 1
while i > 0:
currentDate = res[i][0]
lastDate = res[i][1]
previousDate = res[i-1][0]
if previousDate == (currentDate-1):
# A merge is needed
del res[i]
res[i-1][1] = lastDate or currentDate
i -= 1
return res
def hasEventsAt(self, obj, date, otherEvents):
'''Returns True if, at p_date, an event is found of the same type as
p_otherEvents.'''

View file

@ -65,8 +65,9 @@ img { border: 0; vertical-align: middle}
width: 600px; border: 1px #F0C36D solid; padding: 6px;
background-color: #F9EDBE; text-align: center;
border-radius: 2px 2px 2px 2px; box-shadow: 0 2px 4px #A9A9A9;}
.focus { font-size: 90%; margin: 7px; padding: 7px; background-color: #d7dee4;
border-radius: 2px 2px 2px 2px; box-shadow: 0 2px 4px #A9A9A9;}
.focus { font-size: 90%; margin: 7px 0 7px 0; padding: 7px;
background-color: #d7dee4; border-radius: 2px 2px 2px 2px;
box-shadow: 0 2px 4px #A9A9A9 }
.focus td { padding: 4px 0px 4px 4px }
.discreet { font-size: 90%; }
.lostPassword a { font-size: 90%; color: white; padding-left: 1em;}
@ -137,3 +138,4 @@ img { border: 0; vertical-align: middle}
padding: 10px; margin: 10px 0 10px 0}
.homeTable { background-color: #E3E3E3; border-top: 1px solid grey}
.homeTable td { padding: 10px 5px 10px 10px}
.homeTable th { padding-top: 5px; font-size: 105% }