From 27d40788e2e2c8fbeb8873e895d77a76bbd49a45 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 11 Mar 2024 19:34:18 +0300 Subject: Define User using a frozen dataclass. * gn_auth/auth/authentication/users.py: Import dataclass. Remove NamedTuple and Tuple import. (User): Use a frozen dataclass. (User.get_user_id): Delete. (User.dictify): Ditto. * gn_auth/auth/authorisation/data/views.py: Import dataclasses.dict. (authorisation): Replace user._asdict() with asdict(user). (metadata_resources): Ditto. * gn_auth/auth/authorisation/resources/groups/views.py: (group_members): Replace dictify with asdict. * gn_auth/auth/authorisation/resources/models.py: Import dataclasses.asdict. (assign_resource_user): Replace dictify(user) with asdict(user). (unassign_resource_user): Ditto. * gn_auth/auth/authorisation/resources/views.py: (resource_users): Replace dictify with asdict. * gn_auth/auth/authorisation/users/masquerade/views.py: Import dataclasses.asdict. (masquerade): Replace masq_user._asdict() with asdict(masq_user). * gn_auth/auth/authorisation/users/views.py: (list_all_users): Replace dictify with asdict. Signed-off-by: Munyoki Kilyungi --- gn_auth/auth/authentication/users.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'gn_auth/auth/authentication') diff --git a/gn_auth/auth/authentication/users.py b/gn_auth/auth/authentication/users.py index 46cd838..2f8caa1 100644 --- a/gn_auth/auth/authentication/users.py +++ b/gn_auth/auth/authentication/users.py @@ -1,6 +1,7 @@ """User-specific code and data structures.""" from uuid import UUID, uuid4 -from typing import Any, Tuple, NamedTuple +from typing import Tuple +from dataclasses import dataclass from argon2 import PasswordHasher from argon2.exceptions import VerifyMismatchError @@ -8,19 +9,14 @@ from argon2.exceptions import VerifyMismatchError from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.authorisation.errors import NotFoundError -class User(NamedTuple): + +@dataclass(frozen=True) +class User: """Class representing a user.""" user_id: UUID email: str name: str - def get_user_id(self): - """Return the user's UUID. Mostly for use with Authlib.""" - return self.user_id - - def dictify(self) -> dict[str, Any]: - """Return a dict representation of `User` objects.""" - return {"user_id": self.user_id, "email": self.email, "name": self.name} DUMMY_USER = User(user_id=UUID("a391cf60-e8b7-4294-bd22-ddbbda4b3530"), email="gn3@dummy.user", -- cgit v1.2.3