コード例 #1
0
 def follow(self, request, pk):
     if FriendshipService.has_followed(request.user.id, int(pk)):
         return Response({
             'success': True,
             'duplicate': True,
         },
                         status=status.HTTP_201_CREATED)
     serializer = FriendshipSerializerForCreate(
         data={
             'from_user_id': request.user.id,
             'to_user_id': pk,
         })
     if not serializer.is_valid():
         return Response({
             'success': False,
             'errors': serializer.errors,
         },
                         status=status.HTTP_400_BAD_REQUEST)
     instance = serializer.save()
     return Response(
         FollowingSerializer(instance, context={
             'request': request
         }).data,
         status=status.HTTP_201_CREATED,
     )
コード例 #2
0
ファイル: views.py プロジェクト: wendyfly/django-twitter
    def follow(self, request, pk):
        # 特殊判断重复 follow 的情况(比如前端猛点好多少次 follow)
        # 静默处理,不报错,因为这类重复操作因为网络延迟的原因会比较多,没必要当做错误处理
        to_follow_user = self.get_object()
        if FriendshipService.has_followed(request.user.id, to_follow_user.id):
            return Response(
                {
                    'success': False,
                    'message': 'Please check input',
                    'errors': [{
                        'pk': f'You has followed user with id={pk}'
                    }],
                },
                status=status.HTTP_400_BAD_REQUEST)

        serializer = FriendshipSerializerForCreate(
            data={
                'from_user_id': request.user.id,
                'to_user_id': pk,
            })
        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)
        instance = serializer.save()
        return Response(
            FollowingSerializer(instance, context={
                'request': request
            }).data,
            status=status.HTTP_201_CREATED,
        )
コード例 #3
0
    def follow(self, request, pk):
        # /api/friendships/<pk>/follow/
        # get_object() 会通过上面定义的query_set id=pk取user 取不到会抛404
        # follow_user = self.get_object()
        serializer = FriendshipSerializerForCreate(
            data={
                'from_user_id': request.user.id,
                'to_user_id': pk,
            })

        if not serializer.is_valid():
            return Response(
                {
                    "success": False,
                    "message": "Please check input.",
                    "errors": serializer.errors,
                },
                status=status.HTTP_400_BAD_REQUEST)
        instance = serializer.save()
        return Response(
            FollowingSerializer(instance, context={
                'request': request
            }).data,
            status=status.HTTP_201_CREATED,
        )
コード例 #4
0
    def follow(self, request, pk):
        # /api/friendships/<pk>/follow/
        # 特殊判断重复 follow 的情况(比如前端猛点好多少次 follow)
        # 静默处理,不报错,因为这类重复操作因为网络延迟的原因会比较多,没必要当做错误处理

        # get_object 会检测if user with id=pk 是否存在,如果不存在会return 404 异常
        follow_user = self.get_object()

        if FriendshipService.has_followed(request.user.id, int(pk)):
            return Response({
                'success': True,
                'duplicate': True,
            },
                            status=status.HTTP_201_CREATED)
        serializer = FriendshipSerializerForCreate(
            data={
                'from_user_id': request.user.id,
                'to_user_id': pk,
            })
        if not serializer.is_valid():
            return Response({
                'success': False,
                'errors': serializer.errors,
            },
                            status=status.HTTP_400_BAD_REQUEST)
        instance = serializer.save()
        return Response(
            # instead returning success=> true, returning the pk (the request tries to follow the pk) data
            # the return data includes user_id, created_at
            FollowingSerializer(instance, context={
                'request': request
            }).data,
            status=status.HTTP_201_CREATED,
        )
コード例 #5
0
ファイル: views.py プロジェクト: Kat-JS/django-twitter
 def follow(self, request, pk):
     # check if pk exists
     self.get_object()
     # 特殊判断重复 follow 的情况(比如前端猛点好多少次 follow)
     # 静默处理,不报错,因为这类重复操作因为网络延迟的原因会比较多,没必要当做错误处理
     if Friendship.objects.filter(from_user=request.user,
                                  to_user=pk).exists():
         return Response({
             'success': True,
             'duplicate': True,
         },
                         status=status.HTTP_201_CREATED)
     serializer = FriendshipSerializerForCreate(
         data={
             'from_user_id': request.user.id,
             'to_user_id': pk,
         })
     if not serializer.is_valid():
         return Response({
             'success': False,
             'errors': serializer.errors,
         },
                         status=status.HTTP_400_BAD_REQUEST)
     instance = serializer.save()
     return Response(
         {
             'success': True,
             'following': FollowingSerializer(instance).data
         },
         status=status.HTTP_201_CREATED)
