From 98647a7fd6e362d6615c6aa106d99afa53454075 Mon Sep 17 00:00:00 2001
From: Julian Leucker <leuckerj@gmail.com>
Date: Tue, 26 Mar 2024 19:39:26 +0100
Subject: [PATCH] Add teachers to DocumentationType

---
 aleksis/apps/alsijil/schema/documentation.py | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/aleksis/apps/alsijil/schema/documentation.py b/aleksis/apps/alsijil/schema/documentation.py
index 71ad9c468..6577704e0 100644
--- a/aleksis/apps/alsijil/schema/documentation.py
+++ b/aleksis/apps/alsijil/schema/documentation.py
@@ -38,6 +38,7 @@ class DocumentationType(PermissionsTypeMixin, DjangoFilterMixin, DjangoObjectTyp
             "datetime_end",
             "date_start",
             "date_end",
+            "teachers",
         )
         filter_fields = {
             "id": ["exact", "lte", "gte"],
@@ -47,6 +48,12 @@ class DocumentationType(PermissionsTypeMixin, DjangoFilterMixin, DjangoObjectTyp
     course = graphene.Field(CourseType, required=False)
     subject = graphene.Field(SubjectType, required=False)
 
+    @staticmethod
+    def resolve_teachers(root: Documentation, info, **kwargs):
+        if not str(root.pk).startswith("DUMMY") and hasattr(root, "teachers"):
+            return  root.teachers
+        return root.course.teachers
+
     @classmethod
     def get_queryset(cls, queryset, info):
         return get_objects_for_user(info.context.user, "alsijil.view_documentation", queryset)
@@ -129,6 +136,7 @@ class DocumentationInputType(graphene.InputObjectType):
     id = graphene.ID(required=True)
     course = graphene.ID(required=False)
     subject = graphene.ID(required=False)
+    teachers = graphene.List(graphene.ID, required=False)
 
     topic = graphene.String(required=False)
     homework = graphene.String(required=False)
@@ -158,7 +166,7 @@ class DocumentationBatchCreateOrUpdateMutation(graphene.Mutation):
 
             # Timezone removal is necessary due to ISO style offsets are no valid timezones.
             # Instead, we take the timezone from the lesson_event and save it in a dedicated field.
-            return Documentation.objects.create(
+            obj = Documentation.objects.create(
                 datetime_start=datetime.fromisoformat(datetime_start).replace(tzinfo=timezone.utc),
                 datetime_end=datetime.fromisoformat(datetime_end).replace(tzinfo=timezone.utc),
                 timezone=lesson_event.timezone,
@@ -169,6 +177,12 @@ class DocumentationBatchCreateOrUpdateMutation(graphene.Mutation):
                 homework=doc.homework or "",
                 group_note=doc.group_note or "",
             )
+            if doc.teachers is not None:
+                obj.teachers.add(*doc.teachers)
+            else:
+                obj.teachers.set(lesson_event.course.teachers)
+            obj.save()
+            return obj
         else:
             obj = Documentation.objects.get(id=_id)
 
-- 
GitLab