From 8ccf7409f6c89c3c5079a73c73dc2f6bfae086a7 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Tue, 12 Mar 2024 00:29:30 +0300 Subject: Define Privilege/Role using frozen dataclass. * gn_auth/auth/authorisation/privileges.py: Import dataclass. Remove NamedTuple import. (Privilege): Use frozen dataclass. (Privelege.dictify): Delete. * gn_auth/auth/authorisation/resources/groups/views.py: Import dataclasses.asdict. (group_privileges): Replace dictify with asdict. (add_priv_to_role): Ditto. (delete_priv_from_role): Ditto. * gn_auth/auth/authorisation/resources/models.py: (assign_resource_user): Replace dictify with asdict. (unassign_resource_user): Ditto. * gn_auth/auth/authorisation/resources/system/views.py: Import dataclasses.asdict. Remove dictify import. (system_roles): Replace dictify with asdict. * gn_auth/auth/authorisation/resources/views.py: (resource_users): Replace dictify with asdict. (resources_authorisation): Ditto. * gn_auth/auth/authorisation/roles/models.py: Remove dictify and NameTuple import. (Role): Use frozen dataclass. (Role.dictify): Replace dictify(priv) with asdict(priv). * gn_auth/auth/authorisation/roles/views.py: Import dataclasses.asdict. Remove dictify import. (view_role): Replace dictify with asdict. * gn_auth/auth/authorisation/users/views.py: (user_roles): Replace dictify with asdict. Signed-off-by: Munyoki Kilyungi --- gn_auth/auth/authorisation/roles/models.py | 14 +++++--------- gn_auth/auth/authorisation/roles/views.py | 5 +++-- 2 files changed, 8 insertions(+), 11 deletions(-) (limited to 'gn_auth/auth/authorisation/roles') diff --git a/gn_auth/auth/authorisation/roles/models.py b/gn_auth/auth/authorisation/roles/models.py index a3a9d5b..74f9e64 100644 --- a/gn_auth/auth/authorisation/roles/models.py +++ b/gn_auth/auth/authorisation/roles/models.py @@ -3,30 +3,26 @@ from uuid import UUID, uuid4 from functools import reduce from typing import Any, Sequence, Iterable, NamedTuple +from typing import Any, Sequence, Iterable + from pymonad.either import Left, Right, Either from ...db import sqlite3 as db -from ...dictify import dictify from ...authentication.users import User from ..checks import authorised_p from ..privileges import Privilege from ..errors import NotFoundError, AuthorisationError -class Role(NamedTuple): + +@dataclass(frozen=True) +class Role: """Class representing a role: creates immutable objects.""" role_id: UUID role_name: str user_editable: bool privileges: tuple[Privilege, ...] - def dictify(self) -> dict[str, Any]: - """Return a dict representation of `Role` objects.""" - return { - "role_id": self.role_id, "role_name": self.role_name, - "user_editable": self.user_editable, - "privileges": tuple(dictify(priv) for priv in self.privileges) - } def check_user_editable(role: Role): """Raise an exception if `role` is not user editable.""" diff --git a/gn_auth/auth/authorisation/roles/views.py b/gn_auth/auth/authorisation/roles/views.py index a9b337f..00def89 100644 --- a/gn_auth/auth/authorisation/roles/views.py +++ b/gn_auth/auth/authorisation/roles/views.py @@ -1,9 +1,10 @@ """The views/routes for the `gn3.auth.authorisation.roles` package.""" import uuid +from dataclasses import asdict + from flask import jsonify, Response, Blueprint, current_app -from ...dictify import dictify from ...db import sqlite3 as db from .models import user_role @@ -23,4 +24,4 @@ def view_role(role_id: uuid.UUID) -> Response: with db.connection(db_uri) as conn: the_role = user_role(conn, the_token.user, role_id) return the_role.either( - __error__, lambda a_role: jsonify((dictify(a_role[0]), str(a_role[1])))) + __error__, lambda a_role: jsonify((asdict(a_role[0]), str(a_role[1])))) -- cgit v1.2.3