Skip to content

Commit

Permalink
Merge pull request #398 from ox-it/collection_api_filtered_by_date_range
Browse files Browse the repository at this point in the history
Collection api filtered by date range
  • Loading branch information
ahaith committed Nov 25, 2015
2 parents 6361a9f + e915be9 commit 1cf7615
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion talks/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,18 @@ class Meta:


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

def get_talks(self,obj):
events = obj.get_all_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 = Collection
Expand Down
10 changes: 9 additions & 1 deletion talks/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
HALEventGroupSerializer, HALSearchResultSerializer, EventSerializer, HALCollectionSerializer)
from talks.api.services import events_search, get_event_by_slug, get_eventgroup_by_slug
from talks.core.renderers import ICalRenderer
from talks.core.utils import parse_date

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -312,10 +313,17 @@ def api_collection(request, collection_slug):
:param collection_slug: collection slug
:return: DRF response object
"""

# If from and to dates have been passed as request parameters, filter the events by those dates.
from_date = parse_date(request.GET.get('from', ''))
to_date = parse_date(request.GET.get('to', ''))
try:
collection = Collection.objects.get(slug=collection_slug)
if collection.public:
serializer = HALCollectionSerializer(collection)
if from_date and to_date:
serializer = HALCollectionSerializer(collection, context={'from-date': from_date, 'to-date': to_date})
else:
serializer = HALCollectionSerializer(collection)
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response({'error': "Collection is not public"},
Expand Down

0 comments on commit 1cf7615

Please sign in to comment.