From 1c3d0fc73dfe4682ff41a2c8bd84a29f2d2b130a Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 12 Mar 2024 00:59:07 +0300 Subject: Define Resource/ResourceCategory using frozen dataclass. * gn_auth/auth/authorisation/resources/base.py: Import dataclass and asdict. Remove NamedTuple and dictify. (ResourceCategory): Use frozen dataclass. (ResourceCategory.dictify): Delete. (Resource): Use frozen dataclass. (Resource.dictify): Delete. * gn_auth/auth/authorisation/resources/models.py: Delete dictify import. (assign_resource_user): Replace dictify with asdict. (unassign_resource_user): Ditto. * gn_auth/auth/authorisation/resources/views.py: Import asdict. Remove dictify import. (list_resource_categories): Replace dictify with asdict. (create_resource): Ditto. (view_resource): Ditto. (__safe_get_requests_page__): Ditto. * gn_auth/auth/authorisation/users/views.py: (user_resources): Replace dictify with asdict. Signed-off-by: Munyoki Kilyungi --- gn_auth/auth/authorisation/resources/base.py | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) (limited to 'gn_auth/auth/authorisation/resources/base.py') diff --git a/gn_auth/auth/authorisation/resources/base.py b/gn_auth/auth/authorisation/resources/base.py index d2075c2..02b738f 100644 --- a/gn_auth/auth/authorisation/resources/base.py +++ b/gn_auth/auth/authorisation/resources/base.py @@ -1,37 +1,22 @@ """Base types for resources.""" from uuid import UUID -from typing import Any, Sequence, NamedTuple +from dataclasses import dataclass, asdict +from typing import Any, Sequence -from gn_auth.auth.dictify import dictify -class ResourceCategory(NamedTuple): +@dataclass(frozen=True) +class ResourceCategory: """Class representing a resource category.""" resource_category_id: UUID resource_category_key: str resource_category_description: str - def dictify(self) -> dict[str, Any]: - """Return a dict representation of `ResourceCategory` objects.""" - return { - "resource_category_id": self.resource_category_id, - "resource_category_key": self.resource_category_key, - "resource_category_description": self.resource_category_description - } -class Resource(NamedTuple): +@dataclass(frozen=True) +class Resource: """Class representing a resource.""" resource_id: UUID resource_name: str resource_category: ResourceCategory public: bool resource_data: Sequence[dict[str, Any]] = tuple() - - def dictify(self) -> dict[str, Any]: - """Return a dict representation of `Resource` objects.""" - return { - "resource_id": self.resource_id, - "resource_name": self.resource_name, - "resource_category": dictify(self.resource_category), - "public": self.public, - "resource_data": self.resource_data - } -- cgit v1.2.3