From c52fc9c1cb2529c0d70573d09197821ca7c47cf7 Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Thu, 2 May 2024 19:01:57 +0200
Subject: [PATCH] Split get_for_coursebook

into get_documentations_for_events
& get_for_coursebook
This makes it reusable for further purposes (absences).
---
 aleksis/apps/alsijil/models.py | 89 +++++++++++++++++++---------------
 1 file changed, 51 insertions(+), 38 deletions(-)

diff --git a/aleksis/apps/alsijil/models.py b/aleksis/apps/alsijil/models.py
index 332c428fa..d72f27a56 100644
--- a/aleksis/apps/alsijil/models.py
+++ b/aleksis/apps/alsijil/models.py
@@ -515,44 +515,17 @@ class Documentation(CalendarEvent):
         # which is not possible via constraint, because amends is not local to Documentation
 
     @classmethod
-    def get_for_coursebook(
-        cls,
-        own: bool,
-        date_start: datetime,
-        date_end: datetime,
-        request: HttpRequest,
-        obj_type: Optional[str] = None,
-        obj_id: Optional[str] = None,
-        incomplete: Optional[bool] = False,
-    ) -> list:
-        """Get all the documentations for an object and a time frame.
-
-        obj_type may be one of TEACHER, GROUP, ROOM, COURSE
+    def get_documentations_for_events(
+            cls,
+            events: list,
+            incomplete: Optional[bool] = False,
+    ) -> tuple:
+        """Get all the documentations for the events.
+        Create dummy documentations if none exist.
+        Returns a tuple with a list of existing documentations and a list dummy documentations.
         """
-
-        # 1. Find all LessonEvents for all Lessons of this Course in this date range
-        event_params = {
-            "own": own,
-        }
-        if obj_type is not None and obj_id is not None:
-            event_params.update(
-                {
-                    "type": obj_type,
-                    "id": obj_id,
-                }
-            )
-
-        events = LessonEvent.get_single_events(
-            date_start,
-            date_end,
-            request,
-            event_params,
-            with_reference_object=True,
-        )
-
-        # 2. For each lessonEvent → check if there is a documentation
-        # if so, add the documentation to a list, if not, create a new one
         docs = []
+        dummies = []
         for event in events:
             if incomplete and event["STATUS"] == "CANCELLED":
                 continue
@@ -582,7 +555,7 @@ class Documentation(CalendarEvent):
                 else:
                     course, subject = event_reference_obj.course, event_reference_obj.subject
 
-                docs.append(
+                dummies.append(
                     cls(
                         pk=f"DUMMY;{event_reference_obj.id};{event['DTSTART'].dt.isoformat()};{event['DTEND'].dt.isoformat()}",
                         amends=event_reference_obj,
@@ -593,7 +566,47 @@ class Documentation(CalendarEvent):
                     )
                 )
 
-        return docs
+        return (docs, dummies)
+
+    @classmethod
+    def get_for_coursebook(
+        cls,
+        own: bool,
+        date_start: datetime,
+        date_end: datetime,
+        request: HttpRequest,
+        obj_type: Optional[str] = None,
+        obj_id: Optional[str] = None,
+        incomplete: Optional[bool] = False,
+    ) -> list:
+        """Get all the documentations for an object and a time frame.
+
+        obj_type may be one of TEACHER, GROUP, ROOM, COURSE
+        """
+
+        # Find all LessonEvents for all Lessons of this Course in this date range
+        event_params = {
+            "own": own,
+        }
+        if obj_type is not None and obj_id is not None:
+            event_params.update(
+                {
+                    "type": obj_type,
+                    "id": obj_id,
+                }
+            )
+
+        events = LessonEvent.get_single_events(
+            date_start,
+            date_end,
+            request,
+            event_params,
+            with_reference_object=True,
+        )
+
+        # Lookup or create documentations and return them all.
+        docs, dummies = cls.get_documentations_for_events(events, incomplete)
+        return docs + dummies
 
 
 class ParticipationStatus(CalendarEvent):
-- 
GitLab