aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/auth/authorisation
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-03-11 23:58:35 +0300
committerMunyoki Kilyungi2024-03-13 10:25:27 +0300
commit7e11ddbbdd6ddfa28367c02d0a3a7f3932c369ae (patch)
treea3f50b605411594ddf970752ffcb24a73f1d306b /gn_auth/auth/authorisation
parenta295d21a42a6ae9c463f7661b32df7de11095835 (diff)
downloadgn-auth-7e11ddbbdd6ddfa28367c02d0a3a7f3932c369ae.tar.gz
Define Group using a frozen dataclass.
* gn_auth/auth/authorisation/data/genotypes.py: Import dataclasses.asdict. (link_genotype_data): Replace dictify with asdict. * gn_auth/auth/authorisation/data/mrna.py: Import dataclasses.asdict. (link_mrna_data): Replace dictify with asdict. * gn_auth/auth/authorisation/data/phenotypes.py: Import dataclasses.asdict. (link_phenotype_data): Replace dictify with asdict. * gn_auth/auth/authorisation/resources/groups/models.py: Import dataclass. (Group): Use frozen dataclass. (Group.dictify): Delete. (GroupRole.dictify): Replace dictify with asdict. * gn_auth/auth/authorisation/resources/groups/views.py: Import dataclasses.asdict. Remove dictify import. (list_groups): Replace dictify with asdict. (create_group): Ditto. * gn_auth/auth/authorisation/resources/views.py: (resource_users): Replace dictify with asdict. * gn_auth/auth/authorisation/users/views.py: Import dataclasses.asdict. Remove dictify import. (user_details): Replace dictify with asdict. (user_group): Ditto. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn_auth/auth/authorisation')
-rw-r--r--gn_auth/auth/authorisation/data/genotypes.py3
-rw-r--r--gn_auth/auth/authorisation/data/mrna.py3
-rw-r--r--gn_auth/auth/authorisation/data/phenotypes.py3
-rw-r--r--gn_auth/auth/authorisation/resources/groups/models.py13
-rw-r--r--gn_auth/auth/authorisation/resources/groups/views.py6
-rw-r--r--gn_auth/auth/authorisation/resources/views.py2
-rw-r--r--gn_auth/auth/authorisation/users/views.py7
7 files changed, 18 insertions, 19 deletions
diff --git a/gn_auth/auth/authorisation/data/genotypes.py b/gn_auth/auth/authorisation/data/genotypes.py
index bfddfc1..e0ffdc8 100644
--- a/gn_auth/auth/authorisation/data/genotypes.py
+++ b/gn_auth/auth/authorisation/data/genotypes.py
@@ -1,5 +1,6 @@
"""Handle linking of Genotype data to the Auth(entic|oris)ation system."""
import uuid
+from dataclasses import asdict
from typing import Iterable
from MySQLdb.cursors import DictCursor
@@ -92,6 +93,6 @@ def link_genotype_data(
"description": (
f"Successfully linked {len(datasets)} to group "
f"'{group.group_name}'."),
- "group": dictify(group),
+ "group": asdict(group),
"datasets": datasets
}
diff --git a/gn_auth/auth/authorisation/data/mrna.py b/gn_auth/auth/authorisation/data/mrna.py
index edcd29e..e65fbd0 100644
--- a/gn_auth/auth/authorisation/data/mrna.py
+++ b/gn_auth/auth/authorisation/data/mrna.py
@@ -1,5 +1,6 @@
"""Handle linking of mRNA Assay data to the Auth(entic|oris)ation system."""
import uuid
+from dataclasses import asdict
from typing import Iterable
from MySQLdb.cursors import DictCursor
@@ -96,6 +97,6 @@ def link_mrna_data(
"description": (
f"Successfully linked {len(datasets)} to group "
f"'{group.group_name}'."),
- "group": dictify(group),
+ "group": asdict(group),
"datasets": datasets
}
diff --git a/gn_auth/auth/authorisation/data/phenotypes.py b/gn_auth/auth/authorisation/data/phenotypes.py
index 17555ec..3baac75 100644
--- a/gn_auth/auth/authorisation/data/phenotypes.py
+++ b/gn_auth/auth/authorisation/data/phenotypes.py
@@ -1,5 +1,6 @@
"""Handle linking of Phenotype data to the Auth(entic|oris)ation system."""
import uuid
+from dataclasses import asdict
from typing import Any, Iterable
from MySQLdb.cursors import DictCursor
@@ -136,6 +137,6 @@ def link_phenotype_data(
return {
"description": (
f"Successfully linked {len(traits)} traits to group."),
- "group": dictify(group),
+ "group": asdict(group),
"traits": params
}
diff --git a/gn_auth/auth/authorisation/resources/groups/models.py b/gn_auth/auth/authorisation/resources/groups/models.py
index 959389c..9a60df9 100644
--- a/gn_auth/auth/authorisation/resources/groups/models.py
+++ b/gn_auth/auth/authorisation/resources/groups/models.py
@@ -2,6 +2,7 @@
import json
from uuid import UUID, uuid4
from functools import reduce
+from dataclasses import dataclass
from typing import Any, Sequence, Iterable, Optional, NamedTuple
from flask import g
@@ -21,18 +22,14 @@ from gn_auth.auth.authorisation.roles.models import (
Role, create_role, check_user_editable, revoke_user_role_by_name,
assign_user_role_by_name)
-class Group(NamedTuple):
+
+@dataclass(frozen=True)
+class Group:
"""Class representing a group."""
group_id: UUID
group_name: str
group_metadata: dict[str, Any]
- 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
- }
DUMMY_GROUP = Group(
group_id=UUID("77cee65b-fe29-4383-ae41-3cb3b480cc70"),
@@ -50,8 +47,8 @@ class GroupRole(NamedTuple):
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)
+ "group_role_id": self.group_role_id, "group": asdict(self.group),
}
class GroupCreationError(AuthorisationError):
diff --git a/gn_auth/auth/authorisation/resources/groups/views.py b/gn_auth/auth/authorisation/resources/groups/views.py
index 26534fc..b655a0f 100644
--- a/gn_auth/auth/authorisation/resources/groups/views.py
+++ b/gn_auth/auth/authorisation/resources/groups/views.py
@@ -6,13 +6,13 @@ import datetime
from typing import Iterable
from functools import partial
+from dataclasses import asdict
from MySQLdb.cursors import DictCursor
from flask import request, jsonify, Response, Blueprint, current_app
from gn_auth.auth.db import sqlite3 as db
from gn_auth.auth.db import mariadb as gn3db
from gn_auth.auth.db.sqlite3 import with_db_connection
-from gn_auth.auth.dictify import dictify
from gn_auth.auth.authorisation.roles.models import Role
from gn_auth.auth.authorisation.roles.models import user_roles
@@ -42,7 +42,7 @@ def list_groups():
the_groups = all_groups(conn)
return jsonify(the_groups.maybe(
- [], lambda grps: [dictify(grp) for grp in grps]))
+ [], lambda grps: [asdict(grp) for grp in grps]))
@groups.route("/create", methods=["POST"])
@require_oauth("profile group")
@@ -59,7 +59,7 @@ def create_group():
new_group = _create_group(
conn, group_name, user, request.form.get("group_description"))
return jsonify({
- **dictify(new_group), "group_leader": asdict(user)
+ **asdict(new_group), "group_leader": asdict(user)
})
@groups.route("/members/<uuid:group_id>", methods=["GET"])
diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py
index 8976dfa..5d00f8c 100644
--- a/gn_auth/auth/authorisation/resources/views.py
+++ b/gn_auth/auth/authorisation/resources/views.py
@@ -193,9 +193,9 @@ def resource_users(resource_id: uuid.UUID):
"users.")
results = (
{
- "user_group": dictify(row["user_group"]),
"roles": tuple(dictify(role) for role in row["roles"])
"user": asdict(row["user"]),
+ "user_group": asdict(row["user_group"]),
} for row in (
user_row for user_id, user_row
in with_db_connection(__the_users__).items()))
diff --git a/gn_auth/auth/authorisation/users/views.py b/gn_auth/auth/authorisation/users/views.py
index 9e6c0c3..ae3c45e 100644
--- a/gn_auth/auth/authorisation/users/views.py
+++ b/gn_auth/auth/authorisation/users/views.py
@@ -2,13 +2,12 @@
import traceback
from typing import Any
from functools import partial
-
+from dataclasses import asdict
import sqlite3
from email_validator import validate_email, EmailNotValidError
from flask import request, jsonify, Response, Blueprint, current_app
from gn_auth.auth.db import sqlite3 as db
-from gn_auth.auth.dictify import dictify
from gn_auth.auth.db.sqlite3 import with_db_connection
from gn_auth.auth.authorisation.resources.models import (
@@ -48,7 +47,7 @@ def user_details() -> Response:
False, lambda grp: grp)# type: ignore[arg-type]
return jsonify({
**user_dets,
- "group": dictify(the_group) if the_group else False
+ "group": asdict(the_group) if the_group else False
})
@users.route("/roles", methods=["GET"])
@@ -134,7 +133,7 @@ def user_group() -> Response:
False, lambda grp: grp)# type: ignore[arg-type]
if group:
- return jsonify(dictify(group))
+ return jsonify(asdict(group))
raise NotFoundError("User is not a member of any group.")
@users.route("/resources", methods=["GET"])