Exemple #1
0
def get_user_route(request, user_id):
    """
    Get the user by their ID.
    """

    db_conn = request["db_conn"]
    user = get_user({"id": user_id}, db_conn)
    current_user = get_current_user(request)
    # Posts if in request params
    # Sets if in request params and allowed
    # Follows if in request params and allowed
    if not user:
        return abort(404)

    data = {}
    data["user"] = deliver_user(user, access="private" if current_user and user["id"] == current_user["id"] else None)

    # TODO-2 SPLITUP create new endpoints for these instead
    if "posts" in request["params"]:
        data["posts"] = [post.deliver() for post in get_posts_facade(db_conn, user_id=user["id"])]
    if "sets" in request["params"] and user["settings"]["view_sets"] == "public":
        u_sets = UserSets.get(db_conn, user_id=user["id"])
        data["sets"] = [set_.deliver() for set_ in u_sets.list_sets(db_conn)]
    if "follows" in request["params"] and user["settings"]["view_follows"] == "public":
        data["follows"] = [follow.deliver() for follow in Follow.list(db_conn, user_id=user["id"])]
    if "avatar" in request["params"]:
        size = int(request["params"]["avatar"])
        data["avatar"] = get_avatar(user["email"], size if size else None)

    return 200, data
def test_get_posts_facade(db_conn, posts_table):
    """
    Expect to get a list of posts, and the instances to match the kinds.
    """
    posts_table.insert(
        [
            {"id": "fghj4567", "user_id": "abcd1234", "topic_id": "wxyz7890", "body": "abcd", "kind": "post"},
            {
                "id": "yuio6789",
                "user_id": "abcd1234",
                "topic_id": "wxyz7890",
                "kind": "vote",
                "replies_to_id": "fghj4567",
            },
        ]
    ).run(db_conn)
    posts = discuss.get_posts_facade(topic_id="wxyz7890")
    assert isinstance(posts[0], Vote) or isinstance(posts[1], Vote)
    assert isinstance(posts[0], Post) and isinstance(posts[1], Post)
Exemple #3
0
def get_user_route(request, user_id):
    """
    Get the user by their ID.
    """

    db_conn = request['db_conn']
    user = get_user({'id': user_id}, db_conn)
    current_user = get_current_user(request)
    # Posts if in request params
    # Sets if in request params and allowed
    # Follows if in request params and allowed
    if not user:
        return abort(404)

    data = {}
    data['user'] = deliver_user(user,
                                access='private' if current_user
                                and user['id'] == current_user['id'] else None)

    # TODO-2 SPLITUP create new endpoints for these instead
    if 'posts' in request['params']:
        data['posts'] = [
            post.deliver()
            for post in get_posts_facade(db_conn, user_id=user['id'])
        ]
    if ('sets' in request['params']
            and user['settings']['view_sets'] == 'public'):
        data['sets'] = [
            set_.deliver()
            for set_ in list_user_sets_entity(user['id'], {}, db_conn)
        ]
    if ('follows' in request['params']
            and user['settings']['view_follows'] == 'public'):
        data['follows'] = [
            deliver_follow(follow)
            for follow in list_follows({'user_id': user['id']}, db_conn)
        ]
    if 'avatar' in request['params']:
        size = int(request['params']['avatar'])
        data['avatar'] = get_avatar(user['email'], size if size else None)

    return 200, data
def test_get_posts_facade(db_conn, posts_table):
    """
    Expect to get a list of posts, and the instances to match the kinds.
    """
    posts_table.insert([{
        'id': 'fghj4567',
        'user_id': 'abcd1234',
        'topic_id': 'wxyz7890',
        'body': 'abcd',
        'kind': 'post',
    }, {
        'id': 'yuio6789',
        'user_id': 'abcd1234',
        'topic_id': 'wxyz7890',
        'kind': 'vote',
        'replies_to_id': 'fghj4567',
    }]).run(db_conn)
    posts = discuss.get_posts_facade(db_conn, topic_id='wxyz7890')
    assert isinstance(posts[0], Vote) or isinstance(posts[1], Vote)
    assert isinstance(posts[0], Post) and isinstance(posts[1], Post)
Exemple #5
0
def get_user_route(request, user_id):
    """
    Get the user by their ID.
    """

    user = User.get(id=user_id)
    current_user = get_current_user(request)
    # Posts if in request params
    # Sets if in request params and allowed
    # Follows if in request params and allowed
    if not user:
        return abort(404)

    data = {}
    data['user'] = user.deliver(access='private'
                                if current_user
                                and user['id'] == current_user['id']
                                else None)

    # TODO-2 SPLITUP create new endpoints for these instead
    if 'posts' in request['params']:
        data['posts'] = [post.deliver() for post in
                         get_posts_facade(user_id=user['id'])]
    if ('sets' in request['params']
            and user['settings']['view_sets'] == 'public'):
        u_sets = UserSets.get(user_id=user['id'])
        data['sets'] = [set_.deliver() for set_ in u_sets.list_sets()]
    if ('follows' in request['params']
            and user['settings']['view_follows'] == 'public'):
        data['follows'] = [follow.deliver() for follow in
                           Follow.list(user_id=user['id'])]
    if 'avatar' in request['params']:
        size = int(request['params']['avatar'])
        data['avatar'] = user.get_avatar(size if size else None)

    return 200, data
