Skip to content

Commit

Permalink
Accept from/to date query params in API queries to series
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaith committed Jan 20, 2016
1 parent 2a75319 commit 36f9c5c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 12 additions & 1 deletion talks/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,19 @@ class Meta:


class EventGroupEmbedsSerializer(serializers.ModelSerializer):
talks = HALEventSerializer(many=True, read_only=True, source='events')
talks = serializers.SerializerMethodField()

def get_talks(self,obj):
events = obj.events
if self.context.has_key('from-date') or self.context.has_key('to-date'):
if self.context['from-date']:
events = events.filter(start__gte=self.context['from-date'])
if self.context['to-date']:
events = events.filter(end__lte=self.context['to-date'])

serializer = HALEventSerializer(events, many=True, read_only=True)
return serializer.data

class Meta:
model = EventGroup
fields = ('talks',)
Expand Down
11 changes: 8 additions & 3 deletions talks/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ def api_event_group(request, event_group_slug):
if not eg:
return Response({'error': "Item not found"},
status=status.HTTP_404_NOT_FOUND)

serializer = HALEventGroupSerializer(eg)
from_date = parse_date(request.GET.get('from', ''))
to_date = parse_date(request.GET.get('to',''))

if from_date or to_date:
serializer = HALEventGroupSerializer(eg, context={'from-date': from_date, 'to-date': to_date})
else:
serializer = HALEventGroupSerializer(eg)
return Response(serializer.data, status=status.HTTP_200_OK)


Expand Down Expand Up @@ -320,7 +325,7 @@ def api_collection(request, collection_slug):
try:
collection = Collection.objects.get(slug=collection_slug)
if collection.public:
if from_date and to_date:
if from_date or to_date:
serializer = HALCollectionSerializer(collection, context={'from-date': from_date, 'to-date': to_date})
else:
serializer = HALCollectionSerializer(collection)
Expand Down

0 comments on commit 36f9c5c

Please sign in to comment.