Exemple #1
0
    def put(self, request):
        serializer = ToDoSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
 def put(self, request, pk):
     todo = get_object_or_404(ToDo, pk=pk)
     serializer = ToDoSerializer(todo, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #3
0
    def test_user_can_create_todo(self):
        """
        Test that user can create new ToDo
        """
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        request_data = ToDoSerializer({
            'title': 'Create new ToDo via this API'
        }).data
        response = self.client.post('/todo/', data=request_data)
        # Checks that valid status code has been returned
        self.assertEqual(response.status_code, 201)
        # Checks that  objects count has been increased
        todo = ToDo.objects.by_user(self.user)
        self.assertEqual(todo.count(), len(self.todo) + 1)

        # Checks that object with valid `title` and `created_by` values
        # has been created
        try:
            new_todo = ToDo.objects.get(title='Create new ToDo via this API')
        except ToDo.DoesNotExist:
            new_todo = None
        self.assertIsNotNone(new_todo)
        self.assertEqual(new_todo.created_by, self.user)
Exemple #4
0
 def get(self, request, pk):
     todo = get_object_or_404(ToDoElements, pk=pk)
     serializer = ToDoSerializer(todo)
     return Response(serializer.data)
Exemple #5
0
 def get(self, request):
     todos = ToDoElements.objects.all()
     serializer = ToDoSerializer(todos, many=True)
     return Response(serializer.data)
 def get(self, request):
     todos = ToDo.objects.all()
     serializer = ToDoSerializer(todos, many=True)
     print(get_client_ip(request))
     return Response(serializer.data)