From 4468aa1b64023899098f2ab3539f732138bd45ad Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Fri, 25 Jun 2021 21:16:48 +0200
Subject: [PATCH] [Register objects table] Use the last known school term if
 there is no current school term

---
 aleksis/apps/alsijil/forms.py                | 8 +++++++-
 aleksis/apps/alsijil/util/alsijil_helpers.py | 5 +++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/aleksis/apps/alsijil/forms.py b/aleksis/apps/alsijil/forms.py
index 53b5ffee1..cb12a4eb8 100644
--- a/aleksis/apps/alsijil/forms.py
+++ b/aleksis/apps/alsijil/forms.py
@@ -296,8 +296,14 @@ class FilterRegisterObjectForm(forms.Form):
     def get_initial(cls, has_documentation: Optional[bool] = None):
         date_end = timezone.now().date()
         date_start = date_end - timedelta(days=30)
+        school_term = SchoolTerm.current
+
+        # If there is no current school year, use last known school year.
+        if not school_term:
+            school_term = SchoolTerm.objects.all().last()
+
         return {
-            "school_term": SchoolTerm.current,
+            "school_term": school_term,
             "date_start": date_start,
             "date_end": date_end,
             "has_documentation": has_documentation,
diff --git a/aleksis/apps/alsijil/util/alsijil_helpers.py b/aleksis/apps/alsijil/util/alsijil_helpers.py
index b7442f998..29381c6cb 100644
--- a/aleksis/apps/alsijil/util/alsijil_helpers.py
+++ b/aleksis/apps/alsijil/util/alsijil_helpers.py
@@ -336,6 +336,11 @@ def generate_list_of_all_register_objects(filter_dict: Dict[str, Any]) -> List[D
     # Always force a value for school term, start and end date so that queries won't get too big
     initial_filter_data = FilterRegisterObjectForm.get_initial()
     filter_dict["school_term"] = filter_dict.get("school_term", initial_filter_data["school_term"])
+
+    # If there is not school year at all, there are definitely no data.
+    if not filter_dict["school_term"]:
+        return []
+
     filter_dict["date_start"] = filter_dict.get("date_start", initial_filter_data["date_start"])
     filter_dict["date_end"] = filter_dict.get("date_end", initial_filter_data["date_end"])
     filter_dict["filter_date"] = bool(filter_dict.get("date_start")) and bool(
-- 
GitLab