def getVisibleToAuthor(viewer=None,
                       author=None,
                       time_line=False,
                       localOnly=False):
    resultList = []
    if author is None:
        postList = Post.objects.all()
    else:
        postList = Post.objects.filter(author=author)

    for post in postList:
        if post.isViewable(viewer, post.author):
            # if we are should timeline only, then we need to check whether or not the
            # two are friends
            if post.content_type == Post.MARK_DOWN:
                post.content = markdown.markdown(post.content,
                                                 safe_mode='escape')

            if time_line:
                if (viewer == post.author
                        or FriendRequest.is_friend(viewer, post.author)
                        or FriendRequest.is_following_or_made_request(
                            viewer, post.author)):
                    resultList.append(get_post_json(post))
            else:
                resultList.append(get_post_json(post))

    if localOnly is False:
        if not time_line:
            remote_posts = remote_helper.api_getPublicPost()
            for post in remote_posts:
                if post['content-type'] == Post.MARK_DOWN:
                    post['content_type'] = Post.MARK_DOWN
                    post['content'] = markdown.markdown(post['content'],
                                                        safe_mode='escape')
        else:
            friend_posts = []
            authorID = author.get_uuid() if author is not None else None
            remote_posts = remote_helper.api_getPostByAuthorID(
                viewer, authorID)

            for post in remote_posts:
                if post['content-type'] == Post.MARK_DOWN:
                    post['content_type'] = Post.MARK_DOWN
                    post['content'] = markdown.markdown(post['content'],
                                                        safe_mode='escape')

                author = post['author']
                author = Author.objects.filter(uuid=author['id'])
                if len(author) > 0:
                    author = author.first()
                    if FriendRequest.is_following_or_made_request(
                            author, viewer) or FriendRequest.is_friend(
                                author, viewer):
                        friend_posts.append(post)
            remote_posts = friend_posts

        resultList.extend(remote_posts)

    return resultList
def getVisibleToAuthor(viewer=None, author=None, time_line=False, localOnly=False):
    resultList = []
    if author is None:
        postList = Post.objects.all()
    else:
        postList = Post.objects.filter(author=author)

    for post in postList:
        if post.isViewable(viewer, post.author):
            # if we are should timeline only, then we need to check whether or not the
            # two are friends
            if post.content_type == Post.MARK_DOWN:
                post.content = markdown.markdown(post.content,
                                                 safe_mode='escape')

            if time_line:
                if (viewer == post.author or FriendRequest.is_friend(viewer, post.author) or
                        FriendRequest.is_following_or_made_request(viewer, post.author)):
                    resultList.append(get_post_json(post))
            else:
                resultList.append(get_post_json(post))

    if localOnly is False:
        if not time_line:
            remote_posts = remote_helper.api_getPublicPost()
            for post in remote_posts:
                if post['content-type'] == Post.MARK_DOWN:
                    post['content_type'] = Post.MARK_DOWN
                    post['content'] = markdown.markdown(post['content'],
                                     safe_mode='escape')
        else:
            friend_posts = []
            authorID = author.get_uuid() if author is not None else None
            remote_posts = remote_helper.api_getPostByAuthorID(viewer, authorID)

            for post in remote_posts:
                if post['content-type'] == Post.MARK_DOWN:
                    post['content_type'] = Post.MARK_DOWN
                    post['content'] = markdown.markdown(post['content'],
                                     safe_mode='escape')

                author = post['author']
                author = Author.objects.filter(uuid=author['id'])
                if len(author) > 0:
                    author = author.first()
                    if FriendRequest.is_following_or_made_request(author, viewer) or FriendRequest.is_friend(author, viewer):
                        friend_posts.append(post)
            remote_posts = friend_posts

        resultList.extend(remote_posts)

    return resultList
 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)
    def isViewable(self, viewer, author):

        visibility = self.visibility

        if visibility == Post.PRIVATE:
            return viewer == author
        elif visibility == Post.ANOTHER_AUTHOR:
            post_entry = VisibleToAuthor.objects.filter(
                visibleAuthor=viewer, post=self)
            return post_entry.exists() or viewer == author
        elif visibility == Post.FRIENDS:
            return FriendRequest.is_friend(viewer, author) or viewer == author
        elif visibility == Post.FOAF:
            friendOfFriends = []
            friends = FriendRequest.get_friends(author)
            for friend in friends:
                friendOfFriends += FriendRequest.get_friends(friend)
            return viewer in friendOfFriends or viewer == author or FriendRequest.is_friend(viewer, author)
        elif visibility == Post.SERVERONLY:
            return viewer.host == author.host or viewer == author
        else:
            # Assuming that the visibility type is public
            return True
