def request_friendship(request):
    """Sends a friend request."""
    context = RequestContext(request)

    if request.method == 'POST':
        if request.user.is_authenticated():
            friend_requestee = request.POST['friend_requestee']
            friend_user = User.objects.get(username=friend_requestee)
            friend = Author.objects.get(user=friend_user)
            requester = Author.objects.get(user=request.user)

            if '__' not in friend.user.username:
                # local author
                status = FriendRequest.make_request(requester, friend)
            else:
                print 'HERE'
                # remote author
                status = post_friend_request(requester, friend)
                if status:
                    status = FriendRequest.make_request(requester, friend)

            if status:
                messages.info(request, 'Friend request sent successfully')
            else:
                messages.error(request, 'Error sending a friend request')

            return redirect('/author/friends')
        else:
            _render_error('login.html', 'Please log in.', context)
def request_friendship(request):
    """Sends a friend request."""
    context = RequestContext(request)

    if request.method == 'POST':
        if request.user.is_authenticated():
            friend_requestee = request.POST['friend_requestee']
            friend_user = User.objects.get(username=friend_requestee)
            friend = Author.objects.get(user=friend_user)
            requester = Author.objects.get(user=request.user)

            if '__' not in friend.user.username:
                # local author
                status = FriendRequest.make_request(requester, friend)
            else:
                print 'HERE'
                # remote author
                status = post_friend_request(requester, friend)
                if status:
                    status = FriendRequest.make_request(requester, friend)

            if status:
                messages.info(request, 'Friend request sent successfully')
            else:
                messages.error(request, 'Error sending a friend request')

            return redirect('/author/friends')
        else:
            _render_error('login.html', 'Please log in.', context)
 def test_is_following(self):
     """Testing if a user is following another"""
     c = Client()
     user1 = User.objects.get(username="******")
     user2 = User.objects.get(username="******")
     author1 = Author.objects.get(user=user1)
     author2 = Author.objects.get(user=user2)
     response = FriendRequest.make_request(author1, author2)
     response2 = FriendRequest.is_following(author1, author2)
     response3 = FriendRequest.is_following(author2, author1)
     self.assertEquals(True, response)
     self.assertEquals(True, response2)
     self.assertEquals(False, response3)
 def test_is_following(self):
     """Testing if a user is following another"""
     c = Client()
     user1 = User.objects.get(username="******")
     user2 = User.objects.get(username="******")
     author1 = Author.objects.get(user=user1)
     author2 = Author.objects.get(user=user2)
     response = FriendRequest.make_request(author1, author2)
     response2 = FriendRequest.is_following(author1, author2)
     response3 = FriendRequest.is_following(author2, author1)
     self.assertEquals(True, response)
     self.assertEquals(True, response2)
     self.assertEquals(False, response3)
    def test__request_friend_list(self):
        """Testing if successfully returns a list of friend requests"""
        c = Client()
        c.login(username='******', password='******')
        user1 = User.objects.get(username="******")
        user2 = User.objects.get(username="******")
        author1 = Author.objects.get(user=user1)
        author2 = Author.objects.get(user=user2)
        response = FriendRequest.make_request(author2, author1)

        url = '/author/' + str(user1.id) + '/FriendRequests'
        response = c.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.context['requestList'], ['myuser2'])
    def test__request_friend_list(self):
        """Testing if successfully returns a list of friend requests"""
        c = Client()
        c.login(username='******', password='******')
        user1 = User.objects.get(username="******")
        user2 = User.objects.get(username="******")
        author1 = Author.objects.get(user=user1)
        author2 = Author.objects.get(user=user2)
        response = FriendRequest.make_request(author2, author1)

        url = '/author/' + str(user1.id) + '/FriendRequests'
        response = c.get(url)
        self.assertEquals(response.status_code, 200)
        self.assertEquals(response.context['requestList'], ['myuser2'])
 def test_are_friends(self):
     """Testing if correctly identifies two authors as friends or not"""
     user1 = User.objects.get(username="******")
     user2 = User.objects.get(username="******")
     author1 = Author.objects.get(user=user1)
     author2 = Author.objects.get(user=user2)
     response = FriendRequest.make_request(author1, author2)
     response2 = FriendRequest.is_friend(author1, author2)
     response3 = FriendRequest.accept_request(author2, author1)
     response4 = FriendRequest.is_friend(author1, author2)
     response5 = FriendRequest.is_friend(author2, author1)
     self.assertEquals(True, response)
     self.assertEquals(False, response2)
     self.assertEquals(True, response3)
     self.assertEquals(True, response4)
     self.assertEquals(True, response5)
 def test_are_friends(self):
     """Testing if correctly identifies two authors as friends or not"""
     user1 = User.objects.get(username="******")
     user2 = User.objects.get(username="******")
     author1 = Author.objects.get(user=user1)
     author2 = Author.objects.get(user=user2)
     response = FriendRequest.make_request(author1, author2)
     response2 = FriendRequest.is_friend(author1, author2)
     response3 = FriendRequest.accept_request(author2, author1)
     response4 = FriendRequest.is_friend(author1, author2)
     response5 = FriendRequest.is_friend(author2, author1)
     self.assertEquals(True, response)
     self.assertEquals(False, response2)
     self.assertEquals(True, response3)
     self.assertEquals(True, response4)
     self.assertEquals(True, response5)
