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)
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)
def test_retrieve_one_author(self): res = self.client.get(detail_url(self.author2.id)) serializer = AuthorSerializer(self.author2) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_retrieve_all_authors(self): res = self.client.get(AUTHOR_URL) authors = Author.objects.all() serializer = AuthorSerializer(authors, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def get_author_list(request): authors = Author.objects.all() last_name = request.query_params.get('last_name', None) if last_name is not None: authors = authors.filter(last_name__icontains=last_name) first_name = request.query_params.get('first_name', None) if first_name is not None: authors = authors.filter(last_name__icontains=first_name) authors_serializer = AuthorSerializer(authors, many=True) return Response(authors_serializer.data)
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)
def get_author(author): author_serializer = AuthorSerializer(author) return Response(author_serializer.data)
def test_author_serializer(self): author = mixer.blend('book.Author', name="author") serializer = AuthorSerializer(author) assert serializer.data == {"name": "author"}