From 31c88e61293e6aae01485d5601e9b23571517339 Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Thu, 14 Sep 2023 17:24:05 +0200
Subject: [PATCH] Fix DatetimeTimezoneMixin to work with create and patch
 mutations

---
 aleksis/apps/chronos/schema/__init__.py | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/aleksis/apps/chronos/schema/__init__.py b/aleksis/apps/chronos/schema/__init__.py
index bdfe3ded..84d0348f 100644
--- a/aleksis/apps/chronos/schema/__init__.py
+++ b/aleksis/apps/chronos/schema/__init__.py
@@ -60,9 +60,10 @@ class LessonEventType(DjangoObjectType):
 class DatetimeTimezoneMixin:
     """Handle datetimes for mutations with CalendarEvent objects.
 
-    Since the client sends timezone information as ISO string
-    which only includes an offset (+00:00 UTC). Because an offset
-    is no valid timezone, we set UTC as timezone directly.
+    This is necessary because the client sends timezone information as
+    ISO string which only includes an offset (+00:00 UTC) and an
+    offset is not a valid timezone. Instead we set UTC as timezone
+    here directly.
     """
 
     @classmethod
@@ -76,7 +77,14 @@ class DatetimeTimezoneMixin:
         return value
 
     @classmethod
-    def before_save(cls, root, info, input, obj):
+    def before_save(cls, root, info, input, obj, patch_obj=False):
+        # before_save has different signatures for different mutations
+        # This handles create & patch
+        # https://graphene-django-cud.readthedocs.io/en/latest/guide/other-hooks.html?highlight=before_save#before-save
+
+        if patch_obj:
+            obj = patch_obj
+            
         obj.timezone = obj.amends.timezone
         return obj
 
-- 
GitLab