예제 #1
0
    def test_get_game(self):
        """
        Ensure we can get an existing game.
        """
        event = Event()
        event.event_day = "1820-10-25"
        event.event_time = "14:39"
        event.game_id = 1
        event.location = "Tom's"
        event.gamer_id = 1

        event.save()

        # Make sure request is authenticated
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)

        # Initiate request and store response
        response = self.client.get(f"/events/{event.id}")

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Assert that the game was retrieved
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # Assert that the values are correct
        self.assertEqual(json_response["event_day"], "1820-10-25")
        self.assertEqual(json_response["event_time"], "14:39:00")
        self.assertEqual(json_response["location"], "Tom's")
예제 #2
0
    def create(self, request):
        """Handle POST operations for events

        Returns:
            Response -- JSON serialized event instance
        """
        gamer = Gamer.objects.get(user=request.auth.user)

        event = Event()
        event.event_day = request.data["event_day"]
        event.event_time = request.data["event_time"]
        game = Game.objects.get(pk=request.data["game"])
        event.game = game
        event.location = request.data["location"]
        event.gamer = gamer


        try:
            event.save()
            serializer = EventSerializer(event, context={'request': request})
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        except ValidationError as ex:
            return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)