Skip to content
Snippets Groups Projects
Commit a96d67c2 authored by Julian's avatar Julian
Browse files

Finalize CopySlotsFromDifferentTimeGridMutation

parent 550cc8f9
No related branches found
No related tags found
1 merge request!2Resolve "Frontend for Models"
......@@ -38,6 +38,7 @@ from .lesson import (
)
from .slot import (
CarryOverSlotsMutation,
CopySlotsFromDifferentTimeGridMutation,
SlotBatchCreateMutation,
SlotBatchDeleteMutation,
SlotBatchPatchMutation,
......@@ -269,6 +270,7 @@ class Mutation(graphene.ObjectType):
delete_timebound_course_config = TimeboundCourseConfigDeleteMutation.Field()
update_timebound_course_configs = TimeboundCourseConfigBatchPatchMutation.Field()
carry_over_slots = CarryOverSlotsMutation.Field()
copy_slots_from_grid = CopySlotsFromDifferentTimeGridMutation.Field()
create_validity_range = ValidityRangeCreateMutation.Field()
delete_validity_range = ValidityRangeDeleteMutation.Field()
......
......@@ -158,7 +158,7 @@ class CarryOverSlotsMutation(graphene.Mutation):
)
class CopySlotsFromDifferentRangeMutation(graphene.Mutation):
class CopySlotsFromDifferentTimeGridMutation(graphene.Mutation):
class Arguments:
time_grid = graphene.ID()
from_time_grid = graphene.ID()
......@@ -177,6 +177,8 @@ class CopySlotsFromDifferentRangeMutation(graphene.Mutation):
# Check for each slot in the from_time_grid if it exists in the time_grid, if not, create it
slots = Slot.objects.filter(time_grid=from_time_grid)
result = []
for slot in slots:
slot: BreakSlot | Slot
klass = slot.get_real_instance_class()
......@@ -186,17 +188,19 @@ class CopySlotsFromDifferentRangeMutation(graphene.Mutation):
if hasattr(slot, "period_after"):
defaults["period_after"] = slot.period_after
klass.objects.update_or_create(
weekday=slot.weekday, time_grid=time_grid, period=slot.period, defaults=defaults
result.append(
klass.objects.update_or_create(
weekday=slot.weekday, time_grid=time_grid, period=slot.period, defaults=defaults
)[0].id
)
# Delete all slots in the time_grid that are not in the from_time_grid
objects_to_delete = Slot.objects.filter(time_grid=time_grid).exclude(id__in=slots)
objects_to_delete = Slot.objects.filter(time_grid=time_grid).exclude(id__in=result)
objects_to_delete.delete()
deleted = objects_to_delete.values_list("id", flat=True)
return CopySlotsFromDifferentRangeMutation(
return CopySlotsFromDifferentTimeGridMutation(
deleted=deleted,
result=Slot.objects.filter(time_grid=time_grid).non_polymorphic(),
)
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