Skip to content
Snippets Groups Projects
Commit a6ee60c4 authored by permcu's avatar permcu Committed by Julian
Browse files

Implement AbsencesBatchCreateMutation (WIP)

parent 665173dc
No related branches found
No related tags found
1 merge request!360Resolve "Add absence management to course book student dialog"
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)
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