From 998f5d0714491f6470824c66ecfac2675e30d2b2 Mon Sep 17 00:00:00 2001 From: Ptitloup Date: Tue, 19 Sep 2023 11:52:35 +0200 Subject: [PATCH] fix recurrence and test --- pod/meeting/models.py | 19 ++++++------------- pod/meeting/tests/test_models.py | 24 ++++++++++-------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/pod/meeting/models.py b/pod/meeting/models.py index 6ea3c2f824..ca2d3239cd 100644 --- a/pod/meeting/models.py +++ b/pod/meeting/models.py @@ -441,9 +441,7 @@ def check_recurrence(self): # noqa: C901 self.reset_recurrence() next_occurrence = self.start for _i in range(self.nb_occurrences - 1): - next_occurrence = self.next_occurrence( - next_occurrence + timedelta(days=1) - ) + next_occurrence = self.next_occurrence(next_occurrence) self.recurring_until = next_occurrence # Infinite recurrence... do nothing else: @@ -478,16 +476,11 @@ def get_hashkey(self): # ############################## Meeting occurences def next_occurrence_from_today(self): """Returns the date of the next occurrence for the meeting from today.""" - next_one = self.next_occurrence(timezone.now().date()) - if next_one == timezone.now().date(): + if self.start_at == timezone.now().date(): start_datetime = self.start_at + self.expected_duration if start_datetime > timezone.now(): - return next_one - else: - return self.next_occurrence( - timezone.now().date() + timedelta(days=1) - ) - return next_one + return self.start_at + return self.next_occurrence(timezone.now().date()) def next_occurrence(self, current_date): # noqa: C901 """ @@ -500,7 +493,7 @@ def next_occurrence(self, current_date): # noqa: C901 return current_date + timedelta(days=self.frequency) if self.recurrence == Meeting.WEEKLY: - increment = 0 + increment = 1 # Look in the current week weekday = current_date.weekday() while weekday + increment <= 6: @@ -559,7 +552,7 @@ def get_occurrences(self, start, end=None): while new_start <= real_end: if new_start >= start: occurrences.append(new_start) - new_start = self.next_occurrence(new_start + timedelta(days=1)) + new_start = self.next_occurrence(new_start) return occurrences # check if event is in the period diff --git a/pod/meeting/tests/test_models.py b/pod/meeting/tests/test_models.py index 9497896a3f..4a3e13f2f2 100644 --- a/pod/meeting/tests/test_models.py +++ b/pod/meeting/tests/test_models.py @@ -9,6 +9,7 @@ from django.test import TestCase from django.template.defaultfilters import slugify from pod.authentication.models import AccessGroup +from django.utils import timezone class MeetingTestCase(TestCase): @@ -164,7 +165,7 @@ def setUp(self): def test_models_meetings_get_occurrences_no_recurrence(self): """A meeting without recurrence should return 1 occurence.""" meeting = Meeting.objects.get(id=1) - meeting.start_at = datetime(2022, 7, 7, 14, 0, 0) + meeting.start_at = timezone.make_aware(datetime(2022, 7, 7, 14, 0, 0)) meeting.recurrence = None meeting.save() self.assertEqual( @@ -180,7 +181,7 @@ def test_models_meetings_get_occurrences_daily_nb_occurrences_filled(self): """Daily occurences with date of end of reccurrence filled in but not \ number of occurrences.""" meeting = Meeting.objects.get(id=1) - meeting.start_at = datetime(2022, 7, 7, 14, 0, 0) + meeting.start_at = timezone.make_aware(datetime(2022, 7, 7, 14, 0, 0)) meeting.recurrence = "daily" meeting.frequency = 1 meeting.recurring_until = date(2022, 8, 2) @@ -220,7 +221,7 @@ def test_models_meetings_get_occurrences_daily_reset_recurring_until(self): """Daily occurences with date of end of reccurrence not leaving any \ possibliity of repeated occurence.""" meeting = Meeting.objects.get(id=1) - meeting.start_at = datetime(2022, 7, 7, 14, 0, 0) + meeting.start_at = timezone.make_aware(datetime(2022, 7, 7, 14, 0, 0)) meeting.recurrence = "daily" meeting.frequency = 3 meeting.recurring_until = date(2022, 7, 9) @@ -242,7 +243,7 @@ def test_models_meetings_get_occurrences_daily_recurring_until_filled(self): end of occurence. The date of end of reccurrence shoud be calculated if \ the number of occurrences is repeated.""" meeting = Meeting.objects.get(id=1) - meeting.start_at = datetime(2022, 7, 7, 14, 0, 0) + meeting.start_at = timezone.make_aware(datetime(2022, 7, 7, 14, 0, 0)) meeting.recurrence = "daily" meeting.frequency = 1 meeting.recurring_until = None @@ -315,25 +316,20 @@ def test_models_meetings_get_occurrences_weekly_null_weekdays_is_reset(self): def test_models_meetings_get_occurrences_weekly_weekdays_include_start(self): """For weekly recurrence, the weekday of the start date should be included \ in weekdays.""" - meeting = meeting = Meeting.objects.get(id=1) + meeting = Meeting.objects.get(id=1) with self.assertRaises(ValidationError) as context: - # 2022-7-7 is a Thursday, and weekday 2 is Tuesday - meeting.start_at = datetime(2022, 7, 7, 14, 0, 0) + # 2022-7-7 is a Thursday, and weekday 2 is not Tuesday + meeting.start_at = timezone.make_aware(datetime(2022, 7, 7, 14, 0, 0)) meeting.recurrence = "weekly" meeting.weekdays = "2" meeting.save() - msg = "The day of the start date of the meeting must be " - msg += "included in the recurrence weekdays." - self.assertEqual( - context.exception.messages, - [msg], - ) + self.assertTrue("weekdays" in context.exception.message_dict) def test_models_meetings_get_occurrences_weekly_recurring_until_filled(self): """Weekly occurences with date of end of reccurrence filled in but not \ number of occurrences.""" meeting = Meeting.objects.get(id=1) - meeting.start_at = datetime(2022, 7, 7, 14, 0, 0) + meeting.start_at = timezone.make_aware(datetime(2022, 7, 7, 14, 0, 0)) meeting.recurrence = "weekly" meeting.frequency = 1 meeting.recurring_until = date(2022, 8, 2)