def is_friend(request, user_id1, user_id2):
    """Return whether the provided two users are friends.

    This responds with a JSON of the following content:

    {
        "query": "friends",
        "authors": [
            "9de17f29c12e8f97bcbbd34cc908f1baba40658e",
            "8d919f29c12e8f97bcbbd34cc908f19ab9496989"
        ],
        # or NO
        "friends":"YES"
    }
    """
    if request.method == 'GET':
        try:
            response = {
                'query': 'friends',
                'authors': [
                    user_id1,
                    user_id2
                ],
                'friends': 'NO'
            }

            author1 = Author.objects.filter(uuid=user_id1)
            author2 = Author.objects.filter(uuid=user_id2)

            if len(author1) > 0 and len(author2) > 0:
                # We're only expecting one author
                author1 = author1[0]
                author2 = author2[0]

                status = FriendRequest.is_friend(author1, author2)
                if status:
                    response['friends'] = 'YES'

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            else:
                return HttpResponse(status=404)
        except Exception as e:
            return HttpResponse(e.message,
                                content_type='text/plain',
                                status=500)
    else:
        return HttpResponse(status=405)
Exemple #7
0
    def isViewable(self, viewer, author):

        visibility = self.visibility

        if visibility == Post.PRIVATE:
            return viewer == author
        elif visibility == Post.ANOTHER_AUTHOR:
            post_entry = VisibleToAuthor.objects.filter(visibleAuthor=viewer,
                                                        post=self)
            return post_entry.exists() or viewer == author
        elif visibility == Post.FRIENDS:
            return FriendRequest.is_friend(viewer, author) or viewer == author
        elif visibility == Post.FOAF:
            friendOfFriends = []
            friends = FriendRequest.get_friends(author)
            for friend in friends:
                friendOfFriends += FriendRequest.get_friends(friend)
            return viewer in friendOfFriends or viewer == author or FriendRequest.is_friend(
                viewer, author)
        elif visibility == Post.SERVERONLY:
            return viewer.host == author.host or viewer == author
        else:
            # Assuming that the visibility type is public
            return True
Exemple #8
0
def is_friend(request, user_id1, user_id2):
    """Return whether the provided two users are friends.

    This responds with a JSON of the following content:

    {
        "query": "friends",
        "authors": [
            "9de17f29c12e8f97bcbbd34cc908f1baba40658e",
            "8d919f29c12e8f97bcbbd34cc908f19ab9496989"
        ],
        # or NO
        "friends":"YES"
    }
    """
    if request.method == 'GET':
        try:
            response = {
                'query': 'friends',
                'authors': [user_id1, user_id2],
                'friends': 'NO'
            }

            author1 = Author.objects.filter(uuid=user_id1)
            author2 = Author.objects.filter(uuid=user_id2)

            if len(author1) > 0 and len(author2) > 0:
                # We're only expecting one author
                author1 = author1[0]
                author2 = author2[0]

                status = FriendRequest.is_friend(author1, author2)
                if status:
                    response['friends'] = 'YES'

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            else:
                return HttpResponse(status=404)
        except Exception as e:
            return HttpResponse(e.message,
                                content_type='text/plain',
                                status=500)
    else:
        return HttpResponse(status=405)
Exemple #9
0
def foafResponse(request, user_id):
    """
    This responds with the following JSON:
    {
        "query": "friends",
        "author": "9de17f29c12e8f97bcbbd34cc908f1baba40658e",
        "friends": [
            "7deee0684811f22b384ccb5991b2ca7e78abacde",
            "11c3783f15f7ade03430303573098f0d4d20797b",
        ]
    }
    """
    if request.method == 'POST':
        try:
            #author = Author.objects.filter(uuid=user_id)
            request_data = json.loads(request.body)
            author = request_data["author"]
            if len(author[0]) > 0:
                #uuid = request_data['author']
                friends = []

                for i in request_data["authors"]:
                    if FriendRequest.is_friend(i, author):
                        friends.append(i)

                friends = list(set(friends) & set(request_data["authors"]))
                response = {
                    'query': 'friends',
                    'author': author,
                    'friends': friends
                }

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            else:
                return HttpResponse(status=404)
        except Exception as e:
            return HttpResponse(e.message,
                                content_type='text/plain',
                                status=500)
    else:
        return HttpResponse(status=405)
