From fbf25845255efaefa5b9d9ab312426abb516e043 Mon Sep 17 00:00:00 2001 From: Michael Bauer <michael-bauer@posteo.de> Date: Mon, 5 Feb 2024 18:07:13 +0100 Subject: [PATCH] Fix batch documentation mutation --- aleksis/apps/alsijil/schema/documentation.py | 70 +++++++++++--------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/aleksis/apps/alsijil/schema/documentation.py b/aleksis/apps/alsijil/schema/documentation.py index a866e80c4..68e327529 100644 --- a/aleksis/apps/alsijil/schema/documentation.py +++ b/aleksis/apps/alsijil/schema/documentation.py @@ -136,39 +136,43 @@ class DocumentationBatchCreateOrUpdateMutation(graphene.Mutation): documentations = graphene.List(DocumentationType) @classmethod - def mutate(cls, root, info, input): - for doc in input: - id = doc.id + def create_or_update(cls, info, doc): + id = doc.id - # Sadly, we can't use the update_or_create method since create_defaults is only introduced in Django 5.0 - if id.startswith("DUMMY"): - dummy, lesson_event_id, datetime_start, datetime_end = id.split(";") - lesson_event = LessonEvent.objects.get(id=lesson_event_id) + # Sadly, we can't use the update_or_create method since create_defaults is only introduced in Django 5.0 + if id.startswith("DUMMY"): + dummy, lesson_event_id, datetime_start, datetime_end = id.split(";") + lesson_event = LessonEvent.objects.get(id=lesson_event_id) - if not info.context.user.has_perm( + if not info.context.user.has_perm( "alsijil.add_documentation_for_lesson_event_rule", lesson_event - ): - raise PermissionDenied() - - obj = Documentation.objects.create( - datetime_start=datetime.fromisoformat(datetime_start), - datetime_end=datetime.fromisoformat(datetime_end), - lesson_event=lesson_event, - course=lesson_event.course, - subject=lesson_event.subject, - topic=doc.topic or "", - homework=doc.homework or "", - group_note=doc.group_note or "", - ) # TODO: Add course & subject - else: - obj = Documentation.objects.get(id=id) - - if not info.context.user.has_perm("alsijil.edit_documentation_rule", obj): - raise PermissionDenied() - - obj.topic = doc.topic or "" - obj.homework = doc.homework or "" - obj.group_note = doc.group_note or "" - obj.save() - - return DocumentationBatchCreateOrUpdateMutation(documentations=obj) + ): + raise PermissionDenied() + + return Documentation.objects.create( + datetime_start=datetime.fromisoformat(datetime_start), + datetime_end=datetime.fromisoformat(datetime_end), + lesson_event=lesson_event, + course=lesson_event.course, + subject=lesson_event.subject, + topic=doc.topic or "", + homework=doc.homework or "", + group_note=doc.group_note or "", + ) # TODO: Add course & subject + else: + obj = Documentation.objects.get(id=id) + + if not info.context.user.has_perm("alsijil.edit_documentation_rule", obj): + raise PermissionDenied() + + obj.topic = doc.topic or "" + obj.homework = doc.homework or "" + obj.group_note = doc.group_note or "" + obj.save() + return obj + + @classmethod + def mutate(cls, root, info, input): + objs = [cls.create_or_update(info, doc) for doc in input] + + return DocumentationBatchCreateOrUpdateMutation(documentations=objs) -- GitLab