예제 #1
0
 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)
예제 #2
0
 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)
예제 #3
0
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)
예제 #4
0
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)
예제 #5
0
파일: views.py 프로젝트: zhang1229/BYWebApp
    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)
예제 #6
0
 def create(self, request, *args, **kwargs):
     serializer = CommentSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data)