Beispiel #1
0
    def get_serializer(self, *args, **kwargs):
        # For swagger
        if 'ppk' not in self.kwargs:
            res = CommentSerializer(data=self.request.data)
            res.is_valid(raise_exception=True)
            return res

        # Generate POST data
        if self.request.method == 'POST':
            comment_data = {}
            comment_data.update(self.request.data)
            comment_data.update({
                'object_id':
                self.kwargs['ppk'],
                'content_type':
                ContentType.objects.get_for_model(Post).id,
                'writer':
                Fbuser.objects.get(pk=self.request.user.id).id
            })

            serializer = CommentSerializer(data=comment_data)
            serializer.is_valid(raise_exception=True)
            return serializer

        return super().get_serializer(*args, **kwargs)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
 def post(self,request):
     commentSerializer = CommentSerializer(data = request.data)
     if(commentSerializer.is_valid()):
         print(commentSerializer.validated_data)
         # Comment.objects.create(**commentSerializer.validated_data)
         CommentSerializer.create(**commentSerializer.validated_data)
         return Response(status=status.HTTP_202_ACCEPTED)
     else:
         return Response(status=status.HTTP_400_BAD_REQUEST)
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
0
    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)
Beispiel #8
0
 def create(self, request, *args, **kwargs):
     serializer = CommentSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data)