Example #1
0
async def fetch_all_user_resources(conn, start, limit):
    """Database query to compile general data on all user's in database."""
    return (await r.table("users").order_by(
        index="name"
    ).slice(start, start + limit).map(lambda user: user.merge({
        "id":
        user["next_id"],
        "name":
        user["name"],
        "email":
        user["email"],
        "ownerOf": {
            "tasks":
            fetch_relationships_by_id("task_owners", user["next_id"], "task_id"
                                      ),
            "roles":
            fetch_relationships_by_id("role_owners", user["next_id"], "role_id"
                                      ),
            "packs":
            fetch_relationships_by_id("pack_owners", user["next_id"], "pack_id"
                                      ),
        },
        "memberOf":
        fetch_relationships_by_id("role_members", user["next_id"], "role_id"),
        "proposals":
        fetch_proposal_ids_by_opener(user["next_id"]),
    })).map(lambda user: (user["manager_id"] != "").branch(
        user.merge({"manager": user["manager_id"]}), user)).map(
            lambda user: (user["metadata"] == "").branch(
                user.without("metadata"), user)).without(
                    "next_id", "manager_id", "start_block_num",
                    "end_block_num").coerce_to("array").run(conn))
async def fetch_all_user_resources(conn, head_block_num, start, limit):
    return (await r.table("users").order_by(index="user_id").filter(
        (head_block_num >= r.row["start_block_num"])
        & (head_block_num < r.row["end_block_num"])).slice(
            start, start + limit).map(lambda user: user.merge({
                "id":
                user["user_id"],
                "subordinates":
                fetch_user_ids_by_manager(user["user_id"], head_block_num),
                "ownerOf":
                r.union(
                    fetch_relationships_by_id("task_owners", user["user_id"],
                                              "task_id", head_block_num),
                    fetch_relationships_by_id("role_owners", user["user_id"],
                                              "role_id", head_block_num),
                ),
                "administratorOf":
                r.union(
                    fetch_relationships_by_id("task_admins", user["user_id"],
                                              "task_id", head_block_num),
                    fetch_relationships_by_id("role_admins", user["user_id"],
                                              "role_id", head_block_num),
                ),
                "memberOf":
                fetch_relationships_by_id("role_members", user["user_id"],
                                          "role_id", head_block_num),
                "proposals":
                fetch_proposal_ids_by_opener(user["user_id"], head_block_num),
            })).map(lambda user: (user["manager_id"] != "").branch(
                user.merge({"manager": user["manager_id"]}), user)).map(
                    lambda user: (user["metadata"] == "").branch(
                        user.without("metadata"), user)).without(
                            "user_id", "manager_id", "start_block_num",
                            "end_block_num").coerce_to("array").run(conn))
Example #3
0
async def fetch_user_resource(conn, user_id, head_block_num):
    resource = (
        await r.table("users")
        .get_all(user_id, index="user_id")
        .filter(
            (head_block_num >= r.row["start_block_num"])
            & (head_block_num < r.row["end_block_num"])
        )
        .merge(
            {
                "id": r.row["user_id"],
                "email": r.db("rbac")
                .table("auth")
                .filter({"user_id": user_id})
                .get_field("email")
                .coerce_to("array")
                .nth(0),
                "subordinates": fetch_user_ids_by_manager(user_id, head_block_num),
                "ownerOf": r.union(
                    fetch_relationships_by_id(
                        "task_owners", user_id, "task_id", head_block_num
                    ),
                    fetch_relationships_by_id(
                        "role_owners", user_id, "role_id", head_block_num
                    ),
                ),
                "administratorOf": r.union(
                    fetch_relationships_by_id(
                        "task_admins", user_id, "task_id", head_block_num
                    ),
                    fetch_relationships_by_id(
                        "role_admins", user_id, "role_id", head_block_num
                    ),
                ),
                "memberOf": fetch_relationships_by_id(
                    "role_members", user_id, "role_id", head_block_num
                ),
                "proposals": fetch_proposal_ids_by_opener(user_id, head_block_num),
            }
        )
        .map(
            lambda user: (user["manager_id"] != "").branch(
                user.merge({"manager": user["manager_id"]}), user
            )
        )
        .map(
            lambda user: (user["metadata"] == "").branch(user.without("metadata"), user)
        )
        .without("user_id", "manager_id", "start_block_num", "end_block_num")
        .coerce_to("array")
        .run(conn)
    )
    try:
        return resource[0]
    except IndexError:
        raise ApiNotFound("Not Found: No user with the id {} exists".format(user_id))
