From 6078e6171f5ff1230e64fc9a818b62a6757526ef Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 23 Jul 2018 08:00:06 +0200 Subject: [PATCH] [Bugfix] Rework month padding and event fetching routines Month padding (ie. adding the days of previous/next months) is now working as expected. Fetching multi-day events now also displays events correctly. --- calsocial/calendar_system/gregorian.py | 28 +++++++++++++++----------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/calsocial/calendar_system/gregorian.py b/calsocial/calendar_system/gregorian.py index 3d1fab9..7afb0b6 100644 --- a/calsocial/calendar_system/gregorian.py +++ b/calsocial/calendar_system/gregorian.py @@ -83,17 +83,23 @@ class GregorianCalendar(CalendarSystem): def days(self): day_list = [] - start_day = self.timestamp.replace(day=1) + month_first = self.timestamp.replace(day=1) - while start_day.weekday() > self.START_DAY: - start_day -= timedelta(days=1) + if self.timestamp.month == 12: + month_last = month_first.replace(day=31) + else: + month_last = month_first.replace(month=month_first.month + 1) - timedelta(days=1) - day_list.append(start_day) - current_day = start_day + pad_before = (7 - self.START_DAY + month_first.weekday()) % 7 + pad_after = (6 - month_last.weekday() + self.START_DAY) % 7 - while current_day.weekday() < self.END_DAY and current_day.month <= self.timestamp.month: - current_day += timedelta(days=1) - day_list.append(current_day) + first_display = month_first - timedelta(days=pad_before) + last_display = month_last + timedelta(days=pad_after) + current = first_display + + while current <= last_display: + day_list.append(current) + current += timedelta(days=1) return day_list @@ -212,10 +218,8 @@ class GregorianCalendar(CalendarSystem): start_timestamp = date.replace(hour=0, minute=0, second=0, microsecond=0) end_timestamp = start_timestamp + timedelta(days=1) - events = events.filter(((Event.start_time >= start_timestamp) & - (Event.start_time < end_timestamp)) | - ((Event.end_time >= start_timestamp) & - (Event.end_time < end_timestamp))) \ + events = events.filter((Event.start_time <= end_timestamp) & + (Event.end_time >= start_timestamp)) \ .order_by('start_time', 'end_time') if user is None: