Example #1
0
def update_author(author, request):
    author_data = JSONParser().parse(request)
    author_data = converts_keys(author_data, case='snake')
    author_serializer = AuthorSerializer(author, data=author_data)
    if author_serializer.is_valid():
        author_serializer.save()
        return Response(author_serializer.data)
    return Response(author_serializer.errors,
                    status=status.HTTP_400_BAD_REQUEST)
Example #2
0
def create_author(request):
    author_data = JSONParser().parse(request)
    # convert keys from camelCase to snake_case
    author_data = converts_keys(author_data, case='snake')
    author_serializer = AuthorSerializer(data=author_data)
    if author_serializer.is_valid():
        author_serializer.save()
        return Response(author_serializer.data, status=status.HTTP_201_CREATED)
    return Response(author_serializer.errors,
                    status=status.HTTP_400_BAD_REQUEST)
Example #3
0
def author_list(request):
    """
    List all authors, or create a new author.
    """
    if request.method == 'GET':
        authors = Author.objects.all()
        serializer = AuthorSerializer(authors, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = AuthorSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)