def create(self, request, *args, **kwargs): confess = Confession.objects.filter(id=kwargs.pop('id')) if not confess.exists() or confess.count() != 1: raise ValidationError("This is not valid confess!") serializer = CommentSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save(related=confess.first(), is_parent=1) return Response(serializer.data)
def post(self, request, pk): user = 1 data = {'text': self.request.data, 'article': pk, 'author': user} serializer = CommentSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) print(serializer.errors) return Response(status=status.HTTP_400_BAD_REQUEST)
def comments_view(request): if request.method == 'GET': countryId = request.GET.get('countryId') comments = Comment.objects.filter(country=countryId) serializer = CommentSerializer(comments, many=True) return JsonResponse(serializer.data, safe=False) if request.method == 'POST': data = JSONParser().parse(request) serializer = CommentSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def post_user_comment(request): """ post -> dodaje komentar za odredenog usera :param request: :return: """ if request.method == "POST": # print(User.objects.get(username="******").id) # comment = Comment.objects.create(user_id_id= request.POST.get('user_id'), # comment_body=request.POST.get('comment_body')) # comment.save() # return Response(comment, status=status.HTTP_201_CREATED) # 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 post(self, request, *args, **kwargs): """ 用户点评资源(资源、案例及资讯) """ form = CommentInputForm(request.data) if not form.is_valid(): return Response({'Detail': form.errors}, status=status.HTTP_400_BAD_REQUEST) cld = form.cleaned_data is_valid, error_message = self.is_request_data_valid(request, **cld) if not is_valid: return Response({'Detail': error_message}, status=status.HTTP_400_BAD_REQUEST) serializer = CommentSerializer(data=cld, request=request) if not serializer.is_valid(): return Response({'Detail': serializer.errors}, status=status.HTTP_400_BAD_REQUEST) try: serializer.save() except Exception as e: return Response({'Detail': e.args}, status=status.HTTP_400_BAD_REQUEST) result = self.score_action(request) if isinstance(result, Exception): return Response({'Detail': result.args}, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.data, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs): serializer = CommentSerializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)