Example #4
0
async def fetch_task_resource(conn, task_id, head_block_num):
    resource = (
        await r.table("tasks")
        .get_all(task_id, index="task_id")
        .filter(
            (head_block_num >= r.row["start_block_num"])
            & (head_block_num < r.row["end_block_num"])
        )
        .merge(
            {
                "id": r.row["task_id"],
                "owners": fetch_relationships(
                    "task_owners", "task_id", task_id, head_block_num
                ),
                "administrators": fetch_relationships(
                    "task_admins", "task_id", task_id, head_block_num
                ),
                "roles": fetch_relationships_by_id(
                    "role_tasks", task_id, "role_id", head_block_num
                ),
                "proposals": fetch_proposal_ids_by_opener(task_id, head_block_num),
            }
        )
        .without("task_id")
        .coerce_to("array")
        .run(conn)
    )
    try:
        return resource[0]
    except IndexError:
        raise ApiNotFound("Not Found: No task with the id {} exists".format(task_id))
Example #5
0
async def fetch_all_task_resources(conn, head_block_num, start, limit):
    resources = (
        await r.table("tasks")
        .order_by(index="task_id")
        .filter(
            (head_block_num >= r.row["start_block_num"])
            & (head_block_num < r.row["end_block_num"])
        )
        .slice(start, start + limit)
        .map(
            lambda task: task.merge(
                {
                    "id": task["task_id"],
                    "owners": fetch_relationships(
                        "task_owners", "task_id", task["task_id"], head_block_num
                    ),
                    "administrators": fetch_relationships(
                        "task_admins", "task_id", task["task_id"], head_block_num
                    ),
                    "roles": fetch_relationships_by_id(
                        "role_tasks", task["task_id"], "role_id", head_block_num
                    ),
                    "proposals": fetch_proposal_ids_by_opener(
                        task["task_id"], head_block_num
                    ),
                }
            )
        )
        .without("task_id")
        .coerce_to("array")
        .run(conn)
    )
    return resources
async def fetch_all_pack_resources(conn, head_block_num, start, limit):
    """Get all pack resources"""
    resources = (await r.table("packs").order_by(index="pack_id").slice(
        start, start + limit).map(lambda pack: pack.merge({
            "id":
            pack["pack_id"],
            "roles":
            fetch_relationships_by_id("role_packs", pack["pack_id"], "role_id",
                                      head_block_num),
        })).without("pack_id").coerce_to("array").run(conn))
    return resources
async def search_packs(conn, search_query, paging):
    """Compiling all search fields for packs into one query."""
    resource = (await packs_search_name(search_query).union(
        packs_search_description(search_query)
    ).distinct().pluck(
        "name", "description",
        "pack_id").order_by("name").map(lambda doc: doc.merge({
            "id":
            doc["pack_id"],
            "roles":
            fetch_relationships_by_id("role_packs", doc["pack_id"], "role_id"),
        }).without("pack_id")).slice(paging[0],
                                     paging[1]).coerce_to("array").run(conn))

    return resource
async def fetch_pack_resource(conn, pack_id, head_block_num):
    """Get a pack resource"""
    resource = (await
                r.table("packs").get_all(pack_id, index="pack_id").merge({
                    "id":
                    r.row["pack_id"],
                    "roles":
                    fetch_relationships_by_id("role_packs", pack_id, "role_id",
                                              head_block_num),
                }).without("pack_id").coerce_to("array").run(conn))
    try:
        return resource[0]
    except IndexError:
        raise ApiNotFound(
            "Not Found: No pack with the id {} exists".format(pack_id))
