Esempio n. 1
0
 def create(self, request, *args, **kwargs):
     serializer = TweetCreateSerializer(data=request.data,
                                        context={'request': request})
     if not serializer.is_valid():
         return Response(
             {
                 'success': False,
                 'message': "Please check input",
                 'errors': serializer.errors,
             },
             status=400)
     tweet = serializer.save()
     return Response(TweetSerializer(tweet).data, status=201)
Esempio n. 2
0
 def create(self, request):
     serializer = TweetCreateSerializer(data=request.data,
                                        context={'request': request})
     if not serializer.is_valid():
         return Response(
             {
                 "success": False,
                 "massage": "Please check input.",
                 "errors": serializer.errors,
             },
             status=400)
     # save will call create method in TweetSerializerForCreate
     tweet = serializer.save()
     return Response(TweetSerializer(tweet).data, status=201)
Esempio n. 3
0
 def create(self, request):
     serializer = TweetCreateSerializer(data=request.data,
                                        context={'request': request})
     if not serializer.is_valid():
         return Response(
             {
                 'success': False,
                 'message': 'Please check input',
                 'errors': serializer.errors,
             },
             status=400)
     tweet = serializer.save()
     NewsFeedService.fanout_to_followers(tweet)
     return Response(TweetSerializer(tweet).data, status=201)
Esempio n. 4
0
 def create(self, request, *args, **kwargs):
     """
     重载 create 方法,因为需要默认用当前登录用户作为 tweet.user
     """
     serializer = TweetCreateSerializer(
         data=request.data,
         context={'request': request},
     )
     # use default validator, like min max length
     if not serializer.is_valid():
         return Response(
             {
                 'success': False,
                 'message': "Please check input",
                 'errors': serializer.errors,
             },
             status=400)
     tweet = serializer.save()
     NewsFeedService.fanout_to_followers(tweet)
     return Response(TweetSerializer(tweet).data, status=201)
Esempio n. 5
0
    def create(self, request):
        serializer = TweetCreateSerializer(
            data = request.data,
            context = {'request': request},
        )
        if not serializer.is_valid():
            return Response({
                'success': False,
                'message': "Please check the input",
                'error': serializer.errors,
            }, status=status.HTTP_400_BAD_REQUEST)

        tweet = serializer.save()
        NewsFeedService().fanout_to_followers(tweet)
        serializer = TweetSerializer(
            instance=tweet,
            context={'request': request},
        )
        return Response({
            'success': True,
            'tweet': serializer.data,
        }, status=status.HTTP_201_CREATED)