Exemple #6
0
def get_user_route(request, user_id):
    """
    Get the user by their ID.
    """

    user = User.get(id=user_id)
    current_user = get_current_user(request)
    # Posts if in request params
    # Sets if in request params and allowed
    # Follows if in request params and allowed
    if not user:
        return abort(404)

    data = {}
    data['user'] = user.deliver(access='private' if current_user
                                and user['id'] == current_user['id'] else None)

    # TODO-2 SPLITUP create new endpoints for these instead
    if 'posts' in request['params']:
        data['posts'] = [
            post.deliver() for post in get_posts_facade(user_id=user['id'])
        ]
    if ('sets' in request['params']
            and user['settings']['view_sets'] == 'public'):
        u_sets = UserSets.get(user_id=user['id'])
        data['sets'] = [set_.deliver() for set_ in u_sets.list_sets()]
    if ('follows' in request['params']
            and user['settings']['view_follows'] == 'public'):
        data['follows'] = [
            follow.deliver() for follow in Follow.list(user_id=user['id'])
        ]
    if 'avatar' in request['params']:
        size = int(request['params']['avatar'])
        data['avatar'] = user.get_avatar(size if size else None)

    return 200, data
Exemple #7
0
def get_posts_route(request, topic_id):
    """
    Get a reverse chronological listing of posts for given topic.
    Includes topic meta data and posts (or proposals or votes).
    Paginates.
    """

    db_conn = request['db_conn']

    # Is the topic valid?
    topic = get_topic({'id': topic_id}, db_conn)
    if not topic:
        return 404, {
            'errors': [{
                'name': 'topic_id',
                'message': c('no_topic'),
            }],
            'ref': 'pgnNbqSP1VUWkOYq8MVGPrSS',
        }

    # Pull the entity
    entity_kind = topic['entity']['kind']
    entity = get_latest_accepted(db_conn, entity_kind, topic['entity']['id'])

    # Pull all kinds of posts
    posts = get_posts_facade(db_conn,
                             limit=request['params'].get('limit') or 10,
                             skip=request['params'].get('skip') or 0,
                             topic_id=topic_id)

    # For proposals, pull up the proposal entity version
    # ...then pull up the previous version
    # ...make a diff between the previous and the proposal entity version
    diffs = {}
    entity_versions = {}
    for post_ in posts:
        if post_['kind'] == 'proposal':
            entity_version = entity_versions[post_['id']] = get_version(
                db_conn, post_['entity_version']['kind'],
                post_['entity_version']['id'])
            previous_version = get_version(db_conn,
                                           post_['entity_version']['kind'],
                                           entity_version['previous_id'])
            if previous_version:
                diffs[post_['id']] = object_diff(previous_version.deliver(),
                                                 entity_version.deliver())

    # TODO-2 SPLITUP create new endpoint for this instead
    users = {}
    for post_ in posts:
        user_id = post_['user_id']
        if user_id not in users:
            user = get_user({'id': user_id}, db_conn)
            if user:
                users[user_id] = {
                    'name': user['name'],
                    'avatar': get_avatar(user['email'], 48),
                }

    # TODO-2 SPLITUP create new endpoints for these instead
    output = {
        'topic': deliver_topic(topic),
        'posts': [p.deliver() for p in posts],
        'entity_versions':
        {p: ev.deliver('view')
         for p, ev in entity_versions.items()},
        # 'diffs': diffs,  TODO-2 this causes a circular dependency
        'users': users,
    }
    if entity:
        output[entity_kind] = entity.deliver()
    return 200, output
Exemple #8
0
def get_posts_route(request, topic_id):
    """
    Get a reverse chronological listing of posts for given topic.
    Includes topic meta data and posts (or proposals or votes).
    Paginates.
    """

    # Is the topic valid?
    topic = Topic.get(id=topic_id)
    if not topic:
        return 404, {
            'errors': [{
                'name': 'topic_id',
                'message': c('no_topic'),
            }],
            'ref': 'pgnNbqSP1VUWkOYq8MVGPrSS',
        }

    # Pull the entity
    entity_kind = topic['entity']['kind']
    entity = get_latest_accepted(entity_kind,
                                 topic['entity']['id'])

    # Pull all kinds of posts
    posts = get_posts_facade(
        limit=request['params'].get('limit') or 10,
        skip=request['params'].get('skip') or 0,
        topic_id=topic_id
    )

    # For proposals, pull up the proposal entity version
    # ...then pull up the previous version
    # ...make a diff between the previous and the proposal entity version
    diffs = {}
    entity_versions = {}
    for post_ in posts:
        if post_['kind'] == 'proposal':
            entity_version = entity_versions[post_['id']] = get_version(
                post_['entity_version']['kind'],
                post_['entity_version']['id']
            )
            previous_version = get_version(
                post_['entity_version']['kind'],
                entity_version['previous_id']
            )
            if previous_version:
                diffs[post_['id']] = object_diff(previous_version.deliver(),
                                                 entity_version.deliver())

    # TODO-2 SPLITUP create new endpoint for this instead
    users = {}
    for post_ in posts:
        user_id = post_['user_id']
        if user_id not in users:
            user = User.get(id=user_id)
            if user:
                users[user_id] = {
                    'name': user['name'],
                    'avatar': user.get_avatar(48)
                }

    # TODO-2 SPLITUP create new endpoints for these instead
    output = {
        'topic': topic.deliver(),
        'posts': [p.deliver() for p in posts],
        'entity_versions': {
            p: ev.deliver()
            for p, ev in entity_versions.items()
        },
        # 'diffs': diffs,  TODO-2 this causes a circular dependency
        'users': users,
    }
    if entity:
        output[entity_kind] = entity.deliver()
    return 200, output