Skip to content
Snippets Groups Projects
Commit c731ed02 authored by permcu's avatar permcu
Browse files

Change documentation mutation to batch

Now it works with the createOrPatchMixin.
Suggestion is to really standardize on batch mutations.
parent 00b1c0e5
No related branches found
No related tags found
2 merge requests!352Draft: Resolve "Add dialog with each lesson's students",!350Resolve "Add simple course book list"
......@@ -13,7 +13,7 @@ from .documentation import (
DocumentationBatchCreateMutation,
DocumentationBatchPatchMutation,
DocumentationCreateMutation,
DocumentationCreateOrUpdateMutation,
DocumentationBatchCreateOrUpdateMutation,
DocumentationDeleteMutation,
DocumentationType,
)
......@@ -77,4 +77,4 @@ class Mutation(graphene.ObjectType):
delete_documentation = DocumentationDeleteMutation.Field()
update_documentations = DocumentationBatchPatchMutation.Field()
create_or_update_documentation = DocumentationCreateOrUpdateMutation.Field()
create_or_update_documentations = DocumentationBatchCreateOrUpdateMutation.Field()
......@@ -120,6 +120,7 @@ class DocumentationBatchPatchMutation(PermissionBatchPatchMixin, DjangoBatchPatc
class DocumentationInputType(graphene.InputObjectType):
id = graphene.ID(required=True)
course = graphene.ID(required=False)
subject = graphene.ID(required=False)
......@@ -128,44 +129,46 @@ class DocumentationInputType(graphene.InputObjectType):
group_note = graphene.String(required=False)
class DocumentationCreateOrUpdateMutation(graphene.Mutation):
class DocumentationBatchCreateOrUpdateMutation(graphene.Mutation):
class Arguments:
id = graphene.ID(required=True)
input = DocumentationInputType(required=False)
input = graphene.List(DocumentationInputType)
documentation = graphene.Field(DocumentationType)
documentations = graphene.List(DocumentationType)
@classmethod
def mutate(cls, root, info, id, input):
# 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(
"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=input.topic,
homework=input.homework,
group_note=input.group_note,
) # 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 = input.topic
obj.homework = input.homework
obj.group_note = input.group_note
obj.save()
return DocumentationCreateOrUpdateMutation(documentation=obj)
def mutate(cls, root, info, input):
for doc in input:
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)
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment