From 8eedaab544d5bd1ab667bdd88d055b671bbee1d0 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Mon, 18 Oct 2021 11:52:55 +0300 Subject: wqflask: resource_manager: Add `add_extra_resource_metadata` method --- wqflask/wqflask/resource_manager.py | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/wqflask/wqflask/resource_manager.py b/wqflask/wqflask/resource_manager.py index 03b45822..16bab250 100644 --- a/wqflask/wqflask/resource_manager.py +++ b/wqflask/wqflask/resource_manager.py @@ -113,3 +113,42 @@ def get_user_access_roles(conn: redis.Redis, access_role["metadata"].append( DataRole(roles.get("metadata"))) return {k: max(v) for k, v in access_role.items()} + + +def add_extra_resource_metadata(conn: redis.Redis, resource: Dict) -> Dict: + """If resource['owner_id'] exists, add metadata about that user. Also, +if the resource contains group masks, add the group name into the +resource dict. Note that resource['owner_id'] and the group masks are +unique identifiers so they aren't human readable names. + + Args: + - conn: A redis connection with the responses decoded. + - resource: A dict containing details(metadata) about a + given resource. + + Returns: + An embellished dictionary with the human readable names of the + group masks and the owner id if it was set. + + """ + # Embellish the resource information with owner details if the + # owner is set + if (owner_id := resource.get("owner_id", "none").lower()) == "none": + resource["owner_id"] = None + resource["owner_details"] = None + else: + user_details = json.loads(conn.hget("users", owner_id)) + resource["owner_details"] = { + "email_address": user_details.get("email_address"), + "full_name": user_details.get("full_name"), + "organization": user_details.get("organization"), + } + + # Embellish the resources information with the group name if the + # group masks are present + if groups := resource.get('group_masks', {}): + for group_id in groups.keys(): + resource['group_masks'][group_id]["group_name"] = ( + json.loads(conn.hget("groups", group_id)).get('name')) + return resource + -- cgit v1.2.3