コード例 #6
0
 def follow(self, request, pk):
     # egde case check: duplicate follow action (e.g. multiple clicks from frontend)
     # silent processing. No need to report error.
     # because this kind of duplicated operation are mostly because of internet delay,
     if FriendshipService.has_followed(request.user.id, int(pk)):
         return Response(
             {
                 'success': True,
                 'duplicate': True,
                 'error': [{
                     'pk': f'you have followed the user with id={pk}'
                 }],
             },
             status=status.HTTP_400_BAD_REQUEST)
     serializer = FriendshipSerializerForCreate(
         data={
             'from_user_id': request.user.id,
             'to_user_id': pk,
         })
     if not serializer.is_valid():
         return Response({
             'success': False,
             'errors': serializer.errors,
         },
                         status=status.HTTP_400_BAD_REQUEST)
     instance = serializer.save()
     return Response(
         FollowingSerializer(instance, context={
             'request': request
         }).data,
         status=status.HTTP_201_CREATED,
     )
コード例 #7
0
ファイル: views.py プロジェクト: Ruii-Wang/my-twitter
    def follow(self, request, pk):
        # check if user with id=pk exists
        to_follow_user = self.get_object()

        if FriendshipService.has_followed(request.user.id, to_follow_user.id):
            return Response({
                'success': False,
                'message': 'Please check input',
                'errors': [{'pk': f'You has followed user with id={pk}'}],
            }, status=status.HTTP_400_BAD_REQUEST)

        # /api/friendships/<pk>/follow/
        serializer = FriendshipSerializerForCreate(data={
            'from_user_id': request.user.id,
            'to_user_id': to_follow_user.id,
        })
        if not serializer.is_valid():
            return Response({
                'success': False,
                'message': 'Please check input',
                'errors': serializer.errors,
            }, status=status.HTTP_400_BAD_REQUEST)
        instance = serializer.save()
        return Response(
            FollowingSerializer(instance, context={'request': request}).data,
            status=status.HTTP_201_CREATED
        )
コード例 #8
0
ファイル: views.py プロジェクト: garyyz1003/django-twitter
    def follow(self, request, pk):
        # /api/friendships/<pk>/follow

        # get_object会检测是否有pk对应的用户,如果没有就会报错
        # 这里也能起到检测要follow的用户是否存在的效果
        # 若不存在会返回404
        self.get_object()
        serializer = FriendshipSerializerForCreate(data={
            'from_user_id': request.user.id,
            'to_user_id': pk,
        })

        if not serializer.is_valid():
            return Response({
                "success": False,
                "message": "Please check input",
                "errors": serializer.errors,
            }, status=status.HTTP_400_BAD_REQUEST)
        # 此处也可将该serializer存为一个instance
        # instance = serializer.save()
        # 之后在Response中 如此调用:
        # Response(FollowingSerializer(instance).data, status=status.HTTP_201_CREATED)

        # serializer.save()
        # return Response({'success': True}, status=status.HTTP_201_CREATED)
        instance = serializer.save()
        return Response(FollowingSerializer(instance).data, status=status.HTTP_201_CREATED)
コード例 #9
0
 def followings(self, request, pk):
     friendships = Friendship.objects.filter(
         from_user_id=pk).order_by('-created_at')
     serializer = FollowingSerializer(friendships, many=True)
     return Response(
         {'followings': serializer.data},
         status=status.HTTP_200_OK,
     )
コード例 #10
0
 def followings(self, request, pk):
     friendships = Friendship.objects.filter(
         from_user=pk).order_by('-created_at')
     page = self.paginate_queryset(friendships)
     serializer = FollowingSerializer(page,
                                      many=True,
                                      context={'request': request})
     return self.get_paginated_response(serializer.data)
コード例 #11
0
ファイル: views.py プロジェクト: LittleNotch/django-twitter
    def followings(self, request, pk):
        if GateKeeper.is_switch_on('switch_friendship_to_hbase'):
            page = self.paginator.paginate_hbase(HBaseFollowing, (pk,), request)
        else:
            friendships = Friendship.objects.filter(from_user_id=pk).order_by('-created_at')
            page = self.paginate_queryset(friendships)

        serializer = FollowingSerializer(page, many=True, context={'request': request})
        return self.paginator.get_paginated_response(serializer.data)
コード例 #12
0
 def followings(self, request, pk):
     if not pk:
         return Response('Invalid user',HTTP_401_UNAUTHORIZED)
     friendships = Friendship.objects.filter(from_user_id=pk).order_by('-created_at')
     page = self.paginate_queryset(friendships)
     serialzer = FollowingSerializer(
         page,
         many=True,
         context={'request': request},
     )
     return self.get_paginated_response(serialzer.data)
