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_info_by_user_id(conn, user_id): LOGGER.warning('fetching user with id: ' + user_id) auth_info = await r.table('auth').get(user_id).run(conn) if auth_info is None: raise ApiNotFound("Not Found: " "No user with id '{}' exists.".format(user_id)) return auth_info
async def fetch_proposal_resource(conn, proposal_id, head_block_num): resource = await r.table('proposals')\ .get_all(proposal_id, index='proposal_id')\ .filter((head_block_num >= r.row['start_block_num']) & (head_block_num < r.row['end_block_num']))\ .map(lambda proposal: proposal.merge({ 'id': proposal['proposal_id'], 'type': proposal['proposal_type'], 'object': proposal['object_id'], 'target': proposal['target_id'] }))\ .map(lambda proposal: (proposal['metadata'] == "").branch( proposal.without('metadata'), proposal ))\ .without( 'start_block_num', 'end_block_num', 'proposal_id', 'proposal_type', 'object_id', 'target_id' )\ .coerce_to('array').run(conn) try: return resource[0] except IndexError: raise ApiNotFound( 'Not Found: No proposal with the id {} exists'.format(proposal_id))
async def get_all_metadata(request, user_id): all_metadata = await metadata_query.fetch_all_metadata( request.app.config.DB_CONN, user_id) if not all_metadata: raise ApiNotFound( "Not Found: No user with the id '{}' exists".format(user_id)) return json(all_metadata)
async def get_metadata(request, user_id, key): value = await metadata_query.fetch_metadata(request.app.config.DB_CONN, user_id, key) if not value: raise ApiNotFound( "Not Found: No key '{}' for a user with the id '{}' exists".format( key, user_id)) return json(value)
async def fetch_info_by_user_name(conn, user_name): auth_info = await r.table('auth').filter(r.row["user_name"] == user_name ).coerce_to('array').run(conn) if not auth_info: raise ApiNotFound("Not Found: " "No user with name '{}' exists.".format(user_name)) LOGGER.warning(auth_info[0]) return auth_info[0]
async def fetch_block_by_num(conn, block_num): try: return await r.table('blocks')\ .get(block_num)\ .merge({ 'id': r.row['block_id'], 'num': r.row['block_num'] })\ .without('block_id', 'block_num').run(conn) except ReqlRuntimeError: raise ApiNotFound( "Not Found: " "No block with the block num '{}' exists.".format(block_num))
async def fetch_block_by_id(conn, block_id): resource = await r.table('blocks')\ .get_all(block_id, index='block_id')\ .merge({ 'id': r.row['block_id'], 'num': r.row['block_num'] })\ .without('block_id', 'block_num')\ .coerce_to('array').run(conn) try: return resource[0] except IndexError: raise ApiNotFound( "Not Found: No block with the id '{}' exists.".format(block_id))
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'], '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) )