def post(self, request, pk=None): print("POSTED") serializer_context = { 'request': request, } serializer = CommentSerializer(data=request.data, context=serializer_context) print("P",serializer.is_valid()) if serializer.is_valid() == True: print("sucessful backend") instance = serializer.save() messages.success(request._request, 'Success') return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_get_all_comments(self): response = client.get( f'/api/author/testauthor/posts/{self.post1.id}/comments/') comments = Comment.objects.filter(post=self.post1) serializer = CommentSerializer(comments, many=True) self.assertEqual(response.data['items'], serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def create(self, request, author, post): try: postObj = Post.objects.get(id=post) comment = Comment.objects.create(**request.data, post=postObj) serializer = CommentSerializer(comment) except Post.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) return Response(serializer.data, status=status.HTTP_201_CREATED)
def test_create_comment(self): response = client.post( f'/api/author/testauthor/posts/{self.post.id}/comments/', data=json.dumps(self.payload), content_type='application/json') comment = Comment.objects.get(post=self.post) self.assertTrue(comment) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, CommentSerializer(comment).data)
def post(self, request, pk=None): serializer_context = { 'request': request, } serializer = CommentSerializer(data=request.data, context=serializer_context) print("Comment serializer - validity: ",serializer.is_valid()) if serializer.is_valid() == True: print("sucessful backend") # Compare the userID from request with userID from provided token userIDFromRequest = serializer.validated_data['userID'] userIDFromToken = request.user if(userIDFromRequest == userIDFromToken): instance = serializer.save() messages.success(request._request, 'Success') return Response(serializer.data, status=status.HTTP_201_CREATED) else: raise ValidationError("Sorry, you are trying to comment as another user which is forbidden..") else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def retrieve(self, request, author, post): try: queryset = Comment.objects.filter(post=post) paginator = Paginator(queryset, request.GET.get('size', self.page_size)) page_obj = paginator.page(request.GET.get('page', 1)) serializer = CommentSerializer(page_obj, many=True) if len(serializer.data) == 0: return Response(status=status.HTTP_204_NO_CONTENT) except Post.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) except EmptyPage: return Response(status=status.HTTP_204_NO_CONTENT) return Response({'count': len(queryset), 'items': serializer.data})
def comment_list(request): #Lista los comentarios o crea uno nuevo --> equivale al CommentViewSet if request.method == 'GET': comments = Comment.objects.all() serializer = CommentSerializer(comments, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = CommentSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) #Codigo para created return JSONResponse(serializer.errors, status=400) #Bad request
def comment_detail(request, pk): #Crear, borrar, o actualizar un comentario try: comment = Comment.objects.get(pk=pk) except Comment.DoesNotExist: return HttpResponse(status=404) #Not found :D if request.method == 'GET': serializer = CommentSerializer(comment) return JSONResponse(json.dumps(serializer.data)) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = CommentSerializer(comment, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) #Bad request elif request.method == 'DELETE': comment.delete() return HttpResponse(status=204) #No content