From 61294c122ac661dfb53e1cfbbd6763fe772f2e61 Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Wed, 8 May 2024 13:02:55 +0200
Subject: [PATCH] Refactor get_documentations_for_person

& share between query & mutation.
---
 aleksis/apps/alsijil/models.py          | 27 +++++++++++++++++++++++++
 aleksis/apps/alsijil/schema/__init__.py | 13 ++----------
 aleksis/apps/alsijil/schema/absences.py | 15 ++------------
 3 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/aleksis/apps/alsijil/models.py b/aleksis/apps/alsijil/models.py
index 6a0a0c91f..4f7e90a06 100644
--- a/aleksis/apps/alsijil/models.py
+++ b/aleksis/apps/alsijil/models.py
@@ -568,6 +568,33 @@ class Documentation(CalendarEvent):
 
         return (docs, dummies)
 
+    @classmethod
+    def get_documentations_for_person(
+            cls,
+            person: int,
+            start: datetime,
+            end: datetime,
+            incomplete: Optional[bool] = False,
+    ) -> tuple:
+        """Get all the documentations for the person from start to end datetime.
+        Create dummy documentations if none exist.
+        Returns a tuple with a list of existing documentations and a list dummy documentations.
+        """
+        event_params = {
+            "type": "PARTICIPANT",
+            "obj_id": PERSON_ID,
+        }
+
+        events = LessonEvent.get_single_events(
+            start,
+            end,
+            None,
+            event_params,
+            with_reference_object=True,
+        )
+
+        return Documentation.get_documentations_for_events(events, incomplete)
+
 
 class ParticipationStatus(CalendarEvent):
     """A participation or absence record about a single person.
diff --git a/aleksis/apps/alsijil/schema/__init__.py b/aleksis/apps/alsijil/schema/__init__.py
index 612d00d04..662bffe84 100644
--- a/aleksis/apps/alsijil/schema/__init__.py
+++ b/aleksis/apps/alsijil/schema/__init__.py
@@ -157,21 +157,12 @@ class Query(graphene.ObjectType):
         """
         lessons_for_person = []
         for person in persons:
-            event_params = {
-                "type": "PARTICIPANT",
-                "obj_id": PERSON_ID,
-            }
-
-            events = LessonEvent.get_single_events(
+            docs, dummies = Documentation.get_documentations_for_person(
+                person
                 datetime.combine(start, datetime.min.time()),
                 datetime.combine(end, datetime.max.time()),
-                None,
-                event_params,
-                with_reference_object=True,
             )
 
-            docs, dummies = Documentation.get_documentations_for_events(events)
-
             lessons_for_person.append(
                 id=person,
                 lessons=docs + dummies
diff --git a/aleksis/apps/alsijil/schema/absences.py b/aleksis/apps/alsijil/schema/absences.py
index 5eabe0298..55aa8ecf5 100644
--- a/aleksis/apps/alsijil/schema/absences.py
+++ b/aleksis/apps/alsijil/schema/absences.py
@@ -28,23 +28,12 @@ class AbsencesBatchCreateMutation(graphene.Mutation):
 
         for person in persons:
             # Get all documentations for this person between start & end
-            # Could be shared with query
-            event_params = {
-                "type": "PARTICIPANT",
-                "obj_id": PERSON_ID,
-            }
-
-            events = LessonEvent.get_single_events(
+            docs, dummies = Documentation.get_documentations_for_person(
+                person,
                 datetime.combine(start, datetime.min.time()),
                 datetime.combine(end, datetime.max.time()),
-                None,
-                event_params,
-                with_reference_object=True,
             )
 
-            docs, dummies = Documentation.get_documentations_for_events(events)
-            # till here -> reuse?
-
             # Create doc for dummies that are already in the past
             future = false
             for dummy in dummies:
-- 
GitLab