diff --git a/aleksis/apps/alsijil/schema/absences.py b/aleksis/apps/alsijil/schema/absences.py index 42dfce4668c41db830facacb352871ebc3a619ee..6bb9b9481a1120efb172578f13d8e03d5b0ad9a0 100644 --- a/aleksis/apps/alsijil/schema/absences.py +++ b/aleksis/apps/alsijil/schema/absences.py @@ -1,5 +1,10 @@ import graphene +from datetime import datetime + +from aleksis.apps.kolego.models import Absence + from .documentation import DocumentationType +from ..models import ParticipationStatus class LessonsForPersonType(graphene.ObjectType): id = graphene.ID() # noqa @@ -16,16 +21,73 @@ class AbsencesBatchCreateMutation(graphene.Mutation): ok = graphene.Boolean() @classmethod - def mutate(cls, root, info, input): # noqa - # Get all lesson events for person (share with query) - # Look up documentations for lesson events - # (share with models.py Documentation get_for_coursebook; - # has lesson event lookup as well & returns real & fake documentations - # => extract & reuse the documentation lookup) - # Create a ParticipationStatus for each documentation - # (OR if no documentation and in past create documentation) - # If there are any LessonEvents without a documentation - # create a Kolego Absence for the whole duration - # Return ok=True if everything went well. + def mutate(cls, root, info, persons, start, end, comment, reason): # noqa + # TODO: More permission checks needed? + # DocumentationBatchCreateOrUpdateMutation.create_or_update + # at least already checks permissions. + + 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( + 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? - return AbsencesBatchCreateMutation(ok=BOOL) + # Create doc for dummies that are already in the past + future = false + for dummy in dummies: + # TODO/MAYBE: This in past logic could be somewhere else OR shared. + # The next 5 lines are shared with DocumentationBatchCreateOrUpdateMutation + # & could be deduplicated + dummy, lesson_event_id, datetime_start_iso, datetime_end_iso = _id.split(";") + lesson_event = LessonEvent.objects.get(id=lesson_event_id) + start = datetime.fromisoformat(datetime_start_iso).astimezone( + lesson_event.timezone + ) + + if start < datetime.now(): + # In the past -> Create a Documentation + docs.append( + DocumentationBatchCreateOrUpdateMutation.create_or_update(info, dummy) + ) + else: + future = true + + # Create a ParticipationStatus for each documentation + for doc in docs: + # TODO: Is ID for enough for person&reason OR should it + # resolve to django object first? + ParticipationStatus.objects.create( + person=person, + related_documentation=doc, + absence_reason=reason, + ) + + # If there are still dummy documentations in the future + # create a Kolego Absence + if future: + # TODO: Should probably lookup person&reason as well + # TODO: Are date_start & date_end from CalendarEvent enough + # or more needed? + Absence.objects.create( + date_start=datetime.now().date(), + date_end=end, + reason=reason, + person=person, + comment=comment, + ) + + # Return ok=True if everything went well. + return AbsencesBatchCreateMutation(ok=True)