Verified Commit 4d52e6c9 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Add start and end query paramters to events API

parent 99db59f1
......@@ -27,26 +27,48 @@ from events.exceptions import RegistrationError
from events.models import Event, Registration
def _extract_date(param):
if param is None:
return None
return timezone.make_aware(datetime.strptime(param, '%Y-%m-%d'))
def _extract_date_range(request):
start = timezone.make_aware(
datetime.strptime(request.query_params['start'], '%Y-%m-%d')
end = timezone.make_aware(
datetime.strptime(request.query_params['end'], '%Y-%m-%d')
start = _extract_date(request.query_params['start'])
end = _extract_date(request.query_params['end'])
except (ValueError, KeyError, InvalidTimeError) as e:
raise ParseError(detail='start or end query parameters invalid') from e
return end, start
class EventViewset(viewsets.ReadOnlyModelViewSet):
queryset = Event.objects.filter(,
queryset = Event.objects.filter(published=True)
permission_classes = [IsAuthenticated]
filter_backends = (filters.OrderingFilter,)
ordering_fields = ('start', 'end')
def get_queryset(self):
queryset = Event.objects.filter(published=True)
start = _extract_date(self.request.query_params.get('start', None))
except (ValueError, InvalidTimeError) as e:
raise ParseError(detail='start query parameter invalid') from e
end = _extract_date(self.request.query_params.get('end', None))
except (ValueError, InvalidTimeError) as e:
raise ParseError(detail='end query parameter invalid') from e
if start is not None:
queryset = queryset.filter(start__gte=start)
if end is not None:
queryset = queryset.filter(end__lte=end)
if start is None and end is None:
queryset = queryset.filter(
return queryset
def get_serializer_class(self):
if self.action == 'list':
return EventListSerializer
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment