Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions occurrences/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,35 +291,48 @@ def has_enrolments_system(self) -> bool:

def has_space_for_enrolments(self) -> Optional[bool]:
"""
Determines whether any (upcoming) occurrence has any space left
Determines whether any upcoming or ongoing occurrence has any space left
for enrolments.

Returns:
- True: If at least one upcoming occurrence has space for enrolments.
- True: If at least one upcoming or ongoing occurrence has space for
enrolments.
- False: If no upcoming occurrences have space for enrolments.
- None: If the event doesn't utilize the enrolments system or
relies on an external enrolments system.
"""
if not self.has_enrolments_system() or self.has_external_enrolments_system():
return None

return any(
has_upcoming_space = any(
occurrence.has_space_left()
for occurrence in self.occurrences.filter_upcoming()
)

if self.enrolment_end_days is None:
# No limit to when enrolments can be made before the occurrence start.
return has_upcoming_space or any(
occurrence.has_space_left()
for occurrence in self.occurrences.filter_ongoing()
)

return has_upcoming_space


class OccurrenceQueryset(models.QuerySet):
def delete(self, *args, **kwargs):
for obj in self:
obj.delete()

def filter_upcoming(self, *args, **kwargs):
"""Filter occurrences that are not not cancelled and
are held in the future.
"""
"""Filter occurrences that are not cancelled and are held in the future."""
return self.filter(cancelled=False, start_time__gte=timezone.now())

def filter_ongoing(self, *args, **kwargs):
"""Filter occurrences that are not cancelled and are ongoing at the moment."""
now = timezone.now()
return self.filter(cancelled=False, start_time__lte=now, end_time__gte=now)


class Occurrence(GDPRModel, SerializableMixin, TimestampedModel):
OCCURRENCE_SEAT_TYPE_CHILDREN_COUNT = "children_count"
Expand Down
6 changes: 4 additions & 2 deletions occurrences/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def resolve_last_occurrence_datetime(self, info, **kwargs):

def resolve_has_space_for_enrolments(self, info, **kwargs):
"""
Determines whether any upcoming occurrence has any space left for enrolments.
Determines whether any upcoming or ongoing occurrence has any space left for
enrolments.

This method leverages the internal `has_space_for_enrolments` logic to ascertain
if there are any future event occurrences with available spaces for enrollment.
Expand All @@ -231,7 +232,8 @@ def resolve_has_space_for_enrolments(self, info, **kwargs):
(may not be used in this specific implementation).

Returns:
- True: If at least one upcoming occurrence has space for enrolments.
- True: If at least one upcoming or ongoing occurrence has space for
enrolments.
- False: If no upcoming occurrences have space for enrolments.
- None: If the event doesn't utilize the enrolments system or
relies on an external enrolments system.
Expand Down
74 changes: 72 additions & 2 deletions occurrences/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,20 +980,90 @@ def test_palvelutarjotin_event_has_space_for_enrolments_returning_boolean(
mock_get_event_data,
):
p_event = PalvelutarjotinEventFactory()
# Past occurrences
OccurrenceFactory.create_batch(
4,
p_event=p_event,
cancelled=False,
start_time=timezone.now() - timedelta(days=2),
end_time=timezone.now() - timedelta(days=1),
)
# Ongoing occurrences
OccurrenceFactory.create_batch(
2,
p_event=p_event,
cancelled=False,
start_time=timezone.now() - timedelta(days=1),
end_time=timezone.now() + timedelta(days=1),
)
# Future occurrences
OccurrenceFactory.create_batch(
3,
p_event=p_event,
cancelled=False,
start_time=timezone.now() + timedelta(days=1),
end_time=timezone.now() + timedelta(days=2),
)
assert p_event.occurrences.count() == 9
assert p_event.occurrences.filter_upcoming().count() == 3
assert p_event.occurrences.filter_ongoing().count() == 2
with patch(
"occurrences.models.PalvelutarjotinEvent.has_external_enrolments_system",
return_value=False,
):
with patch(
"occurrences.models.PalvelutarjotinEvent.has_internal_enrolments_system",
return_value=True,
):
with patch(
"occurrences.models.Occurrence.has_space_left"
) as mock_has_space_left:
mock_has_space_left.return_value = False
assert p_event.has_space_for_enrolments() is False
assert mock_has_space_left.call_count == 3
with patch(
"occurrences.models.PalvelutarjotinEvent.has_external_enrolments_system",
return_value=False,
):
with patch(
"occurrences.models.PalvelutarjotinEvent.has_internal_enrolments_system",
return_value=True,
):
with patch(
"occurrences.models.Occurrence.has_space_left"
) as mock_has_space_left:
# 2nd occurrence returns True, so result is True
# and any-call returns after 2nd round.
mock_has_space_left.side_effect = [False, True, False]
assert p_event.has_space_for_enrolments() is True
assert mock_has_space_left.call_count == 2


@pytest.mark.django_db
def test_palvelutarjotin_event_has_space_for_enrolments_ongoing_events(
mock_get_event_data,
):
"""Ongoing events are not considered"""
p_event = PalvelutarjotinEventFactory(enrolment_end_days=None)
# Past occurrences
OccurrenceFactory.create_batch(
2,
p_event=p_event,
cancelled=False,
start_time=timezone.now() - timedelta(days=2),
end_time=timezone.now() - timedelta(days=1),
)
# Ongoing occurrences
OccurrenceFactory.create_batch(
3,
p_event=p_event,
cancelled=False,
start_time=timezone.now() - timedelta(days=1),
end_time=timezone.now() + timedelta(days=1),
)
assert p_event.occurrences.count() == 6
assert p_event.occurrences.filter_upcoming().count() == 3
assert p_event.occurrences.count() == 5
assert p_event.occurrences.filter_upcoming().count() == 0
assert p_event.occurrences.filter_ongoing().count() == 3
with patch(
"occurrences.models.PalvelutarjotinEvent.has_external_enrolments_system",
return_value=False,
Expand Down