From 52bc7f5037acb4a0c7d55022a7f818a742706790 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 10 Jun 2024 12:38:45 -0500 Subject: Share reusable function --- gn_auth/auth/authorisation/resources/views.py | 33 +++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py index 3a61704..24b2416 100644 --- a/gn_auth/auth/authorisation/resources/views.py +++ b/gn_auth/auth/authorisation/resources/views.py @@ -341,26 +341,29 @@ def toggle_public(resource_id: uuid.UUID) -> Response: else "Made resource private")}) +def __resultset_to_roles__(roles, row): + """Convert SQLite3 resultset into `Role` objects""" + _role = roles.get(row["role_id"]) + return { + **roles, + row["role_id"]: Role( + role_id=uuid.UUID(row["role_id"]), + role_name=row["role_name"], + user_editable=bool(row["user_editable"]), + privileges=( + (_role.privileges if bool(_role) else tuple()) + + (Privilege( + privilege_id=row["privilege_id"], + privilege_description=row[ + "privilege_description"]),))) + } + + @resources.route("/roles", methods=["GET"]) @require_oauth("profile group resource role") def resource_roles(resource_id: uuid.UUID) -> Response: """Return the roles the user has to act on a given resource.""" with require_oauth.acquire("profile group resource role") as _token: - def __resultset_to_roles__(roles, row): - _role = roles.get(row["role_id"]) - return { - **roles, - row["role_id"]: Role( - role_id=uuid.UUID(row["role_id"]), - role_name=row["role_name"], - user_editable=bool(row["user_editable"]), - privileges=( - (_role.privileges if bool(_role) else tuple()) + - (Privilege( - privilege_id=row["privilege_id"], - privilege_description=row[ - "privilege_description"]),))) - } def __roles__(conn: db.DbConnection) -> tuple[Role, ...]: -- cgit v1.2.3