From 052b3a05d2d9db9997d2f89224f8c41d7690f31d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Wed, 26 Feb 2025 23:21:54 +0100
Subject: [PATCH] Massively reduce query count in scheduler

---
 AKScheduling/templates/admin/AKScheduling/scheduling.html | 4 +++-
 AKScheduling/views.py                                     | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/AKScheduling/templates/admin/AKScheduling/scheduling.html b/AKScheduling/templates/admin/AKScheduling/scheduling.html
index 437043cd..2c4a27e5 100644
--- a/AKScheduling/templates/admin/AKScheduling/scheduling.html
+++ b/AKScheduling/templates/admin/AKScheduling/scheduling.html
@@ -356,8 +356,10 @@
                     {% endif %}
                     {% for slot in track_slots.list %}
                         <div class="unscheduled-slot badge" style='background-color: {{ slot.ak.category.color }}'
-                             data-event='{ "title": "{{ slot.ak.short_name }}", "duration": {"hours": "{{ slot.duration|unlocalize }}"}, "constraint": "roomAvailable", "description": "{{ slot.ak.details | escapejs }}", "slotID": "{{ slot.pk }}", "backgroundColor": "{{ slot.ak.category.color }}", "url": "{% url "admin:AKModel_akslot_change" slot.pk %}"}' data-details="{{ slot.ak.details }}">{{ slot.ak.short_name }}
+                             {% with slot.ak.details as details %}
+                             data-event='{ "title": "{{ slot.ak.short_name }}", "duration": {"hours": "{{ slot.duration|unlocalize }}"}, "constraint": "roomAvailable", "description": "{{ details | escapejs }}", "slotID": "{{ slot.pk }}", "backgroundColor": "{{ slot.ak.category.color }}", "url": "{% url "admin:AKModel_akslot_change" slot.pk %}"}' data-details="{{ details }}">{{ slot.ak.short_name }}
                             ({{ slot.duration }} h)<br>{{ slot.ak.owners_list }}
+                            {% endwith %}
                         </div>
                     {% endfor %}
                 {% endfor %}
diff --git a/AKScheduling/views.py b/AKScheduling/views.py
index e0fa0bcf..321f9264 100644
--- a/AKScheduling/views.py
+++ b/AKScheduling/views.py
@@ -41,7 +41,7 @@ class SchedulingAdminView(AdminViewMixin, FilterByEventSlugMixin, ListView):
     context_object_name = "slots_unscheduled"
 
     def get_queryset(self):
-        return super().get_queryset().filter(start__isnull=True).select_related('event', 'ak').order_by('ak__track', 'ak')
+        return super().get_queryset().filter(start__isnull=True).select_related('event', 'ak', 'ak__track', 'ak__category').prefetch_related('ak__types', 'ak__owners', 'ak__conflicts', 'ak__prerequisites', 'ak__requirements').order_by('ak__track', 'ak')
 
     def get_context_data(self, *, object_list=None, **kwargs):
         context = super().get_context_data(object_list=object_list, **kwargs)
-- 
GitLab