Example #1
0
async def fetch_role_resource(conn, role_id):
    """Get a role resource by role_id."""
    resource = (
        await r.table("roles")
        .get_all(role_id, index="role_id")
        .merge(
            {
                "id": r.row["role_id"],
                "owners": fetch_relationships("role_owners", "role_id", role_id),
                "administrators": fetch_relationships(
                    "role_admins", "role_id", role_id
                ),
                "members": fetch_relationships("role_members", "role_id", role_id),
                "tasks": fetch_relationships("role_tasks", "role_id", role_id),
                "proposals": fetch_proposal_ids_by_opener(role_id),
                "packs": fetch_relationships("role_packs", "role_id", role_id),
            }
        )
        .without("role_id")
        .coerce_to("array")
        .run(conn)
    )
    try:
        return resource[0]
    except IndexError:
        raise ApiNotFound("Role {} doesn't exist.".format(role_id))
Example #2
0
async def fetch_role_resource(conn, role_id, head_block_num):
    resource = (await
                r.table("roles").get_all(role_id, index="role_id").merge({
                    "id":
                    r.row["role_id"],
                    "owners":
                    fetch_relationships("role_owners", "role_id", role_id,
                                        head_block_num),
                    "administrators":
                    fetch_relationships("role_admins", "role_id", role_id,
                                        head_block_num),
                    "members":
                    fetch_relationships("role_members", "role_id", role_id,
                                        head_block_num),
                    "tasks":
                    fetch_relationships("role_tasks", "role_id", role_id,
                                        head_block_num),
                    "proposals":
                    fetch_proposal_ids_by_opener(role_id, head_block_num),
                    "packs":
                    fetch_relationships("role_packs", "role_id", role_id,
                                        head_block_num),
                }).without("role_id").coerce_to("array").run(conn))
    try:
        return resource[0]
    except IndexError:
        raise ApiNotFound(
            "Not Found: No role with the id {} exists".format(role_id))
Example #3
0
async def search_roles(conn, search_query, paging):
    """Compiling all search fields for roles into one query."""
    resource = (
        await roles_search_name(search_query)
        .union(roles_search_description(search_query))
        .distinct()
        .pluck("name", "description", "role_id")
        .order_by("name")
        .map(
            lambda doc: doc.merge(
                {
                    "id": doc["role_id"],
                    "members": fetch_relationships(
                        "role_members", "role_id", doc["role_id"]
                    ),
                    "owners": fetch_relationships(
                        "role_owners", "role_id", doc["role_id"]
                    ),
                }
            ).without("role_id")
        )
        .slice(paging[0], paging[1])
        .coerce_to("array")
        .run(conn)
    )

    return resource
Example #4
0
async def fetch_all_role_resources(conn, start, limit):
    """Get all role resources."""
    resources = (
        await r.table("roles")
        .order_by(index="role_id")
        .slice(start, start + limit)
        .map(
            lambda role: role.merge(
                {
                    "id": role["role_id"],
                    "owners": fetch_relationships(
                        "role_owners", "role_id", role["role_id"]
                    ),
                    "administrators": fetch_relationships(
                        "role_admins", "role_id", role["role_id"]
                    ),
                    "members": fetch_relationships(
                        "role_members", "role_id", role["role_id"]
                    ),
                    "tasks": fetch_relationships(
                        "role_tasks", "role_id", role["role_id"]
                    ),
                    "proposals": fetch_proposal_ids_by_opener(role["role_id"]),
                    "packs": fetch_relationships(
                        "role_packs", "role_id", role["role_id"]
                    ),
                }
            )
        )
        .without("role_id")
        .coerce_to("array")
        .run(conn)
    )
    return resources
Example #5
0
async def fetch_all_role_resources(conn, head_block_num, start, limit):
    resources = (await r.table("roles").order_by(index="role_id").filter(
        (head_block_num >= r.row["start_block_num"])
        & (head_block_num < r.row["end_block_num"])).slice(
            start, start + limit).map(lambda role: role.merge({
                "id":
                role["role_id"],
                "owners":
                fetch_relationships("role_owners", "role_id", role["role_id"],
                                    head_block_num),
                "administrators":
                fetch_relationships("role_admins", "role_id", role["role_id"],
                                    head_block_num),
                "members":
                fetch_relationships("role_members", "role_id", role["role_id"],
                                    head_block_num),
                "tasks":
                fetch_relationships("role_tasks", "role_id", role["role_id"],
                                    head_block_num),
                "proposals":
                fetch_proposal_ids_by_opener(role["role_id"], head_block_num),
                "packs":
                fetch_relationships("role_packs", "role_id", role["role_id"],
                                    head_block_num),
            })).without("role_id").coerce_to("array").run(conn))
    return resources
Example #6
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))
async def fetch_recommended_resources(conn, identifier, head_block_num, start,
                                      limit):
    """Fetch recommended roles for a user"""
    resources = (await r.table("roles").outer_join(
        r.table("role_members"),
        lambda a, b: a["role_id"].eq(b["role_id"])).zip().filter(
            lambda c: c.has_fields("identifiers").not_()
            | c["identifiers"].contains(identifier).not_()
        ).map(lambda role: role.merge({
            "id":
            role["role_id"],
            "owners":
            fetch_relationships("role_owners", "role_id", role["role_id"],
                                head_block_num),
            "administrators":
            fetch_relationships("role_admins", "role_id", role["role_id"],
                                head_block_num),
            "members":
            fetch_relationships("role_members", "role_id", role["role_id"],
                                head_block_num),
            "tasks":
            fetch_relationships("role_tasks", "role_id", role["role_id"],
                                head_block_num),
            "proposals":
            fetch_proposal_ids_by_opener(role["role_id"], head_block_num),
            "packs":
            fetch_relationships("role_packs", "role_id", role["role_id"],
                                head_block_num),
        })).slice(start, start +
                  limit).without("role_id", "object_id", "related_id",
                                 "identifiers").coerce_to("array").run(conn))
    return resources
Example #8
0
def get_role(conn, role_id):
    """Get a role resource by role_id."""
    resource = (r.table("roles").get_all(role_id, index="role_id").merge({
        "id":
        r.row["role_id"],
        "owners":
        fetch_relationships("role_owners", "role_id", role_id),
        "administrators":
        fetch_relationships("role_admins", "role_id", role_id),
        "members":
        fetch_relationships("role_members", "role_id", role_id),
        "metadata":
        r.row["metadata"],
    }).without("role_id").coerce_to("array").run(conn))
    return resource
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
async def fetch_all_pack_resources(conn, 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"),
        "owners":
        fetch_relationships("pack_owners", "pack_id", pack["pack_id"]),
    })).without("pack_id").coerce_to("array").run(conn))
    return resources
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))