diff --git a/AKDashboard/views.py b/AKDashboard/views.py index afabaf7df0b61574baf12d67d2ae86ac35a8bc83..5f2f52826cb83575222ff52df67e7aba3eeb8a4b 100644 --- a/AKDashboard/views.py +++ b/AKDashboard/views.py @@ -18,7 +18,7 @@ class DashboardView(TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context['events'] = Event.objects.filter(public=True) + context['events'] = Event.objects.filter(public=True).prefetch_related('dashboardbutton_set') return context @@ -54,7 +54,7 @@ class DashboardEventView(DetailView): # Changes in plan if apps.is_installed("AKPlan"): if not context['event'].plan_hidden: - last_changed_slots = AKSlot.objects.filter(event=context['event'], start__isnull=False).order_by('-updated')[ + last_changed_slots = AKSlot.objects.select_related('ak').filter(event=context['event'], start__isnull=False).order_by('-updated')[ :int(settings.DASHBOARD_RECENT_MAX)] for changed_slot in last_changed_slots: recent_changes.append({'icon': ('clock', 'far'), diff --git a/AKModel/models.py b/AKModel/models.py index ac24085aeccfdfe59c4fca13e14dc51c4add97b9..710b0251bcd4c01e291b8f1d183fec3e780d45ac 100644 --- a/AKModel/models.py +++ b/AKModel/models.py @@ -84,14 +84,14 @@ class Event(models.Model): :return: list of category-AK-list-tuples, optionally the additional list of AK wishes :rtype: list[(AKCategory, list[AK])] [, list[AK]] """ - categories = self.akcategory_set.all() + categories = self.akcategory_set.select_related('event').all() categories_with_aks = [] ak_wishes = [] if wishes_seperately: for category in categories: ak_list = [] - for ak in category.ak_set.all(): + for ak in category.ak_set.select_related('event').prefetch_related('owners', 'akslot_set').all(): if filter(ak): if ak.wish: ak_wishes.append(ak) @@ -211,6 +211,9 @@ class AKTrack(models.Model): def __str__(self): return self.name + def aks_with_category(self): + return self.ak_set.select_related('category').all() + class AKRequirement(models.Model): """ An AKRequirement describes something needed to hold an AK, e.g. infrastructure. @@ -291,7 +294,7 @@ class AK(models.Model): @property def details(self): from AKModel.availability.models import Availability - availabilities = ', \n'.join(f'{a.simplified}' for a in Availability.objects.filter(ak=self)) + availabilities = ', \n'.join(f'{a.simplified}' for a in Availability.objects.select_related('event').filter(ak=self)) return f"""{self.name}{" (R)" if self.reso else ""}: {self.owners_list} @@ -307,7 +310,7 @@ class AK(models.Model): @property def durations_list(self): - return ", ".join(str(slot.duration_simplified) for slot in self.akslot_set.all()) + return ", ".join(str(slot.duration_simplified) for slot in self.akslot_set.select_related('event').all()) @property def wish(self): @@ -590,7 +593,7 @@ class ConstraintViolation(models.Model): @property def _ak_slots_str(self): if self.pk and self.pk > 0: - return ', '.join(str(a) for a in self.ak_slots.all()) + return ', '.join(str(a) for a in self.ak_slots.select_related('event').all()) return ', '.join(str(a) for a in self.ak_slots_tmp) def save(self, *args, **kwargs): diff --git a/AKScheduling/api.py b/AKScheduling/api.py index fb8db200795c14650191ce49f23749d1e3eb4a52..98838e0f31c9b1232bdfb75e040ef9abbe3e1264 100644 --- a/AKScheduling/api.py +++ b/AKScheduling/api.py @@ -36,7 +36,7 @@ class EventsView(LoginRequiredMixin, EventSlugMixin, ListView): model = AKSlot def get_queryset(self): - return super().get_queryset().filter(event=self.event, room__isnull=False) + return super().get_queryset().select_related('ak').filter(event=self.event, room__isnull=False) def render_to_response(self, context, **response_kwargs): return JsonResponse( @@ -149,4 +149,4 @@ class ConstraintViolationsViewSet(EventSlugMixin, viewsets.ModelViewSet): return get_object_or_404(ConstraintViolation, pk=self.kwargs["pk"]) def get_queryset(self): - return ConstraintViolation.objects.filter(event=self.event).order_by('manually_resolved', '-type', '-timestamp') + return ConstraintViolation.objects.select_related('event', 'room').prefetch_related('aks', 'ak_slots', 'ak_owner', 'requirement', 'category').filter(event=self.event).order_by('manually_resolved', '-type', '-timestamp') diff --git a/AKScheduling/templates/admin/AKScheduling/manage_tracks.html b/AKScheduling/templates/admin/AKScheduling/manage_tracks.html index 3c0cd30fbcb3964d9aea4e5baaf6db3408aa976e..1a3c5fef3fa33329a46678fd5419e483eeae6571 100644 --- a/AKScheduling/templates/admin/AKScheduling/manage_tracks.html +++ b/AKScheduling/templates/admin/AKScheduling/manage_tracks.html @@ -228,7 +228,7 @@ </div> <div class="card-body"> <ul data-track-id="{{ track.pk }}" data-name="{{ track }}" data-sync="true" class="ak-list"> - {% for ak in track.ak_set.all %} + {% for ak in track.aks_with_category %} <li data-ak-id="{{ ak.pk }}" data-toggle="tooltip" data-placement="top" title=""> {{ ak.name }} ({{ ak.category }}) </li> diff --git a/AKScheduling/views.py b/AKScheduling/views.py index 50c485a7601a81e82327b5d618698054075e3618..9d0302cfb1fa135a4dd3bf32f91c999cc2ceec13 100644 --- a/AKScheduling/views.py +++ b/AKScheduling/views.py @@ -31,7 +31,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().order_by('ak__track') + return super().get_queryset().filter(start__isnull=True).select_related('event', 'ak').order_by('ak__track') def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) @@ -53,7 +53,7 @@ class TrackAdminView(AdminViewMixin, FilterByEventSlugMixin, ListView): def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) - context["aks_without_track"] = self.event.ak_set.filter(track=None) + context["aks_without_track"] = self.event.ak_set.select_related('category').filter(track=None) return context @@ -129,10 +129,10 @@ class InterestEnteringAdminView(SuccessMessageMixin, AdminViewMixin, EventSlugMi # Find other AK wishes (regardless of the category)... if context['ak'].wish: - other_aks = [ak for ak in context['event'].ak_set.all() if ak.wish] + other_aks = [ak for ak in context['event'].ak_set.prefetch_related('owners').all() if ak.wish] # or other AKs of this category else: - other_aks = [ak for ak in context['ak'].category.ak_set.all() if not ak.wish] + other_aks = [ak for ak in context['ak'].category.ak_set.prefetch_related('owners').all() if not ak.wish] for other_ak in other_aks: if next_is_next: @@ -143,9 +143,9 @@ class InterestEnteringAdminView(SuccessMessageMixin, AdminViewMixin, EventSlugMi next_is_next = True last_ak = other_ak - for category in context['event'].akcategory_set.all(): + for category in context['event'].akcategory_set.prefetch_related('ak_set').all(): aks_for_category = [] - for ak in category.ak_set.all(): + for ak in category.ak_set.prefetch_related('owners').all(): if ak.wish: ak_wishes.append(ak) else: diff --git a/AKSubmission/views.py b/AKSubmission/views.py index 796d05d39ce47a3a649d551f3eed344e817de791..5f3f42b902fca36f0e89565c76badf00723270b6 100644 --- a/AKSubmission/views.py +++ b/AKSubmission/views.py @@ -32,7 +32,7 @@ class AKOverviewView(FilterByEventSlugMixin, ListView): wishes_as_category = False def filter_aks(self, context, category): - return category.ak_set.all() + return category.ak_set.select_related('event').prefetch_related('owners').all() def get_active_category_name(self, context): return context["categories_with_aks"][0][0].name @@ -126,6 +126,9 @@ class AKDetailView(EventSlugMixin, DetailView): context_object_name = "ak" template_name = "AKSubmission/ak_detail.html" + def get_queryset(self): + return super().get_queryset().select_related('event').prefetch_related('owners') + def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(object_list=object_list, **kwargs) context["availabilities"] = Availability.objects.filter(ak=context["ak"]) @@ -136,7 +139,7 @@ class AKDetailView(EventSlugMixin, DetailView): context["featured_slot_type"] = "NONE" if apps.is_installed("AKPlan"): in_two_hours = current_timestamp + timedelta(hours=2) - slots = context["ak"].akslot_set.filter(start__isnull=False, room__isnull=False) + slots = context["ak"].akslot_set.filter(start__isnull=False, room__isnull=False).select_related('room') for slot in slots: if slot.end > current_timestamp: if slot.start <= current_timestamp: