from django.http import HttpResponseServerError from rest_framework.viewsets import ViewSet from rest_framework.response import Response from rest_framework import serializers, status from levelupapi.models import Event class EventView(ViewSet): """Level up event types view""" def retrieve(self, request, pk): """Handle GET requests for single event type Returns: Response -- JSON serialized event type """ try: game_type = Event.objects.get(pk=pk) serializer = EventSerializer(game_type) return Response(serializer.data) except Event.DoesNotExist as ex: return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND) def list(self, request): """Handle GET requests to get all event types Returns: Response -- JSON serialized list of event types """ events = Event.objects.all() # Add in the next 3 lines game = request.query_params.get('game_id', None) if game is not None: events = events.filter(game_id=game) serializer = EventSerializer(events, many=True) return Response(serializer.data) class EventSerializer(serializers.ModelSerializer): """JSON serializer for event types """ class Meta: model = Event fields = ('id', 'description', 'date', 'time', 'game_id', 'organizer_id') depth = 2