From af46a31cb5c33bdc03f9d5d628b17ce11fe39fea Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Thu, 8 Apr 2021 21:19:49 +0200
Subject: [PATCH] Limit selected fields in build_timetable

---
 aleksis/apps/chronos/util/build.py | 72 +++++++++++++++++++++++++++++-
 1 file changed, 71 insertions(+), 1 deletion(-)

diff --git a/aleksis/apps/chronos/util/build.py b/aleksis/apps/chronos/util/build.py
index 185c8f54..483df4d3 100644
--- a/aleksis/apps/chronos/util/build.py
+++ b/aleksis/apps/chronos/util/build.py
@@ -48,6 +48,25 @@ def build_timetable(
 
     # Get matching lesson periods
     lesson_periods = LessonPeriod.objects
+    lesson_periods = (
+        lesson_periods.select_related(None)
+        .select_related("lesson", "lesson__subject", "period", "room")
+        .only(
+            "lesson",
+            "period",
+            "room",
+            "lesson__subject",
+            "period__weekday",
+            "period__period",
+            "lesson__subject__short_name",
+            "lesson__subject__name",
+            "lesson__subject__colour_fg",
+            "lesson__subject__colour_bg",
+            "room__short_name",
+            "room__name",
+        )
+    )
+
     if is_week:
         lesson_periods = lesson_periods.in_week(date_ref)
     else:
@@ -72,6 +91,23 @@ def build_timetable(
     else:
         extra_lessons = extra_lessons.filter_from_type(type_, obj)
 
+    extra_lessons = extra_lessons.only(
+        "week",
+        "year",
+        "period",
+        "subject",
+        "room",
+        "comment",
+        "period__weekday",
+        "period__period",
+        "subject__short_name",
+        "subject__name",
+        "subject__colour_fg",
+        "subject__colour_bg",
+        "room__short_name",
+        "room__name",
+    )
+
     # Sort lesson periods in a dict
     extra_lessons_per_period = extra_lessons.group_by_periods(is_week=is_week)
 
@@ -87,6 +123,18 @@ def build_timetable(
     else:
         events = events.filter_from_type(type_, obj)
 
+    events = events.only(
+        "id",
+        "title",
+        "date_start",
+        "date_end",
+        "period_from",
+        "period_to",
+        "period_from__weekday",
+        "period_from__period",
+        "period_to__weekday",
+        "period_to__period",
+    )
     # Sort events in a dict
     events_per_period = {}
     for event in events:
@@ -144,7 +192,29 @@ def build_timetable(
         else:
             week = date_ref
         supervisions = (
-            Supervision.objects.in_week(week).all().annotate_week(week).filter_by_teacher(obj)
+            Supervision.objects.in_week(week)
+            .all()
+            .annotate_week(week)
+            .filter_by_teacher(obj)
+            .only(
+                "area",
+                "break_item",
+                "teacher",
+                "area",
+                "area__short_name",
+                "area__name",
+                "area__colour_fg",
+                "area__colour_bg",
+                "break_item__short_name",
+                "break_item__name",
+                "break_item__after_period__period",
+                "break_item__after_period__weekday",
+                "break_item__before_period__period",
+                "break_item__before_period__weekday",
+                "teacher__short_name",
+                "teacher__first_name",
+                "teacher__last_name",
+            )
         )
 
         if not is_week:
-- 
GitLab