コード例 #13
0
ファイル: views.py プロジェクト: linr1/django-twitter
 def follow(self, request, pk):
     # check if user with id=pk exists
     follow_user = self.get_object()
     serializer = FriendshipSerializerForCreate(data={
         'from_user_id': request.user.id,
         'to_user_id': follow_user.id
     })
     if not serializer.is_valid():
         return Response({
             'success': False,
             'message': 'Please check input.',
             'errors': serializer.errors,
         }, status=status.HTTP_400_BAD_REQUEST)
     instance = serializer.save()
     return Response(
         FollowingSerializer(instance, context={'request': request}).data,
         status=status.HTTP_201_CREATED
     )
コード例 #14
0
ファイル: views.py プロジェクト: Olililili/django-twitter
    def follow(self, request, pk):
        # check if user with id=pk exists
        self.get_object()
        # /api/friendships/<pk>/follow/
        serializer = FriendshipSerializerForCreate(data={
            'from_user_id': request.user.id,
            'to_user_id': pk,
        })
        if not serializer.is_valid():
            return Response({
                "success": False,
                "message": "Please check input.",
                "errors": serializer.errors,
            }, status=400)

        instance = serializer.save()
        FriendshipService.invalidate_following_cache(request.user.id)
        return Response(
            FollowingSerializer(instance, context={'request': request}).data,
            status=status.HTTP_201_CREATED,
        )
コード例 #15
0
ファイル: views.py プロジェクト: PingLu8/django-twitter
    def follow(self, request, pk):
        self.get_object()
        # if follower keep click follow multiple times, return duplicate, no error
        if Friendship.objects.filter(from_user=request.user, to_user=pk).exists():
            return Response({
                'success': True,
                'duplicate': True,
            }, status = status.HTTP_201_CREATED)

        # /api/friendships/<pk>/follow
        serializer = FriendshipSerializerForCreate(data={
            'from_user_id': request.user.id,
            'to_user_id': pk,
        })
        if not serializer.is_valid():
            return Response({
                "success": False,
                "message": "Please check input.",
                "errors": serializer.errors,
            }, status=status.HTTP_400_BAD_REQUEST)
        instance = serializer.save()
        return Response(FollowingSerializer(instance, context={'request': request}).data, status=status.HTTP_201_CREATED)
コード例 #16
0
    def follow(self, request, pk):
        # /api/friendships/<pk>/follow/
        # raise 404 if no user with id=pk
        to_follow_user = self.get_object()

        # 特殊判断重复 follow 的情况(比如前端猛点好多少次 follow)
        # 静默处理,不报错,因为这类重复操作因为网络延迟的原因会比较多,没必要当做错误处理
        # 或者报400
        if FriendshipService.has_followed(request.user.id, to_follow_user.id):
            return Response(
                {
                    'success': False,
                    'errors': [{
                        'pk': f'You have followed user with id={pk}'
                    }],
                },
                status=status.HTTP_400_BAD_REQUEST)

        serializer = FriendshipSerializerForCreate(
            data={
                'from_user_id': request.user.id,
                'to_user_id': to_follow_user.id,
            })
        if not serializer.is_valid():
            return Response({
                'success': False,
                'errors': serializer.errors,
            },
                            status=status.HTTP_400_BAD_REQUEST)
        instance = serializer.save()
        # 手动invalidate cache 或者利用listener自动调用 invalidate_following_cache
        # FriendshipService.invalidate_following_cache(request.user.id)
        return Response(FollowingSerializer(instance,
                                            context={
                                                'request': request
                                            }).data,
                        status=status.HTTP_201_CREATED)
コード例 #17
0
ファイル: views.py プロジェクト: hanyuanh/my-twitter
    def follow(self, request, pk):
        # check if user with id+pk exists
        self.get_object()

        # /api/friendships/<pk>/follow/
        # special case: multiple click on follow because of network delay\
        serializer = FriendshipSerializerForCreate(data={
            'from_user_id': request.user.id,   # current user
            'to_user_id': pk,                  # the user I want to follow
        })
        # Another way to implement:
        # create a public method for the validation response
        # raise_400_if_serializer_is_not_valid(serializer)
        if not serializer.is_valid():
            return Response({
                "success": False,
                "message": "Please check input",
                "errors": serializer.errors,
            }, status=status.HTTP_400_BAD_REQUEST)
        instance = serializer.save()
        return Response(
            FollowingSerializer(instance, context={'request':request}).data, # to know exactly who I follow
            status=status.HTTP_201_CREATED,
        )