diff --git a/program/views.py b/program/views.py index 1c00187c59a7ef968ab706db74340564c51cdf15..c9f60e494201c1d37e4ffa71d56a267cfd214e0f 100644 --- a/program/views.py +++ b/program/views.py @@ -1112,61 +1112,6 @@ class APIScheduleViewSet(viewsets.ModelViewSet): if not self.request.user.has_perm("program.change_schedule"): return Response(status=status.HTTP_401_UNAUTHORIZED) - if request.method == "PATCH": - # only these fields can be updated without generating conflicts - allowed = {"default_playlist_id", "is_repetition", "last_date"} - update_fields = set(request.data.get("schedule").keys()) - - if update_fields.issubset(allowed): - schedule = self.get_object() - - default_playlist_id = request.data.get("schedule").get("default_playlist_id") - if default_playlist_id == "" or default_playlist_id is None: - # "clear" the default_playlist_id if the field has no value - schedule.default_playlist_id = None - else: - schedule.default_playlist_id = int(default_playlist_id) - - is_repetition = request.data.get("schedule").get("is_repetition") - if is_repetition == "true" or is_repetition == "1": - schedule.is_repetition = True - if is_repetition == "false" or is_repetition == "0": - schedule.is_repetition = False - - last_date = request.data.get("schedule").get("last_date") - if last_date == "" or last_date is None: - # "clear" the last_date if the field has no value - schedule.last_date = None - else: - try: - last_date = date.fromisoformat(last_date) - except ValueError as e: - data = {"last_date": e.args[0]} - - return Response(data, status=status.HTTP_400_BAD_REQUEST) - if schedule.last_date is None or schedule.last_date > last_date: - schedule.last_date = last_date - - last_end = timezone.make_aware( - datetime.combine(last_date, schedule.end_time) - ) - - TimeSlot.objects.filter(schedule=schedule, start__gt=last_end).delete() - else: - data = {"last_date": "This field cannot be updated to this date"} - - return Response(data, status=status.HTTP_400_BAD_REQUEST) - - schedule.save() - serializer = ScheduleSerializer(schedule) - - return Response(serializer.data) - else: - bad_fields = update_fields.difference(allowed) - data = {field: "This field cannot be updated with PATCH" for field in bad_fields} - - return Response(data, status=status.HTTP_400_BAD_REQUEST) - # Only allow updating when with the `schedule` JSON object if "schedule" not in request.data: return Response(status=status.HTTP_400_BAD_REQUEST) @@ -1196,6 +1141,63 @@ class APIScheduleViewSet(viewsets.ModelViewSet): return Response(resolution) + def partial_update(self, request, *args, **kwargs): + """ + Partial update a schedule without generating timeslots, testing or resolving collisions. + """ + + # only these fields can be updated without generating conflicts + allowed = {"default_playlist_id", "is_repetition", "last_date"} + update_fields = set(request.data.get("schedule").keys()) + + if update_fields.issubset(allowed): + schedule = self.get_object() + + default_playlist_id = request.data.get("schedule").get("default_playlist_id") + if default_playlist_id == "" or default_playlist_id is None: + # "clear" the default_playlist_id if the field has no value + schedule.default_playlist_id = None + else: + schedule.default_playlist_id = int(default_playlist_id) + + is_repetition = request.data.get("schedule").get("is_repetition") + if is_repetition == "true" or is_repetition == "1": + schedule.is_repetition = True + if is_repetition == "false" or is_repetition == "0": + schedule.is_repetition = False + + last_date = request.data.get("schedule").get("last_date") + if last_date == "" or last_date is None: + # "clear" the last_date if the field has no value + schedule.last_date = None + else: + try: + last_date = date.fromisoformat(last_date) + except ValueError as e: + data = {"last_date": e.args[0]} + + return Response(data, status=status.HTTP_400_BAD_REQUEST) + if schedule.last_date is None or schedule.last_date > last_date: + schedule.last_date = last_date + + last_end = timezone.make_aware(datetime.combine(last_date, schedule.end_time)) + + TimeSlot.objects.filter(schedule=schedule, start__gt=last_end).delete() + else: + data = {"last_date": "This field cannot be updated to this date"} + + return Response(data, status=status.HTTP_400_BAD_REQUEST) + + schedule.save() + serializer = ScheduleSerializer(schedule) + + return Response(serializer.data) + else: + bad_fields = update_fields.difference(allowed) + data = {field: "This field cannot be updated with PATCH" for field in bad_fields} + + return Response(data, status=status.HTTP_400_BAD_REQUEST) + # TODO: Create is currently not implemented because timeslots are supposed to be inserted # by creating or updating a schedule.