Esempio n. 1
0
    def post(self, request, format=None):
        # Validate the request body
        serializer = FriendRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        author = serializer.validated_data['author']
        friend = serializer.validated_data['friend']

        if author.isForeign() and friend.isForeign():
            return Response({"error": "We aren't a matchmaking service for foreign authors. Try OKcupid?"},
                            status=status.HTTP_400_BAD_REQUEST)

        # If someone requsets an unfriend this is either a
        # cancellation of a friend request, or termination
        # of a friend relationship, so we have to remove from
        # the author from the friends list of friends if it exists
        # to terminate an existing friend relationship, not leave
        # a hanging friend request from the friend immediately
        # after termination of their relationship.
        if (friend in author.friends.all()):
            author.friends.remove(friend)
        if (author in friend.friends.all()):
            friend.friends.remove(author)

        return Response()
Esempio n. 2
0
    def post(self, request, format=None):
        # Validate the request body
        serializer = FriendRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        author = serializer.validated_data['author']
        friend = serializer.validated_data['friend']
        
        if author.isForeign() and friend.isForeign():
            return Response({"error": "We aren't a matchmaking service for foreign authors. Try OKcupid?"},
                            status=status.HTTP_400_BAD_REQUEST)
        try:
            if friend.isForeign():
                # Echo request to foreign Author's Node
                self.echo_request(author, friend)
        except Exception as e:
            print(str(e))

        if friend not in author.friends.all():
            author.friends.add(friend)

        if (friend not in author.follows.all()):
            author.follows.add(friend)

        return Response()
Esempio n. 3
0
 def post(self, request):
     serializer = FriendRequestSerializer(data=request.data)
     if serializer.is_valid():
         if request.user.username != serializer.data['from_user']:
             return Response(serializer.data,
                             status=status.HTTP_401_UNAUTHORIZED)
         from_user = get_object_or_404(
             UserProfile, user__username=serializer.data['from_user'])
         to_user = get_object_or_404(
             UserProfile, user__username=serializer.data['to_user'])
         if to_user in from_user.friends.all() or from_user == to_user:
             return Response(serializer.data, status=status.HTTP_200_OK)
         try:
             friend_request = FriendRequest.objects.get(from_user=from_user,
                                                        to_user=to_user)
         except FriendRequest.DoesNotExist:
             try:
                 friend_request = FriendRequest.objects.get(
                     from_user=to_user, to_user=from_user)
             except FriendRequest.DoesNotExist:
                 friend_request = FriendRequest(from_user=from_user,
                                                to_user=to_user)
                 friend_request.save()
         serializer_out = FriendRequestSerializer(friend_request)
         return Response(serializer_out.data,
                         status=status.HTTP_201_CREATED)
Esempio n. 4
0
    def perform_create(
        self, serializer: serializers.FriendRequestSerializer
    ) -> None:
        """Saves FriendRequest if serializer doesn't raise errors"""

        try:
            serializer.save()
        except ModelValidationError as error:
            raise ValidationError(error.message)
Esempio n. 5
0
    def post(self, request, format=None):
        # Validate the request body
        serializer = FriendRequestSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        author = serializer.validated_data['author']
        friend = serializer.validated_data['friend']

        if author.isForeign() and friend.isForeign():
            return Response({"error": "We aren't a matchmaking service for foreign authors. Try OKcupid?"},
                            status=status.HTTP_400_BAD_REQUEST)

        if (friend in author.follows.all()):
            author.follows.remove(friend)

        return Response()
Esempio n. 6
0
 def post(self, request, format=None):
     serializer = FriendRequestSerializer(data=request.data)
     if serializer.is_valid():
         if request.user.username != serializer.data['to_user']:
             return Response({"Error": "Permission denied"},
                             status=status.HTTP_401_UNAUTHORIZED)
         from_user = get_object_or_404(
             UserProfile, user__username=serializer.data['from_user'])
         to_user = get_object_or_404(
             UserProfile, user__username=serializer.data['to_user'])
         friend_request = get_object_or_404(FriendRequest,
                                            from_user=from_user,
                                            to_user=to_user)
         if serializer.data['action'] == "accept":
             from_user.last_change = timezone.now()
             to_user.last_change = timezone.now()
             from_user.save()
             to_user.save()
             friend_request.accept()
         elif serializer.data['action'] == "refuse":
             friend_request.refuse()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
Esempio n. 7
0
 def get(self, request, format=None):
     queryset = self.get_queryset()
     serializer = FriendRequestSerializer(queryset, many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)