From 1827826903b8ab2e4e7a11289261d9d93e1da466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?= <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de> Date: Fri, 13 May 2022 01:11:05 +0200 Subject: [PATCH] Speedup views of AKPlan Make sure event is only loaded once Massively reduce amount of SQL queries (e.g., plan view reduced from multiple queries per AK where over half of them are duplicates down to only 2 queries -- independent of AK count) Replace software-side list constructions with in-database operations --- AKModel/views.py | 3 ++- AKPlan/templates/AKPlan/plan_index.html | 6 +++--- AKPlan/templates/AKPlan/plan_room.html | 2 +- AKPlan/views.py | 15 +++++++++------ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/AKModel/views.py b/AKModel/views.py index 8374cb41..539c4f43 100644 --- a/AKModel/views.py +++ b/AKModel/views.py @@ -25,7 +25,8 @@ class EventSlugMixin: def _load_event(self): # Find event based on event slug - self.event = get_object_or_404(Event, slug=self.kwargs.get("event_slug", None)) + if self.event is None: + self.event = get_object_or_404(Event, slug=self.kwargs.get("event_slug", None)) def get(self, request, *args, **kwargs): self._load_event() diff --git a/AKPlan/templates/AKPlan/plan_index.html b/AKPlan/templates/AKPlan/plan_index.html index c7a5460b..4fdbc700 100644 --- a/AKPlan/templates/AKPlan/plan_index.html +++ b/AKPlan/templates/AKPlan/plan_index.html @@ -81,7 +81,7 @@ {% block content %} <div class="float-right"> <ul class="nav nav-pills"> - {% if event.room_set.count > 0 %} + {% if rooms|length > 0 %} <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" @@ -94,13 +94,13 @@ </div> </li> {% endif %} - {% if event.aktrack_set.count > 0 %} + {% if tracks|length > 0 %} <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">{% trans "Tracks" %}</a> <div class="dropdown-menu"> - {% for t in event.aktrack_set.all %} + {% for t in tracks %} <a class="dropdown-item" href="{% url "plan:plan_track" event_slug=event.slug pk=t.pk %}">{{ t }}</a> {% endfor %} diff --git a/AKPlan/templates/AKPlan/plan_room.html b/AKPlan/templates/AKPlan/plan_room.html index 7a7e62ed..8acd1fe7 100644 --- a/AKPlan/templates/AKPlan/plan_room.html +++ b/AKPlan/templates/AKPlan/plan_room.html @@ -16,7 +16,7 @@ {% block encode %} [ - {% for slot in room.akslot_set.all %} + {% for slot in slots %} {% if slot.start %} {'title': '{{ slot.ak }}', 'start': '{{ slot.start | timezone:event.timezone | date:"Y-m-d H:i:s" }}', diff --git a/AKPlan/views.py b/AKPlan/views.py index 2c9c6d2b..828e5e58 100644 --- a/AKPlan/views.py +++ b/AKPlan/views.py @@ -18,7 +18,7 @@ class PlanIndexView(FilterByEventSlugMixin, ListView): def get_queryset(self): # Ignore slots not scheduled yet - return super().get_queryset().filter(start__isnull=False) + return super().get_queryset().filter(start__isnull=False).select_related('ak', 'room', 'ak__category') def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) @@ -54,6 +54,8 @@ class PlanIndexView(FilterByEventSlugMixin, ListView): if settings.PLAN_SHOW_HIERARCHY: context["buildings"] = sorted(buildings) + context["tracks"] = self.event.aktrack_set.all() + return context @@ -94,6 +96,11 @@ class PlanRoomView(FilterByEventSlugMixin, DetailView): model = Room context_object_name = "room" + def get_context_data(self, *, object_list=None, **kwargs): + context = super().get_context_data(object_list=object_list, **kwargs) + context["slots"] = AKSlot.objects.filter(room=context['room']).select_related('ak', 'ak__category', 'ak__track') + return context + class PlanTrackView(FilterByEventSlugMixin, DetailView): template_name = "AKPlan/plan_track.html" @@ -102,9 +109,5 @@ class PlanTrackView(FilterByEventSlugMixin, DetailView): def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) - - context["slots"] = [] - for ak in context["track"].ak_set.all(): - context["slots"].extend(ak.akslot_set.all()) - + context["slots"] = AKSlot.objects.filter(event=self.event, ak__track=context['track']).select_related('ak', 'room', 'ak__category') return context -- GitLab