def updatePost(postJson):
    post = Post.objects.filter(guid=postJson['guid'])
    if len(post) > 0:
        post = post.first()
        if 'title' in postJson:
            post.title = postJson['title']

        if 'description' in postJson:
            post.description = postJson['description']

        if 'content-type' in postJson:
            post.content_type = postJson['content-type']

        if 'content' in postJson:
            post.content = postJson['content']

        if 'visibility' in postJson:
            post.visibility = postJson['visibility']

        post.save()
    else:
        if 'author' in postJson:
            authorJson = postJson['author']
            if authorJson['host'] == LOCAL_HOST:
                author = Author.objects.filter(uuid=authorJson['id'])
                if len(author) > 0:
                    author = author.first()
                else:
                    raise Exception("invalid author")
            else:
                raise Exception("invalid author")
        else:
            raise Exception("invalid author")

        post = Post.objects.create(title=postJson['title'],
                                   description=postJson['description'],
                                   content_type=postJson['content-type'],
                                   content=postJson['content'],
                                   guid=postJson['guid'],
                                   visibility=postJson['visibility'],
                                   author=author,
                                   publication_date=parser.parse(
                                       postJson['pubDate']))
    if 'comments' in postJson:

        # easiest way to keep comments updated is by deleting exsting comments and adding the new ones
        Comment.removeCommentsForPost(post)

        comments = postJson['comments']
        for comment in comments:
            authorJson = comment['author']
            comment_id = comment['guid']

            commentFilter = Comment.objects.filter(guid=comment_id)
            if len(commentFilter) == 0:
                # no comment exists with the id, add it
                author = Author.objects.filter(uuid=authorJson['id'])

                if len(author) > 0:
                    author = author.first()
                else:
                    author = author_utils.createRemoteUser(
                        displayName=authorJson['displayname'],
                        host=authorJson['host'],
                        uuid=authorJson['id'])
                comment_text = comment['comment']
                Comment.objects.create(guid=comment_id,
                                       comment=comment_text,
                                       pubDate=parser.parse(
                                           comment['pubDate']),
                                       author=author,
                                       post=post)

    if 'categories' in postJson:

        #remove all previous categories and add the new ones
        PostCategory.removeAllCategoryForPost(post)

        categories = postJson['categories']
        for category in categories:
            PostCategory.addCategoryToPost(post, category)

    return get_post_json(post)
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)
Exemplo n.º 3
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 updatePost(postJson):
    post = Post.objects.filter(guid=postJson['guid'])
    if len(post) > 0:
        post = post.first()
        if 'title' in postJson:
            post.title = postJson['title']

        if 'description' in postJson:
            post.description = postJson['description']

        if 'content-type' in postJson:
            post.content_type = postJson['content-type']

        if 'content' in postJson:
            post.content = postJson['content']

        if 'visibility' in postJson:
            post.visibility = postJson['visibility']

        post.save()
    else:
        if 'author' in postJson:
            authorJson = postJson['author']
            if authorJson['host'] == LOCAL_HOST:
                author = Author.objects.filter(uuid=authorJson['id'])
                if len(author) > 0:
                    author = author.first()
                else:
                    raise Exception("invalid author")
            else:
                raise Exception("invalid author")
        else:
            raise Exception("invalid author")

        post = Post.objects.create(title=postJson['title'],
                                   description=postJson['description'],
                                   content_type=postJson['content-type'],
                                   content=postJson['content'],
                                   guid=postJson['guid'],
                                   visibility=postJson['visibility'],
                                   author=author,
                                   publication_date=parser.parse(postJson['pubDate']))
    if 'comments' in postJson:

        # easiest way to keep comments updated is by deleting exsting comments and adding the new ones
        Comment.removeCommentsForPost(post)

        comments = postJson['comments']
        for comment in comments:
            authorJson = comment['author']
            comment_id = comment['guid']

            commentFilter = Comment.objects.filter(guid=comment_id)
            if len(commentFilter) == 0:
                # no comment exists with the id, add it
                author = Author.objects.filter(uuid=authorJson['id'])

                if len(author) > 0:
                    author = author.first()
                else:
                    author = author_utils.createRemoteUser(displayName=authorJson['displayname'],
                                                         host=authorJson['host'],
                                                         uuid=authorJson['id'])
                comment_text = comment['comment']
                Comment.objects.create(guid=comment_id,
                                       comment=comment_text,
                                       pubDate=parser.parse(comment['pubDate']),
                                       author=author,
                                       post=post)

    if 'categories' in postJson:

        #remove all previous categories and add the new ones
        PostCategory.removeAllCategoryForPost(post)

        categories = postJson['categories']
        for category in categories:
            PostCategory.addCategoryToPost(post, category)

    return get_post_json(post)