Skip to content
Snippets Groups Projects

Add customizable ordering and new request type for timeslot queryset

Merged Richard Blechinger requested to merge ordering-timeslots into master
+ 26
4
@@ -19,6 +19,7 @@
#
import json
import logging
from datetime import date, datetime, time, timedelta
from django.contrib.auth.models import User
@@ -37,6 +38,7 @@ from program.serializers import TypeSerializer, LanguageSerializer, MusicFocusSe
UserSerializer
from program.utils import get_cached_shows
logger = logging.getLogger(__name__)
def json_day_schedule(request, year=None, month=None, day=None):
if year is None and month is None and day is None:
@@ -507,6 +509,7 @@ class APITimeSlotViewSet(viewsets.ModelViewSet):
/timeslots/{pk} eturns the given timeslot (GET)
/timeslots/?start={start_date}&end={end_date} returns timeslots within the time range (GET)
/shows/{show_pk}/timeslots returns timeslots of the show (GET, POST)
/shows/{show_pk}/timeslots?surrounding returns the 10 nearest timeslots for the current date (GET)
/shows/{show_pk}/timeslots/{pk} returns a timeslots by its ID (GET, PUT, DELETE)
/shows/{show_pk}/timeslots/?start={start_date}&end={end_date} returns timeslots of the show within the time range
/shows/{show_pk}/schedules/{schedule_pk}/timeslots returns all timeslots of the schedule (GET, POST)
@@ -522,7 +525,6 @@ class APITimeSlotViewSet(viewsets.ModelViewSet):
def get_queryset(self):
show_pk = int_or_none('show_pk', self.kwargs)
schedule_pk = int_or_none('schedule_pk', self.kwargs)
# Filters
# Return next 60 days by default
@@ -533,6 +535,25 @@ class APITimeSlotViewSet(viewsets.ModelViewSet):
start = datetime.combine(datetime.strptime(self.request.query_params.get('start'), '%Y-%m-%d').date(), time(0, 0))
end = datetime.combine(datetime.strptime(self.request.query_params.get('end'), '%Y-%m-%d').date(), time(23, 59))
default_order = '-start'
order = self.request.query_params.get('order', default_order)
# If someone tries to sort by a field that isn't available on the model
# we silently ignore that and use the default sort order.
model_fields = [field.name for field in TimeSlot._meta.get_fields()]
if order.lstrip('-') not in model_fields:
order = default_order
if 'surrounding' in self.request.query_params:
today = datetime.today()
nearest_timeslots_in_future = TimeSlot.objects.filter(start__gte=today).order_by('start').values_list('id', flat=True)[:5]
nearest_timeslots_in_past = TimeSlot.objects.filter(start__lt=today).order_by('-start').values_list('id', flat=True)[:5]
relevant_timeslot_ids = list(nearest_timeslots_in_future) + list(nearest_timeslots_in_past)
return TimeSlot.objects.filter(id__in=relevant_timeslot_ids).order_by(order)
# Endpoints
#
@@ -541,7 +562,7 @@ class APITimeSlotViewSet(viewsets.ModelViewSet):
# Returns timeslots of the given show and schedule
#
if show_pk and schedule_pk:
return TimeSlot.objects.filter(show=show_pk, schedule=schedule_pk, start__gte=start, end__lte=end).order_by('start')
return TimeSlot.objects.filter(show=show_pk, schedule=schedule_pk, start__gte=start, end__lte=end).order_by(order)
#
# /shows/1/timeslots/
@@ -549,7 +570,7 @@ class APITimeSlotViewSet(viewsets.ModelViewSet):
# Returns timeslots of the show
#
elif show_pk and schedule_pk is None:
return TimeSlot.objects.filter(show=show_pk, start__gte=start, end__lte=end).order_by('start')
return TimeSlot.objects.filter(show=show_pk, start__gte=start, end__lte=end).order_by(order)
#
# /timeslots/
@@ -557,12 +578,13 @@ class APITimeSlotViewSet(viewsets.ModelViewSet):
# Returns all timeslots
#
else:
return TimeSlot.objects.filter(start__gte=start, end__lte=end).order_by('start')
return TimeSlot.objects.filter(start__gte=start, end__lte=end).order_by(order)
def retrieve(self, request, *args, **kwargs):
pk = int_or_none('pk', self.kwargs)
show_pk = int_or_none('show_pk', self.kwargs)
if show_pk:
timeslot = get_object_or_404(TimeSlot, pk=pk, show=show_pk)
else:
Loading