From 9c64a33fd206c764f7d8c160d881e8915e5ef4df Mon Sep 17 00:00:00 2001 From: Tobias Date: Sat, 8 Dec 2018 19:25:14 +1300 Subject: [PATCH] Refactor timeteabel to speed up dashboard --- .../schedule/schedule_conference.html | 4 +- vendor/symposion/schedule/timetable.py | 42 +++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/pinaxcon/templates/symposion/schedule/schedule_conference.html b/pinaxcon/templates/symposion/schedule/schedule_conference.html index 7fe56daf..9ecfd53c 100644 --- a/pinaxcon/templates/symposion/schedule/schedule_conference.html +++ b/pinaxcon/templates/symposion/schedule/schedule_conference.html @@ -46,12 +46,12 @@ aria-labelledby="schedule_day_{{ timetable.day.date|date:"l"|lower}}-tab" >
- {% cache 180 cache-schedule timetable.day.date %} + {% cache 0 cach1e-schedule timetable.day.date %} {% include "symposion/schedule/_grid.html" %} {% endcache %}
- {% cache 180 cache-schedule-mobile timetable.day.date %} + {% cache 0 cache-schedule-mobile timetable.day.date %} {% include "symposion/schedule/_mobile.html" %} {% endcache %}
diff --git a/vendor/symposion/schedule/timetable.py b/vendor/symposion/schedule/timetable.py index 9fcb627d..893851aa 100644 --- a/vendor/symposion/schedule/timetable.py +++ b/vendor/symposion/schedule/timetable.py @@ -9,32 +9,40 @@ class TimeTable(object): def __init__(self, day): self.day = day + self.slots_qs = Slot.objects.filter(day=day)\ + .select_related('kind', 'content_ptr__speaker__user')\ + .prefetch_related('content_ptr__additional_speakers') + self._times = sorted( + set(itertools.chain(*self.slots_qs.values_list("start", "end"))) + ) - def slots_qs(self): - qs = Slot.objects.all() - qs = qs.filter(day=self.day) - return qs + def slots(self): + if not hasattr(self, '_slots'): + filters = { + "room_count": Count("slotroom"), + "order": Min("slotroom__room__order") + } + self._slots = self.slots_qs.annotate(**filters).order_by("order") + return self._slots def rooms(self): - qs = Room.objects.all() - qs = qs.filter(schedule=self.day.schedule) - qs = qs.filter( - pk__in=SlotRoom.objects.filter(slot__in=self.slots_qs().values("pk")).values("room")) - qs = qs.order_by("order") - return qs + if not hasattr(self, '_rooms'): + self._rooms = Room.objects\ + .filter(slotroom__slot__in=self.slots_qs)\ + .distinct()\ + .order_by("order") + return self._rooms def __iter__(self): - times = sorted(set(itertools.chain(*self.slots_qs().values_list("start", "end")))) - slots = Slot.objects.filter(pk__in=self.slots_qs().values("pk")) - slots = slots.annotate(room_count=Count("slotroom"), order=Min("slotroom__room__order")) - slots = slots.order_by("order") + slots = self.slots() + row = [] total_room_count = self.rooms().count() - for time, next_time in pairwise(times): - row = {"time": time, "slots": []} + for time, next_time in pairwise(self._times): + row = {"time": time, "end": next_time, "slots": []} row_slots = [ slot for slot in slots if slot.start == time] for slot in row_slots: - slot.rowspan = TimeTable.rowspan(times, slot.start, slot.end) + slot.rowspan = TimeTable.rowspan(self._times, slot.start, slot.end) slot.colspan = slot.room_count if not slot.exclusive else total_room_count row["slots"].append(slot) if row["slots"] or next_time is None: