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

Implement AbsencesBatchCreateMutation (WIP)

parent 19adf0c0
No related branches found
No related tags found
1 merge request!361Resolve "Add statistics page for absences"
import graphene import graphene
from datetime import datetime
from aleksis.apps.kolego.models import Absence
from .documentation import DocumentationType from .documentation import DocumentationType
from ..models import ParticipationStatus
class LessonsForPersonType(graphene.ObjectType): class LessonsForPersonType(graphene.ObjectType):
id = graphene.ID() # noqa id = graphene.ID() # noqa
...@@ -16,16 +21,73 @@ class AbsencesBatchCreateMutation(graphene.Mutation): ...@@ -16,16 +21,73 @@ class AbsencesBatchCreateMutation(graphene.Mutation):
ok = graphene.Boolean() ok = graphene.Boolean()
@classmethod @classmethod
def mutate(cls, root, info, input): # noqa def mutate(cls, root, info, persons, start, end, comment, reason): # noqa
# Get all lesson events for person (share with query) # TODO: More permission checks needed?
# Look up documentations for lesson events # DocumentationBatchCreateOrUpdateMutation.create_or_update
# (share with models.py Documentation get_for_coursebook; # at least already checks permissions.
# has lesson event lookup as well & returns real & fake documentations
# => extract & reuse the documentation lookup) for person in persons:
# Create a ParticipationStatus for each documentation # Get all documentations for this person between start & end
# (OR if no documentation and in past create documentation) # Could be shared with query
# If there are any LessonEvents without a documentation event_params = {
# create a Kolego Absence for the whole duration "type": "PARTICIPANT",
# Return ok=True if everything went well. "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