def put(self, request, pk, id, format=None): comment = self.get_object(id) serializer = CommentSerializer(comment, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, format=None): serializer = CommentSerializer(data=request.data) if serializer.is_valid(): obj = Comment.objects.filter(id=serializer.data['id']) obj.delete() return Response(stastus=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): user = request.user serializer = CommentSerializer(data=request.data, context={'user': user}) if (serializer.is_valid()): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): data = JSONParser().parse(request) data["user"] = request.user.id serializer = CommentSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def comment_add(self, request, **kwargs): serializer = CommentSerializer(context={"host": request.get_host()}, data=request.data) if not serializer.is_valid(): return Response(data=serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) post = get_object_or_404(Post, id=kwargs.get('post_id')) self.check_object_permissions(request, post.group) serializer.save(owner=request.user, post=post) return JsonResponse(data=serializer.data, status=201, safe=False)
def comments(request, id): if request.method == 'GET': commentss = Comment.objects.filter(post__id=id).order_by('-pub_date') serializer = CommentSerializer(commentss, many=True) return Response(serializer.data) elif request.method == 'POST': request.data['post'] = id serializer = CommentSerializer(data=request.data) print(serializer) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def comment_edit(self, request, **kwargs): serializer = CommentSerializer(context={"host": request.get_host()}, data=request.data) if not serializer.is_valid(): return Response(data=serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) post = get_object_or_404(Post, id=kwargs.get('post_id')) comment = get_object_or_404(Comment, id=kwargs.get('comment_id'), post=post) self.check_object_permissions(request, comment) serializer.update(comment, serializer.validated_data) return JsonResponse(data=serializer.data, status=200, safe=False)
def comment_list(request, post): if request.method == 'GET': comments = Comment.objects.filter(post=post) comments_serializer = CommentSerializer(comments, many=True) return JsonResponse(comments_serializer.data, safe=False) # 'safe=False' for objects serialization elif request.method == 'POST': comment_data = JSONParser().parse(request) comment_data['post']=post comment_serializer = CommentSerializer(data=comment_data) if comment_serializer.is_valid(): comment_serializer.save() return JsonResponse(comment_serializer.data, status=status.HTTP_201_CREATED) return JsonResponse(comment_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_comment(self, request, **kwargs): try: post = Post.objects.get(**kwargs) except Post.DoesNotExist: return response404('Post') self.check_object_permissions(request=request, obj=post.group) serializer = CommentSerializer(data=request.data, partial=True) if not serializer.is_valid(): return response406({ **serializer.errors, 'message': 'Błąd walidacji' }) serializer.save(post=post, owner=request.user) return response200({ **serializer.data, 'message': 'Pomyślnie utworzono komentarz' })
def comments(self, request, pk): post = self.get_object() if request.method == "GET": comment_qs = Comment.objects.filter(post=post) serializer = CommentSerializer(comment_qs, many=True) return Response(serializer.data) else: comment = request.data comment["user"] = request.user.id comment["post"] = post.id serializer = CommentSerializer(data=comment) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): post_id = request.data.get("post_id") if post_id is None: return Response( { "success": False, "message": "Required Param post_id is missing" }, status=400) try: post = Post.objects.get(pk=post_id) except Post.DoesNotExist: return Response( { "success": False, "message": "Invalid post_id param provided" }, status=400) data = { "commenter": request.user.id, "post": post.id, "comment": request.data.get("comment") } comment = CommentSerializer(data=data) if comment.is_valid(): instance = comment.save() serializer = CommentSerializer(instance, many=False) msg = "{} added a new comment".format(request.user.username) if int(post.user.id) != int(request.user.id): NotificationFunctions.create_notification( post=post.id, user=post.user.id, auther=request.user.id, message=msg, notification_type=Notification.POST_COMMENT) return Response({ "success": True, "message": "Comment Saved.", "data": serializer.data }) return Response({ "success": False, "message": comment.errors }, status=400)
def update_comment(self, request, **kwargs): try: comment = Comment.objects.get(**kwargs) except Comment.DoesNotExist: return response404('Comment') serializer = CommentSerializer(comment, data=request.data, partial=True) self.check_object_permissions(request=request, obj=comment) if not serializer.is_valid(): return response406({ **serializer.errors, 'message': 'Błąd walidacji' }) serializer.save() return response200({ **serializer.data, 'message': 'Pomyślnie zaktualizowano komentarz' })
def comment(self, request, comment_pk, pk): try: comment = Comment.objects.get(id=comment_pk) self.check_object_permissions(self.request, comment) except ObjectDoesNotExist: raise NotFound(detail="Comment does not exist") if request.method == "GET": serializer = CommentSerializer(comment) return Response(serializer.data) if request.method == "DELETE": comment.delete() return Response(status=status.HTTP_204_NO_CONTENT) if request.method == "PATCH": serializer = CommentSerializer(comment, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)