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)
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)
def test_serializes_author_with_first_and_last_name(self): author = Author.objects.create(first_name='Terry', last_name='Pratchett') serialized = AuthorSerializer(author).data self.assertEqual(author.id, serialized['id']) self.assertEqual(author.first_name, serialized['first_name']) self.assertEqual(author.last_name, serialized['last_name'])
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)
def test_get_all_authors(self): # get API response response = client.get(reverse('get_all_authors')) # get data from db authors = Author.objects.all() serializer = AuthorSerializer(authors, many=True) self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_authors(self): resp = self.authorizedClient.get(reverse('api:authors-list'), format='json') authors = Author.objects.all() serializer = AuthorSerializer(authors, many=True) self.assertEqual(resp.data['results'], serializer.data)
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})
def author_detail(request, author_id): try: author = Author.objects.get(id=author_id) except Author.DoesNotExist as e: return Response({'message': str(e)}, status=400) if request.method == 'GET': serializer = AuthorSerializer(author) return Response(serializer.data)
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)
def test_update_author(self): # Attempt to modify name of author new_name = "Kazi Nazrul Islam" resp = self.authorizedClient.patch(reverse( 'api:authors-detail', kwargs={'pk': self.author.id}), {"name": new_name}, format='json') self.assertEqual(resp.status_code, status.HTTP_200_OK) self.author = Author.objects.get(pk=self.author.id) serializer = AuthorSerializer(self.author) self.assertEqual(resp.data, serializer.data)
def test_create_author(self): resp = self.authorizedClient.post( reverse('api:authors-list'), { "name": "Kazi Nazrul", "country": "BD", "occupation": "Novelist", "education": "School", "biography": "bio graphy about kazinazrul" }, format="json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) created_author = Author.objects.get(pk=resp.data['id']) serializer = AuthorSerializer(created_author) self.assertEqual(resp.data, serializer.data)
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 test_serializes_author_with_only_last_name(self): author = Author.objects.create(last_name='Homer') serialized = AuthorSerializer(author).data self.assertEqual(author.id, serialized['id']) self.assertEqual(None, serialized['first_name']) self.assertEqual(author.last_name, serialized['last_name'])
def get(self, request, pk): author = self.get_object(pk) serializer = AuthorSerializer(author) return Response(serializer.data)
def get(self, request): objs = Author.objects.all() data = AuthorSerializer(objs, many=True).data return Response(data)