def put(self, request, pk): comment = self.get_object(pk) 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 post(self,request,format=None): serializer = CommentSerializer(data=request.data,context= {'user_id':request.user.id}) if serializer.is_valid(): serializer_2 = CommentSerializer(serializer.save()) r.hset("COMMENTS",serializer_2.data["id"],json.dumps(serializer_2.data)) return Response(json.dumps(serializer.data), status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = CommentSerializer(data=request.data) 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 comments(self, request, pk=None): self.pagination_class.page_size = 1 comments = Comment.objects.filter(pk=pk) page = self.paginate_queryset(comments) if page is not None: serializer = CommentSerializer(page, many=True) return self.get_paginated_response(serializer.data) serializer = CommentSerializer(comments, many=True) return Response(serializer.data)
def comments(self, request, pk): post = self.get_object() if request.method == 'GET': serializer = CommentSerializer(post.comment_set.all(), many=True) return Response(serializer.data, status=status.HTTP_200_OK) if request.method == 'POST': comment = post.comment_set.create(user=request.user, body=request.data['body']) return Response(CommentSerializer(comment).data, status=status.HTTP_201_CREATED)
def comment_submit(request): if request.method == 'POST': user_id = request.POST.get('user_id') content = request.POST.get('comment') article_id = request.POST.get('article_id') reply_user_id = request.POST.get('reply_user_id', None) supercomment_id = request.POST.get('supercomment_id', None) try: user_id = int(user_id) article_id = int(article_id) if supercomment_id is not None and supercomment_id != '': supercomment_id = int(supercomment_id) if reply_user_id is not None and reply_user_id != '': reply_user_id = int(reply_user_id) except Exception as e: resp = {'statecode': 400, 'detail': 'Bad Request Parameters'} else: comment = Comment.objects.submit_comment(user_id, article_id, content, supercomment_id, reply_user_id) serializers = CommentSerializer(comment) print(serializers.data) resp = {'statecode': 200, 'comment': serializers.data} else: resp = {'statecode': 400, 'detail': 'Bad Request Method'} return HttpResponse(json.dumps(resp), content_type="application/json")
class PostDetailsSerializer(serializers.ModelSerializer): class Meta: model = Post read_only_fields = "author", "liked_by", "created_at", "updated_at", fields = "__all__" comment_set = CommentSerializer(many=True)
def get(self, request): try: data = Comment.objects.all() s_data = CommentSerializer(data, many=True).data return Response(data={ 'status': True, "result": s_data }, status=status.HTTP_200_OK) except Exception as e: return Response(data={ 'status': False, "message": str(e) }, status=status.HTTP_400_BAD_REQUEST)
def comment_post_form(request): """ With POST method comment form submit. In this view the recieved data updated and pass to serializer Inputs: data from POST => post,text,user.id that get from request.user Outputs: Serializer.data [not impostant becuase in the js if request is success alert with notificatin """ if request.method == 'GET': pass elif request.method == 'POST': user = User.objects.get(username=request.user) print(request.data) param = { 'user': user.pk, 'post': int(request.data['post']), 'text': request.data['text'], } serializer = CommentSerializer(data=param) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data) return JsonResponse(serializer.errors, status=400)
def comment_post_api(request, pk): try: post = get_object_or_404(NewsPost, id=pk) except NewsPost.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': comments = Comment.objects.filter(post=post) serializer = CommentSerializer(comments, many=True) return Response(serializer.data, status=status.HTTP_200_OK) if request.method == 'POST': serializer = CommentSerializer(data=request.data, context={'request': request}) if serializer.is_valid(): serializer.save(post_id=pk, user=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: id = request.data['id'] num = Post.objects.get(id=int(id)) comment = request.data['comment'] data = Comment.objects.create(post=num, body=comment) s_data = CommentSerializer(data, many=True).data return Response(data={ 'status': True, "result": s_data }, status=status.HTTP_200_OK) except Exception as e: return Response(data={ 'status': False, "message": str(e) }, status=status.HTTP_400_BAD_REQUEST)
def comments(self, request, pk=None): post = self.get_object() comments = post.comments.all() serializer = CommentSerializer(comments, many=True) return Response(serializer.data)
def get(self, requset): comments = Comment.objects.all() serializer = CommentSerializer(comments, many=True) return Response(serializer.data)
def get(self, request, pk): comment = self.get_object(pk) serializer= CommentSerializer(comment) return Response(serializer.data)
def get(self,request,pk,format=None): comment = Comment.objects.filter(post_id=pk) serializer = CommentSerializer(comment,many=True) return Response(json.dumps(serializer.data))