diff options
author | Frederick Muriuki Muriithi | 2023-09-13 08:04:55 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-09-26 03:44:29 +0300 |
commit | 4b36031859d1f2e0d4a255741b543afecfe3cecd (patch) | |
tree | fbe29b6dfd2bdf63f0dc6bac9542ca630b4c7312 /gn_auth/auth/authorisation/resources/data.py | |
parent | 9a3b628e2744b658ade8902c1cd33deccbd48b63 (diff) | |
download | gn-auth-4b36031859d1f2e0d4a255741b543afecfe3cecd.tar.gz |
Extract resource-type-specific code into separate modules
For easier maintenance, extract the code that relates to specific
resource types/categories into separate modules, each dealing with a
single resource type/category.
Diffstat (limited to 'gn_auth/auth/authorisation/resources/data.py')
-rw-r--r-- | gn_auth/auth/authorisation/resources/data.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/gn_auth/auth/authorisation/resources/data.py b/gn_auth/auth/authorisation/resources/data.py new file mode 100644 index 0000000..8f5e625 --- /dev/null +++ b/gn_auth/auth/authorisation/resources/data.py @@ -0,0 +1,29 @@ +""" +Utilities for handling data on resources. + +These are mostly meant for internal use. +""" +from uuid import UUID +from typing import Sequence +from functools import reduce + +import sqlite3 + +from .base import Resource + +def __attach_data__( + data_rows: Sequence[sqlite3.Row], + resources: Sequence[Resource]) -> Sequence[Resource]: + def __organise__(acc, row): + resource_id = UUID(row["resource_id"]) + return { + **acc, + resource_id: acc.get(resource_id, tuple()) + (dict(row),) + } + organised: dict[UUID, tuple[dict, ...]] = reduce(__organise__, data_rows, {}) + return tuple( + Resource( + resource.group, resource.resource_id, resource.resource_name, + resource.resource_category, resource.public, + organised.get(resource.resource_id, tuple())) + for resource in resources) |