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))
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))
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
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
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
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_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))
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))
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))
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