def foafResponse(request, user_id):
    """
    This responds with the following JSON:
    {
        "query": "friends",
        "author": "9de17f29c12e8f97bcbbd34cc908f1baba40658e",
        "friends": [
            "7deee0684811f22b384ccb5991b2ca7e78abacde",
            "11c3783f15f7ade03430303573098f0d4d20797b",
        ]
    }
    """
    if request.method == 'POST':
        try:
            #author = Author.objects.filter(uuid=user_id)
            request_data= json.loads(request.body)
            author = request_data["author"]
            if len(author[0])>0:
                #uuid = request_data['author']
                friends=[]

                for i in request_data["authors"]:
                    if FriendRequest.is_friend(i, author):
                        friends.append(i)

                friends = list(set(friends) & set(request_data["authors"]))
                response = {'query': 'friends','author': author,'friends': friends}

                return HttpResponse(json.dumps(response),
                                    content_type='application/json',
                                    status=200)
            else:
                return HttpResponse(status=404)
        except Exception as e:
            return HttpResponse(e.message,
                                content_type='text/plain',
                                status=500)
    else:
        return HttpResponse(status=405)
def search(request):
    """Returns a list of authors.

    The returned list of authors contains their username, first_name,
    and last_name.
    """
    context = RequestContext(request)

    if request.method == 'POST':
        search_value = request.POST['searchValue']

        author_info = []

        if search_value == '*' or search_value is None:
            users = User.objects.all()
        else:
            # query all values containing search results
            users = User.objects.filter(
                Q(username__contains=search_value) & ~Q(username=request.user))

        results = 0
        # setting each author search information
        for user in users:
            results += 1
            try:
                friend = False
                sent = False
                received = False
                sent = False
                follow = False
                sentList = []
                receivedList = []
                rUser = Author.objects.get(user=request.user)
                author = Author.objects.get(user=user)
                friend = FriendRequest.is_friend(rUser, author)
                follow = FriendRequest.is_following(rUser, author)
                sentList = FriendRequest.sent_requests(rUser)
                if sentList:
                    if author in sentList:
                        sent = True
                receivedList = FriendRequest.received_requests(rUser)
                if receivedList:
                    if author in receivedList:
                        received = True
                user_info = {
                    "displayname": author.get_username(),
                    "userName": author.user.username,
                    "userID": author.get_uuid(),
                    "host": author.get_host(),
                    "friend": friend,
                    "sent": sent,
                    "received": received,
                    "follow": follow
                }

                author_info.append(user_info)
            except:
                pass
        print("context")
        context = RequestContext(
            request, {
                'searchValue': search_value,
                'authorInfo': author_info,
                'results': results
            })

    return render_to_response('searchResults.html', context)
def search(request):
    """Returns a list of authors.

    The returned list of authors contains their username, first_name,
    and last_name.
    """
    context = RequestContext(request)

    if request.method == 'POST':
        search_value = request.POST['searchValue']

        author_info = []

        if search_value == '*' or search_value is None:
            users = User.objects.all()
        else:
            # query all values containing search results
            users = User.objects.filter(Q(username__contains=search_value) &
                                        ~Q(username=request.user))

        results = 0
        # setting each author search information
        for user in users:
            results += 1
            try:
                friend = False
                sent = False
                received = False
                sent = False
                follow = False
                sentList = []
                receivedList = []
                rUser = Author.objects.get(user = request.user)
                author = Author.objects.get(user=user)
                friend = FriendRequest.is_friend(rUser, author)
                follow = FriendRequest.is_following(rUser, author)
                sentList = FriendRequest.sent_requests(rUser)
                if sentList:
                    if author in sentList:
                        sent = True
                receivedList = FriendRequest.received_requests(rUser)
                if receivedList:
                    if author in receivedList:
                        received = True
                user_info = {"displayname": author.get_username(),
                             "userName": author.user.username,
                             "userID": author.get_uuid(),
                             "host": author.get_host(),
                             "friend": friend,
                             "sent": sent,
                             "received": received,
                             "follow": follow}

                author_info.append(user_info)
            except:
                pass
        print("context")
        context = RequestContext(request, {'searchValue': search_value,
                                           'authorInfo': author_info,
                                           'results': results})

    return render_to_response('searchResults.html', context)