Skip to content

Commit

Permalink
fix recurrence and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitloup committed Sep 19, 2023
1 parent e9018ff commit 998f5d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
19 changes: 6 additions & 13 deletions pod/meeting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
"""
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
24 changes: 10 additions & 14 deletions pod/meeting/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 998f5d0

Please sign in to comment.