Exemplo n.º 1
0
 def post(self, request):
     serializer = AuthorSerializer(data=request.data)
     if not serializer.is_valid():
         return Response(serializer.errors, status=400)
     
     serializer.save()
     return Response(serializer.data, status=201)
Exemplo n.º 2
0
def author_detail(request, author_id):
    try:
        category = Author.objects.get(id=author_id)
    except Author.DoesNotExist as e:
        return JsonResponse({'error': str(e)})

    if request.method == 'GET':
        serializer = AuthorSerializer(category)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':

        request_body = json.loads(request.body)

        serializer = AuthorSerializer(instance=category, data=request_body)

        if serializer.is_valid():
            serializer.save()

            return JsonResponse(serializer.data)

        return JsonResponse({'error': serializer.errors})

        # Delete selected object

    elif request.method == 'DELETE':

        category.delete()

        return JsonResponse({'deleted': True})
Exemplo n.º 3
0
 def patch(self, request, pk):
     author = self.get_object(pk)
     serializer = AuthorSerializer(author, data=request.data, partial=True)
     if not serializer.is_valid():
         return Response(serializer.errors, status=400)
     
     serializer.update(author, request.data)
     return Response(serializer.data, status=200)
Exemplo n.º 4
0
    def put(self, request, authorId):
        try:
            reqed_author = Author.objects.get(pk=authorId)
        except:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if request.user.id != authorId:
            return Response(status=status.HTTP_403_FORBIDDEN)

        serializer = AuthorSerializer(reqed_author, data=request.data)

        if serializer.is_valid():
            serializer.save()
            from_one_author = True
            return render(request, self.template_name, {'from_one_author':from_one_author, 'user_be_viewed':reqed_author})
        return Response(status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
def author_list(request):
    if request.method == 'GET':
        categories = Author.objects.all()
        serializer = AuthorSerializer(categories, many=True)

        return Response(serializer.data)
    # return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        request_body = json.loads(request.body)

        serializer = AuthorSerializer(data=request_body)
        if serializer.is_valid():  # Validating data from clients
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors,
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    def post(self, request, author_pk=None, format=None):
        # ensure user is authenticated
        if (not request.user.is_authenticated()):
            return Response({'message':'Not authenticated'}, status=status.HTTP_401_UNAUTHORIZED)
        try:
            author = Author.objects.get(id=author_pk)
        except Author.DoesNotExist as e:
            return Response({"message":"Author does not exist"}, status=status.HTTP_404_NOT_FOUND)

        if request.user == author.user:
            print "1"
            serializer = AuthorSerializer(author, data=request.data)
            print "2"
            if serializer.is_valid():
                print "DEBUG : API - views.py - AuthorDetail"
                serializer.save()
                return Response(serializer.data, status=status.HTTP_200_OK)
            else:
                return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        else:
           return Response({"message":"only this author can make changes to their profile"},status=status.HTTP_403_FORBIDDEN)