Exemple #9
0
def friend_request(request):
    """Makes a friend request.

    This expects a POST request with the following JSON as the content:

    {
        "query": "friendrequest",
        "author": {
            "id": "8d919f29c12e8f97bcbbd34cc908f19ab9496989",
            "host": "http://127.0.0.1:5454/",
            "displayname": "Greg"
        },
        "friend": {
            "id":"9de17f29c12e8f97bcbbd34cc908f1baba40658e",
            "host":"http://127.0.0.1:5454/",
            "displayname":"Lara",
            "url":"http://127.0.0.1:5454/author/
                   9de17f29c12e8f97bcbbd34cc908f1baba40658e"
        }
    }

    If all is well, this responds with a 200 OK.
    """
    if request.method == 'POST':
        try:
            try:
                request_data = json.loads(request.body)

                uuid_author = request_data['author']['id']
                uuid_friend = request_data['friend']['id']
                host_author = request_data['author']['host']
                host_friend = request_data['friend']['host']
                display_author = request_data['author']['displayname']
            except Exception as e:
                return HttpResponse('Bad JSON format: %s' % e.message,
                                    content_type='text/plain',
                                    status=400)

            author = Author.objects.filter(uuid=uuid_author)
            friend = Author.objects.filter(uuid=uuid_friend)

            if len(friend) == 0:
                return HttpResponse('The friend uuid must exist on our server',
                                    content_type='text/plain',
                                    status=400)

            if len(author) == 0:
                # We need to create this author, since it doesn't currently
                # exist.
                try:
                    author = author_utils.createRemoteUser(
                        displayName=display_author,
                        host=host_author,
                        uuid=uuid_author)
                except Exception as e:
                    return HttpResponse(e.message,
                                        content_type='text/plain',
                                        status=500)

            author = author[0]
            friend = friend[0]

            if friend.host != LOCAL_HOST:
                return HttpResponse('The friend must be a user on our server',
                                    content_type='text/plain',
                                    status=400)

            if FriendRequest.make_request(author, friend):
                return HttpResponse(status=200)
            else:
                return HttpResponse(
                    'Could not make friend request for '
                    'author %s at %s and friend %s at %s. '
                    'The friend request has already been '
                    'made.' %
                    (uuid_author, host_author, uuid_friend, host_friend),
                    content_type='text/plain',
                    status=500)
        except Exception as e:
            return HttpResponse(e.message,
                                content_type='text/plain',
                                status=500)
    else:
        return HttpResponse(status=405)
def friend_request(request):
    """Makes a friend request.

    This expects a POST request with the following JSON as the content:

    {
        "query": "friendrequest",
        "author": {
            "id": "8d919f29c12e8f97bcbbd34cc908f19ab9496989",
            "host": "http://127.0.0.1:5454/",
            "displayname": "Greg"
        },
        "friend": {
            "id":"9de17f29c12e8f97bcbbd34cc908f1baba40658e",
            "host":"http://127.0.0.1:5454/",
            "displayname":"Lara",
            "url":"http://127.0.0.1:5454/author/
                   9de17f29c12e8f97bcbbd34cc908f1baba40658e"
        }
    }

    If all is well, this responds with a 200 OK.
    """
    if request.method == 'POST':
        try:
            try:
                request_data = json.loads(request.body)

                uuid_author = request_data['author']['id']
                uuid_friend = request_data['friend']['id']
                host_author = request_data['author']['host']
                host_friend = request_data['friend']['host']
                display_author = request_data['author']['displayname']
            except Exception as e:
                return HttpResponse('Bad JSON format: %s' % e.message,
                                    content_type='text/plain',
                                    status=400)

            author = Author.objects.filter(uuid=uuid_author)
            friend = Author.objects.filter(uuid=uuid_friend)

            if len(friend) == 0:
                return HttpResponse('The friend uuid must exist on our server',
                                    content_type='text/plain',
                                    status=400)

            if len(author) == 0:
                # We need to create this author, since it doesn't currently
                # exist.
                try:
                    author = author_utils.createRemoteUser(displayName=display_author,
                                                           host=host_author,
                                                           uuid=uuid_author)
                except Exception as e:
                    return HttpResponse(e.message,
                                        content_type='text/plain',
                                        status=500)

            author = author[0]
            friend = friend[0]

            if friend.host != LOCAL_HOST:
                return HttpResponse('The friend must be a user on our server',
                                    content_type='text/plain',
                                    status=400)

            if FriendRequest.make_request(author, friend):
                return HttpResponse(status=200)
            else:
                return HttpResponse('Could not make friend request for '
                                    'author %s at %s and friend %s at %s. '
                                    'The friend request has already been '
                                    'made.'
                                    % (uuid_author, host_author,
                                       uuid_friend, host_friend),
                                    content_type='text/plain',
                                    status=500)
        except Exception as e:
            return HttpResponse(e.message,
                                content_type='text/plain',
                                status=500)
    else:
        return HttpResponse(status=405)