From 4b36031859d1f2e0d4a255741b543afecfe3cecd Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 13 Sep 2023 08:04:55 +0300 Subject: 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. --- gn_auth/auth/authorisation/resources/data.py | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 gn_auth/auth/authorisation/resources/data.py (limited to 'gn_auth/auth/authorisation/resources/data.py') 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) -- cgit v1.2.3