Example #9
0
async def search_users(conn, search_query, paging):
    """Compiling all search fields for users into one query."""
    resource = (await users_search_name(search_query).union(
        users_search_email(search_query)
    ).distinct().pluck(
        "name", "email", "next_id"
    ).order_by("name").map(lambda doc: doc.merge({
        "id":
        doc["next_id"],
        "memberOf":
        fetch_relationships_by_id("role_members", doc["next_id"], "role_id"),
    }).without("next_id")).slice(paging[0],
                                 paging[1]).coerce_to("array").run(conn))

    return resource
async def fetch_pack_resource(conn, pack_id):
    """Get a pack resource"""
    resource = (await
                r.table("packs").get_all(pack_id, index="pack_id").merge({
                    "id":
                    r.row["pack_id"],
                    "roles":
                    fetch_relationships_by_id("role_packs", pack_id,
                                              "role_id"),
                    "owners":
                    fetch_relationships("pack_owners", "pack_id", pack_id),
                }).without("pack_id").coerce_to("array").run(conn))
    try:
        return resource[0]
    except IndexError:
        raise ApiNotFound("Pack {} doesn't exist.".format(pack_id))
async def fetch_all_task_resources(conn, start, limit):
    """Gets all task resources."""
    resources = (await r.table("tasks").order_by(
        index="task_id"
    ).slice(start, start + limit).map(lambda task: task.merge({
        "id":
        task["task_id"],
        "owners":
        fetch_relationships("task_owners", "task_id", task["task_id"]),
        "administrators":
        fetch_relationships("task_admins", "task_id", task["task_id"]),
        "roles":
        fetch_relationships_by_id("role_tasks", task["task_id"], "role_id"),
        "proposals":
        fetch_proposal_ids_by_opener(task["task_id"]),
    })).without("task_id").coerce_to("array").run(conn))
    return resources
Example #12
0
async def fetch_user_resource(conn, user_id, head_block_num):
    """Database query to get data on an individual user."""
    resource = (
        await r.table("users")
        .get_all(user_id, index="user_id")
        .merge(
            {
                "id": r.row["user_id"],
                "name": r.row["name"],
                "email": r.row["email"],
                "subordinates": fetch_user_ids_by_manager(user_id, head_block_num),
                "ownerOf": {
                    "tasks": fetch_relationships_by_id(
                        "task_owners", user_id, "task_id", head_block_num
                    ),
                    "roles": fetch_relationships_by_id(
                        "role_owners", user_id, "role_id", head_block_num
                    ),
                    "packs": fetch_relationships_by_id(
                        "pack_owners", user_id, "pack_id", head_block_num
                    ),
                },
                "administratorOf": {
                    "tasks": fetch_relationships_by_id(
                        "task_admins", user_id, "task_id", head_block_num
                    ),
                    "roles": fetch_relationships_by_id(
                        "role_admins", user_id, "role_id", head_block_num
                    ),
                },
                "memberOf": fetch_relationships_by_id(
                    "role_members", user_id, "role_id", head_block_num
                ),
                "expired": fetch_expired_roles(user_id),
                "proposals": fetch_proposal_ids_by_opener(user_id, head_block_num),
            }
        )
        .map(
            lambda user: (user["manager_id"] != "").branch(
                user.merge({"manager": user["manager_id"]}), user
            )
        )
        .map(
            lambda user: (user["metadata"] == "").branch(user.without("metadata"), user)
        )
        .without("user_id", "manager_id", "start_block_num", "end_block_num")
        .coerce_to("array")
        .run(conn)
    )
    try:
        return resource[0]
    except IndexError:
        raise ApiNotFound("Not Found: No user with the id {} exists".format(user_id))
Example #13
0
def get_user(conn, next_id):
    """Database query to get data on an individual user."""
    resource = (r.table("users").get_all(next_id, index="next_id").merge({
        "id":
        r.row["next_id"],
        "remote_id":
        r.row["remote_id"],
        "name":
        r.row["name"],
        "email":
        r.row["email"],
        "username":
        r.row["username"],
        "metadata":
        r.row["metadata"],
        "memberOf":
        fetch_relationships_by_id("role_members", next_id, "role_id"),
    }).map(lambda user: (user["manager_id"] != "").branch(
        user.merge({"manager": user["manager_id"]}), user)).without(
            "next_id", "start_block_num",
            "end_block_num").coerce_to("array").run(conn))
    return resource