Skip to content
Draft
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
6 changes: 4 additions & 2 deletions graphene_linked_events/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,10 @@ def _test_events_p_event_relations(events_json):
events = json.loads(events_json, object_hook=lambda d: SimpleNamespace(**d))

# Get related palvelutarjotin events
p_events = PalvelutarjotinEvent.objects.prefetch_related("occurrences").filter(
linked_event_id__in=[e.id for e in events.data]
p_events = (
PalvelutarjotinEvent.objects.select_related("organisation")
.prefetch_related("occurrences")
.filter(linked_event_id__in=[e.id for e in events.data])
)

# Prune a list of events which have a link to a palvelutarjotin event
Expand Down
24 changes: 16 additions & 8 deletions occurrences/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,25 @@ def has_space_for_enrolments(self) -> Optional[bool]:
if not self.has_enrolments_system() or self.has_external_enrolments_system():
return None

has_upcoming_space = any(
occurrence.has_space_left()
for occurrence in self.occurrences.filter_upcoming()
)
now = timezone.now()
# Use prefetched occurrences (.all()) to avoid extra DB queries per event.
all_occurrences = list(self.occurrences.all())

upcoming = [
o
for o in all_occurrences
if not o.cancelled and o.start_time >= now
]
has_upcoming_space = any(o.has_space_left() for o in 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()
)
ongoing = [
o
for o in all_occurrences
if not o.cancelled and o.start_time <= now and o.end_time >= now
]
return has_upcoming_space or any(o.has_space_left() for o in ongoing)

return has_upcoming_space

Expand Down
25 changes: 13 additions & 12 deletions occurrences/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,23 @@ class Meta:
interfaces = (graphene.relay.Node,)

def resolve_next_occurrence_datetime(self, info, **kwargs):
try:
return (
self.occurrences.filter(start_time__gte=timezone.now(), cancelled=False)
.earliest("start_time")
.start_time
)
except Occurrence.DoesNotExist:
# Filter the prefetched occurrences in Python to avoid extra DB queries.
now = timezone.now()
upcoming = [
o
for o in self.occurrences.all()
if not o.cancelled and o.start_time >= now
]
if not upcoming:
return None
return min(upcoming, key=lambda o: o.start_time).start_time

def resolve_last_occurrence_datetime(self, info, **kwargs):
try:
return (
self.occurrences.filter(cancelled=False).latest("start_time").start_time
)
except Occurrence.DoesNotExist:
# Filter the prefetched occurrences in Python to avoid extra DB queries.
non_cancelled = [o for o in self.occurrences.all() if not o.cancelled]
if not non_cancelled:
return None
return max(non_cancelled, key=lambda o: o.start_time).start_time

def resolve_has_space_for_enrolments(self, info, **kwargs):
"""
Expand Down
Loading