diff options
author | Frederick Muriuki Muriithi | 2023-01-28 03:16:45 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-01-28 03:20:01 +0300 |
commit | 76c464946d01073b8bcb757345d0d42b9a8207e4 (patch) | |
tree | 7d5cd20018c65207b809842277e7533d7490de7b /gn3/auth/authorisation/resources.py | |
parent | e6e173b74d381f590ff5a8e7957489ea4d50c06b (diff) | |
download | genenetwork3-76c464946d01073b8bcb757345d0d42b9a8207e4.tar.gz |
auth: rework dictify
Define a Protocol type to use with the `dictify` function and implement the
`dictify` methods for the various classes.
Diffstat (limited to 'gn3/auth/authorisation/resources.py')
-rw-r--r-- | gn3/auth/authorisation/resources.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/gn3/auth/authorisation/resources.py b/gn3/auth/authorisation/resources.py index 29e50bf..1e37d7a 100644 --- a/gn3/auth/authorisation/resources.py +++ b/gn3/auth/authorisation/resources.py @@ -1,9 +1,10 @@ """Handle the management of resources.""" import json from uuid import UUID, uuid4 -from typing import Dict, Sequence, NamedTuple +from typing import Any, Dict, Sequence, NamedTuple from gn3.auth import db +from gn3.auth.dictify import dictify from gn3.auth.authentication.users import User from .checks import authorised_p @@ -19,6 +20,14 @@ class ResourceCategory(NamedTuple): 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): """Class representing a resource.""" group: Group @@ -27,6 +36,15 @@ class Resource(NamedTuple): resource_category: ResourceCategory public: bool + def dictify(self) -> dict[str, Any]: + """Return a dict representation of `Resource` objects.""" + return { + "group": dictify(self.group), "resource_id": self.resource_id, + "resource_name": self.resource_name, + "resource_category": dictify(self.resource_category), + "public": self.public + } + @authorised_p(("group:resource:create-resource",), error_message="Could not create resource") def create_resource( |