Exemple #1
0
    def create(self, request, *args, **kwargs):
        data = {
            'user_id': request.user.id,
            'tweet_id': request.data.get('tweet_id'),
            'content': request.data.get('content'),
        }
        # Here must have 'data=' to assign params to data
        # because the first default param is instance
        serializer = CommentSerializerForCreate(data=data)
        if not serializer.is_valid():
            return Response(
                {
                    'message': 'Please check input',
                    'errors': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)

        # the save() method would trigger create() method in serializer, click
        # into save() to see its implementation.
        comment = serializer.save()
        NotificationService.send_comment_notification(comment)
        return Response(
            CommentSerializer(comment, context={
                'request': request
            }).data,
            status=status.HTTP_201_CREATED,
        )
Exemple #2
0
    def create(self, request, *args, **kwargs):
        data = {
            'user_id': request.user.id,
            'tweet_id': request.data.get('tweet_id'),
            'content': request.data.get('content'),
        }
        # 注意这里必须要加 'data=' 来指定参数是传给 data 的
        # 因为默认的第一个参数是 instance
        serializer = CommentSerializerForCreate(data=data)
        if not serializer.is_valid():
            return Response(
                {
                    'message': 'Please check input',
                    'errors': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)

        # save 方法会触发 serializer 里的 create 方法,点进 save 的具体实现里可以看到
        comment = serializer.save()
        NotificationService.send_comment_notification(comment)
        return Response(
            CommentSerializer(comment, context={
                'request': request
            }).data,
            status=status.HTTP_201_CREATED,
        )
Exemple #3
0
    def create(self, request, *args, **kwargs):
        data = {
            'user_id': request.user.id,
            'tweet_id': request.data.get('tweet_id'),
            'content': request.data.get('content'),
        }

        # validate the input by CommentSerializerForCreate
        serializer = CommentSerializerForCreate(data=data)
        if not serializer.is_valid():
            return Response(
                {
                    'success': False,
                    'message': 'Invalid Comment Input',
                    'error': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)

        # save validated comment
        comment = serializer.save()
        # raise notification for comment creation
        NotificationService.send_comment_notification(comment)
        serializer = CommentSerializer(
            instance=comment,
            context={'request': request},
        )
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Exemple #4
0
    def create(self, request, *args, **kwargs):
        data = {
            'user_id': request.user.id,
            'tweet_id': request.data.get('tweet_id'),
            'content': request.data.get('content')
        }

        serializer = CommentSerializerForCreate(data=data)

        if not serializer.is_valid():
            return Response(
                {
                    'message': 'Please check input',
                    'errors': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)

        comment = serializer.save()
        NotificationService.send_comment_notification(comment)

        return Response(CommentSerializer(comment,
                                          context={
                                              'request': request
                                          }).data,
                        status=status.HTTP_201_CREATED)
Exemple #5
0
    def create(self, request, *args, **kwargs):
        data = {
            "user_id": request.user.id,
            "tweet_id": request.data.get('tweet_id'),
            "content": request.data.get('content'),
        }

        serializer = CommentSerializerForCreate(data=data)

        if not serializer.is_valid():
            return Response(
                {
                    'message': 'Please check input',
                    'errors': serializer.errors,
                },
                status=400)

        comment = serializer.save()
        return Response(CommentSerializer(comment).data, status=201)
Exemple #6
0
 def create(self, request, *args, **kwargs):
     data = {
         'user_id': request.user.id,
         'tweet_id': request.data.get('tweet_id'),
         'content': request.data.get('content'),
     }
     # must 'data=' specify param is to data
     # default first param is instance
     serializer = CommentSerializerForCreate(data=data)
     if not serializer.is_valid():
         return Response({
             'message': 'Please check input',
             'errors': serializer.errors,
         }, status=status.HTTP_400_BAD_REQUEST)
     # save method will trigger create()
     comment = serializer.save()
     NotificationService.send_comment_notification(comment)
     return Response(
         CommentSerializer(comment, context={'request': request}).data,
         status=status.HTTP_201_CREATED
     )
Exemple #7
0
    def create(self, request):
        data = {
            'user_id': request.user.id,
            'tweet_id': request.data.get('tweet_id'),
            'content': request.data.get('content'),
        }
        serialzier = CommentSerializerForCreate(
            data=data,
            context={'request': request},
        )

        if not serialzier.is_valid():
            return Response({
                "success": False,
                "message": "Please check input.",
                "errors": serialzier.errors,
            }, status=HTTP_400_BAD_REQUEST)

        comment = serialzier.save()
        #send out notifications
        NotificationService.send_comment_notification(comment)
        return Response(CommentSerializer(comment, context={'request': request}).data, status = HTTP_201_CREATED)
Exemple #8
0
    def create(self, request, *args, **kwargs):
        # 这里是直接构造一个data dict,里面包含我们create要用到的参数
        # 也可以像之前一样, 把request当成context传进去:
        # serializer = CommentSerializerForCreate(
        #     data=request.data,
        #     context={'request': request},
        # )
        data = {
            'user_id': request.user.id,
            'tweet_id': request.data.get('tweet_id'),
            'content': request.data.get('content'),
        }
        # 注意这里必须要加 'data=' 来指定参数是传给 data 的
        # 因为默认的第一个参数是 instance
        serializer = CommentSerializerForCreate(data=data)
        # serializer = CommentSerializerForCreate(
        #     data=request.data,
        #     context={'request': request},
        # )
        if not serializer.is_valid():
            return Response(
                {
                    'success': False,
                    'message': 'Please check input.',
                    'errors': serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)

        # save 方法会触发 serializer 里的 create 方法,点进 save 的具体实现里可以看到
        comment = serializer.save()
        # 发送 comment notification
        NotificationService.send_comment_notification(comment)
        return Response(
            CommentSerializer(comment, context={
                'request': request
            }).data,
            status=status.HTTP_201_CREATED,
        )
Exemple #9
0
    def create(self, request, *args, **kwargs):
        data = {
            'user_id': request.user.id,
            'tweet_id': request.data.get('tweet_id'),
            'content': request.data.get('content'),
        }

        # note that here we need to add 'data=' to specify that those params are to be passed to data
        # because by default the first parameter is meant to be instance
        serializer = CommentSerializerForCreate(data=data)
        if not serializer.is_valid():
            return Response({
                'message': 'Please check input',
                'errors': serializer.errors,
            }, status=status.HTTP_400_BAD_REQUEST)

        # save will trigger the create method inside serializer
        comment = serializer.save()
        NotificationService.send_comment_notification(comment)
        return Response(
            CommentSerializer(comment, context={'request': request}).data,
            status=status.HTTP_201_CREATED,
        )