From 76c464946d01073b8bcb757345d0d42b9a8207e4 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Sat, 28 Jan 2023 03:16:45 +0300 Subject: auth: rework dictify Define a Protocol type to use with the `dictify` function and implement the `dictify` methods for the various classes. --- gn3/auth/authorisation/groups.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'gn3/auth/authorisation/groups.py') diff --git a/gn3/auth/authorisation/groups.py b/gn3/auth/authorisation/groups.py index 6d1b1a3..9dd5b71 100644 --- a/gn3/auth/authorisation/groups.py +++ b/gn3/auth/authorisation/groups.py @@ -7,8 +7,8 @@ from flask import g from pymonad.maybe import Just, Maybe, Nothing from gn3.auth import db +from gn3.auth.dictify import dictify from gn3.auth.authentication.users import User -from gn3.auth.dictify import register_dictifier from gn3.auth.authentication.checks import authenticated_p from .checks import authorised_p @@ -23,9 +23,12 @@ class Group(NamedTuple): group_name: str group_metadata: dict[str, Any] -register_dictifier(Group, lambda grp: { - "group_id": grp.group_id, "group_name": grp.group_name, - "group_metadata": grp.group_metadata}) + def dictify(self): + """Return a dict representation of `Group` objects.""" + return { + "group_id": self.group_id, "group_name": self.group_name, + "group_metadata": self.group_metadata + } class GroupRole(NamedTuple): """Class representing a role tied/belonging to a group.""" @@ -33,6 +36,13 @@ class GroupRole(NamedTuple): group: Group role: Role + def dictify(self) -> dict[str, Any]: + """Return a dict representation of `GroupRole` objects.""" + return { + "group_role_id": self.group_role_id, "group": dictify(self.group), + "role": dictify(self.role) + } + class GroupCreationError(AuthorisationError): """Raised whenever a group creation fails""" -- cgit v1.2.3