Ejemplo n.º 1
0
def test_list_kind(db_conn, follows_table):
    """
    Expect to get follows by kind.
    """

    follows_table.insert([{
        'user_id': 'JFldl93k',
        'created': r.now(),
        'modified': r.now(),
        'entity': {
            'kind': 'card',
            'id': 'JFlsjFm',
        },
    }, {
        'user_id': 'abcd1234',
        'created': r.now(),
        'modified': r.now(),
        'entity': {
            'kind': 'card',
            'id': 'JFlsjFm',
        },
    }, {
        'user_id': 'abcd1234',
        'created': r.now(),
        'modified': r.now(),
        'entity': {
            'kind': 'unit',
            'id': 'u39Fdjf0',
        },
    }]).run(db_conn)

    assert len(Follow.list(db_conn, kind='card')) == 2
    assert len(Follow.list(db_conn, kind='unit')) == 1
Ejemplo n.º 2
0
def test_list_kind(db_conn, follows_table):
    """
    Expect to get follows by kind.
    """

    follows_table.insert([{
        'user_id': 'JFldl93k',
        'created': r.now(),
        'modified': r.now(),
        'entity': {
            'kind': 'card',
            'id': 'JFlsjFm',
        },
    }, {
        'user_id': 'abcd1234',
        'created': r.now(),
        'modified': r.now(),
        'entity': {
            'kind': 'card',
            'id': 'JFlsjFm',
        },
    }, {
        'user_id': 'abcd1234',
        'created': r.now(),
        'modified': r.now(),
        'entity': {
            'kind': 'unit',
            'id': 'u39Fdjf0',
        },
    }]).run(db_conn)

    assert len(Follow.list(kind='card')) == 2
    assert len(Follow.list(kind='unit')) == 1
Ejemplo n.º 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":
        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
Ejemplo n.º 4
0
def follow_route(request):
    """
    Follow a card, unit, or set.
    """

    # TODO-3 simplify this method. does some of this belong in the model?

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    follow_data = dict(**request['params'])
    follow_data['user_id'] = current_user['id']

    follow = Follow(follow_data)
    errors = follow.validate()
    if errors:
        return 400, {
            'errors': errors,
            'ref': '4Qn9oWVWiGKvXSONQKHSy1T6'
        }

    # Ensure the entity exists   TODO-3 should this be a model validation?
    if follow['entity']['kind'] == 'topic':
        entity = Topic.get(id=follow['entity']['id'])
    else:
        entity = get_latest_accepted(follow['entity']['kind'],
                                     follow['entity']['id'])
    if not entity:
        return abort(404)

    # Ensure we don't already follow   TODO-3 should this be a model validation?
    prev = Follow.list(user_id=current_user['id'],
                       entity_id=follow_data['entity']['id'])
    if prev:
        return abort(409)

    follow, errors = follow.save()
    if errors:
        return 400, {
            'errors': errors,
            'ref': 'gKU6wgTItxpKyDs0eAlonCmi',
        }

    return 200, {'follow': follow.deliver(access='private')}
Ejemplo n.º 5
0
def follow_route(request):
    """
    Follow a card, unit, or set.
    """

    # TODO-3 simplify this method. does some of this belong in the model?

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    follow_data = dict(**request['params'])
    follow_data['user_id'] = current_user['id']

    follow = Follow(follow_data)
    errors = follow.validate()
    if errors:
        return 400, {'errors': errors, 'ref': '4Qn9oWVWiGKvXSONQKHSy1T6'}

    # Ensure the entity exists   TODO-3 should this be a model validation?
    if follow['entity']['kind'] == 'topic':
        entity = Topic.get(id=follow['entity']['id'])
    else:
        entity = get_latest_accepted(follow['entity']['kind'],
                                     follow['entity']['id'])
    if not entity:
        return abort(404)

    # Ensure we don't already follow   TODO-3 should this be a model validation?
    prev = Follow.list(user_id=current_user['id'],
                       entity_id=follow_data['entity']['id'])
    if prev:
        return abort(409)

    follow, errors = follow.save()
    if errors:
        return 400, {
            'errors': errors,
            'ref': 'gKU6wgTItxpKyDs0eAlonCmi',
        }

    return 200, {'follow': follow.deliver(access='private')}
Ejemplo n.º 6
0
def get_follows_route(request):
    """
    Get a list of the users follows.
    """

    db_conn = request["db_conn"]

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    follows = Follow.list(db_conn, user_id=current_user["id"], **request["params"])

    output = {"follows": [follow.deliver(access="private") for follow in follows]}

    # TODO-3 SPLITUP should this be a different endpoint?
    if "entities" in request["params"]:
        entities = flush_entities(db_conn, [follow["entity"] for follow in follows])
        output["entities"] = [entity.deliver() if entity else None for entity in entities]

    return 200, output
Ejemplo n.º 7
0
def get_follows_route(request):
    """
    Get a list of the users follows.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    follows = Follow.list(user_id=current_user['id'], **request['params'])

    output = {
        'follows': [follow.deliver(access='private') for follow in follows]
    }

    # TODO-3 SPLITUP should this be a different endpoint?
    if 'entities' in request['params']:
        entities = flush_entities(follow['entity'] for follow in follows)
        output['entities'] = [entity.deliver() if entity else None
                              for entity in entities]

    return 200, output
Ejemplo n.º 8
0
def get_follows_route(request):
    """
    Get a list of the users follows.
    """

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    follows = Follow.list(user_id=current_user['id'], **request['params'])

    output = {
        'follows': [follow.deliver(access='private') for follow in follows]
    }

    # TODO-3 SPLITUP should this be a different endpoint?
    if 'entities' in request['params']:
        entities = flush_entities(follow['entity'] for follow in follows)
        output['entities'] = [
            entity.deliver() if entity else None for entity in entities
        ]

    return 200, output
Ejemplo n.º 9
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
Ejemplo n.º 10
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