Skip to content
Snippets Groups Projects

refactor schedule REST API

Merged Konrad Mohrfeldt requested to merge kmohrf/schedule-api-refactor into master
1 file
+ 2
4
Compare changes
  • Side-by-side
  • Inline
+ 24
40
@@ -42,6 +42,7 @@ from program.models import (
MusicFocus,
Note,
Schedule,
ScheduleConflictError,
Show,
TimeSlot,
Topic,
@@ -58,6 +59,7 @@ from program.serializers import (
ScheduleConflictResponseSerializer,
ScheduleCreateUpdateRequestSerializer,
ScheduleDryRunResponseSerializer,
ScheduleResponseSerializer,
ScheduleSerializer,
ShowSerializer,
TimeSlotSerializer,
@@ -344,9 +346,10 @@ class APIShowViewSet(DisabledObjectPermissionCheckMixin, viewsets.ModelViewSet):
@extend_schema_view(
create=extend_schema(
summary="Create a new schedule.",
request=ScheduleCreateUpdateRequestSerializer,
responses={
status.HTTP_201_CREATED: OpenApiResponse(
response=ScheduleConflictResponseSerializer,
response=ScheduleResponseSerializer,
description=(
"Signals the successful creation of the schedule and of the projected "
"timeslots."
@@ -366,9 +369,9 @@ class APIShowViewSet(DisabledObjectPermissionCheckMixin, viewsets.ModelViewSet):
Returned in case the request contained invalid data.
This may happen if:
* the until date is before the start date (`no-start-after-end`),
* the last date is before the start date (`no-start-after-end`),
in which case you should correct either the start or until date.
* The start and until date are the same (`no-same-day-start-and-end`).
* The start and last date are the same (`no-same-day-start-and-end`).
This is only allowed for single timeslots with the recurrence rule
set to `once`. You should fix either the start or until date.
* The number of conflicts and solutions aren’t the same
@@ -410,8 +413,14 @@ class APIShowViewSet(DisabledObjectPermissionCheckMixin, viewsets.ModelViewSet):
},
),
retrieve=extend_schema(summary="Retrieve a single schedule."),
update=extend_schema(summary="Update an existing schedule."),
partial_update=extend_schema(summary="Partially update an existing schedule."),
update=extend_schema(
summary="Update an existing schedule.",
request=ScheduleCreateUpdateRequestSerializer,
),
partial_update=extend_schema(
summary="Partially update an existing schedule.",
request=ScheduleCreateUpdateRequestSerializer,
),
destroy=extend_schema(summary="Delete an existing schedule."),
list=extend_schema(summary="List all schedules."),
)
@@ -475,28 +484,16 @@ class APIScheduleViewSet(
if show_pk is None or "schedule" not in request.data:
return Response(status=status.HTTP_400_BAD_REQUEST)
# First create submit -> return projected timeslots and collisions
# TODO: Perhaps directly insert into database if no conflicts found
if "solutions" not in request.data:
# TODO: respond with status.HTTP_409_CONFLICT when the dashboard can handle it
return Response(
Schedule.make_conflicts(request.data["schedule"], pk, show_pk),
)
# Otherwise try to resolve
resolution = Schedule.resolve_conflicts(request.data, pk, show_pk)
try:
resolution = Schedule.resolve_conflicts(request.data, pk, show_pk)
except ScheduleConflictError as exc:
return Response(exc.conflicts, status.HTTP_409_CONFLICT)
if all(key in resolution for key in ["create", "update", "delete"]):
# this is a dry-run
return Response(resolution, status=status.HTTP_202_ACCEPTED)
# If resolution went well
if "projected" not in resolution:
return Response(resolution, status=status.HTTP_201_CREATED)
# Otherwise return conflicts
# TODO: respond with status.HTTP_409_CONFLICT when the dashboard can handle it
return Response(resolution)
return Response(resolution, status=status.HTTP_201_CREATED)
def update(self, request, *args, **kwargs):
"""
@@ -532,26 +529,13 @@ class APIScheduleViewSet(
serializer = ScheduleSerializer(schedule)
return Response(serializer.data)
# First update submit -> return projected timeslots and collisions
if "solutions" not in request.data:
# TODO: respond with status.HTTP_409_CONFLICT when the dashboard can handle it
return Response(
Schedule.make_conflicts(
request.data["schedule"], schedule.pk, schedule.show.pk
)
try:
resolution = Schedule.resolve_conflicts(
request.data, schedule.pk, schedule.show.pk
)
except ScheduleConflictError as exc:
return Response(exc.conflicts, status.HTTP_409_CONFLICT)
# Otherwise try to resolve
resolution = Schedule.resolve_conflicts(
request.data, schedule.pk, schedule.show.pk
)
# If resolution went well
if "projected" not in resolution:
return Response(resolution)
# Otherwise return conflicts
# TODO: respond with status.HTTP_409_CONFLICT when the dashboard can handle it
return Response(resolution)
def destroy(self, request, *args, **kwargs):
Loading