Пример #1
0
    def follow(self, request, pk):
        # check i fuser with id=pk exists
        # self.get_object()
        # 特殊判断重复 follow 的情况 (比如前端猛点多次follow)
        # 静默处理, 不报错, 因为这类重复操作 因为网络延迟的原因会比较多, 没必要当做错误处理
        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)
        serializer.save()

        return Response({'success': True}, status=status.HTTP_201_CREATED)
Пример #2
0
 def follow(self, request, pk):
     # return 201 with silently processing when user trying to follow the same user multiple times
     # this type of error happens frequently due to network latency
     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)
     serializer.save()
     # invalidate following caches, so that next time when querying it, the request will be forwarded to DB
     # FriendshipService.invalidate_following_cache(request.user.id)
     return Response({'success': True}, status=status.HTTP_201_CREATED)
 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,
     )
Пример #4
0
    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,
        )
Пример #5
0
    def follow(self, request, pk):
        # check if pk exists in user database
        self.get_object()

        # silently handle duplicate follow request
        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)

        serializer.save()

        return Response({'success': True}, status=status.HTTP_201_CREATED)
Пример #6
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,
        )
Пример #7
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,
     )
Пример #8
0
    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
        )
Пример #9
0
 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)
Пример #10
0
    def follow(self, request, pk):
        pk = int(pk)

        if Friendship.objects.filter(from_user=request.user,
                                     to_user=pk).exists():
            return Response({
                'success': True,
                'duplicate': True,
            }, status=200)

        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=400)

        serializer.save()
        return Response({'success': True}, status=201)
Пример #11
0
    def follow(self, request, pk):

        #check if follow user exists
        self.get_object()
        if Friendship.objects.filter(from_user=request.user,to_user=pk):
            return Response({
                "success": True,
                "duplicate": True,
            }, HTTP_201_CREATED)
        if not User.objects.filter(id=pk).exists():
            logging.error(pk)
            logging.error(User.objects.filter(id=pk).exists())
            return Response({
                "success": False,
            }, 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": "Invalid inputs.",
                "error": serializer.errors,
            }, HTTP_400_BAD_REQUEST)
        friendship = serializer.save()
        #populate newsfeed timeline for new friendship
        NewsFeedService.populate_newsfeed_for_friendship(friendship)
        return Response(
            FriendshipSerializerForCreate(friendship).data,
            status=HTTP_201_CREATED
        )
Пример #12
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,
        )
Пример #13
0
    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)
Пример #14
0
 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
     )
Пример #15
0
 def follow(self, request, pk):
     # dup follow e.g. front end click follow many times
     # keep silent network delay, it is not a real error
     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)
     serializer.save()
     #FriendshipService.invalidate_following_cache(request.user.id)
     return Response({'success': True}, status=status.HTTP_201_CREATED)
Пример #16
0
    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,
        )
Пример #17
0
    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)
Пример #18
0
    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)

        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)
        serializer.save()
        return Response({'success': True}, status=status.HTTP_201_CREATED)
Пример #19
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)
Пример #20
0
    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,
        )