aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-03-12 00:59:07 +0300
committerMunyoki Kilyungi2024-03-13 15:34:56 +0300
commit1c3d0fc73dfe4682ff41a2c8bd84a29f2d2b130a (patch)
treeb019fe88388eae409354234292c1f053bbdb1b71
parent58fde0def491099d3833e3d58ec7c441d84d1ef2 (diff)
downloadgn-auth-1c3d0fc73dfe4682ff41a2c8bd84a29f2d2b130a.tar.gz
Define Resource/ResourceCategory using frozen dataclass.
* gn_auth/auth/authorisation/resources/base.py: Import dataclass and asdict. Remove NamedTuple and dictify. (ResourceCategory): Use frozen dataclass. (ResourceCategory.dictify): Delete. (Resource): Use frozen dataclass. (Resource.dictify): Delete. * gn_auth/auth/authorisation/resources/models.py: Delete dictify import. (assign_resource_user): Replace dictify with asdict. (unassign_resource_user): Ditto. * gn_auth/auth/authorisation/resources/views.py: Import asdict. Remove dictify import. (list_resource_categories): Replace dictify with asdict. (create_resource): Ditto. (view_resource): Ditto. (__safe_get_requests_page__): Ditto. * gn_auth/auth/authorisation/users/views.py: (user_resources): Replace dictify with asdict. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r--gn_auth/auth/authorisation/resources/base.py27
-rw-r--r--gn_auth/auth/authorisation/resources/models.py5
-rw-r--r--gn_auth/auth/authorisation/resources/views.py16
-rw-r--r--gn_auth/auth/authorisation/users/views.py2
4 files changed, 19 insertions, 31 deletions
diff --git a/gn_auth/auth/authorisation/resources/base.py b/gn_auth/auth/authorisation/resources/base.py
index d2075c2..02b738f 100644
--- a/gn_auth/auth/authorisation/resources/base.py
+++ b/gn_auth/auth/authorisation/resources/base.py
@@ -1,37 +1,22 @@
"""Base types for resources."""
from uuid import UUID
-from typing import Any, Sequence, NamedTuple
+from dataclasses import dataclass, asdict
+from typing import Any, Sequence
-from gn_auth.auth.dictify import dictify
-class ResourceCategory(NamedTuple):
+@dataclass(frozen=True)
+class ResourceCategory:
"""Class representing a resource category."""
resource_category_id: UUID
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):
+@dataclass(frozen=True)
+class Resource:
"""Class representing a resource."""
resource_id: UUID
resource_name: str
resource_category: ResourceCategory
public: bool
resource_data: Sequence[dict[str, Any]] = tuple()
-
- def dictify(self) -> dict[str, Any]:
- """Return a dict representation of `Resource` objects."""
- return {
- "resource_id": self.resource_id,
- "resource_name": self.resource_name,
- "resource_category": dictify(self.resource_category),
- "public": self.public,
- "resource_data": self.resource_data
- }
diff --git a/gn_auth/auth/authorisation/resources/models.py b/gn_auth/auth/authorisation/resources/models.py
index 97e6adf..3720baa 100644
--- a/gn_auth/auth/authorisation/resources/models.py
+++ b/gn_auth/auth/authorisation/resources/models.py
@@ -6,7 +6,6 @@ from sqlite3 import Row
from typing import Dict, Sequence, Optional
from gn_auth.auth.db import sqlite3 as db
-from gn_auth.auth.dictify import dictify
from gn_auth.auth.authentication.users import User
from gn_auth.auth.db.sqlite3 import with_db_connection
@@ -341,7 +340,7 @@ def assign_resource_user(
(str(user.user_id), str(role.role.role_id),
str(resource.resource_id)))
return {
- "resource": dictify(resource),
+ "resource": asdict(resource),
"user": asdict(user),
"role": asdict(role),
"description": (
@@ -365,7 +364,7 @@ def unassign_resource_user(
str(role.role.role_id),
str(resource.resource_id)))
return {
- "resource": dictify(resource),
+ "resource": asdict(resource),
"user": asdict(user),
"role": asdict(role),
"description": (
diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py
index 43ee08b..13d9bdf 100644
--- a/gn_auth/auth/authorisation/resources/views.py
+++ b/gn_auth/auth/authorisation/resources/views.py
@@ -2,6 +2,8 @@
import uuid
import json
import sqlite3
+
+from dataclasses import asdict
from functools import reduce
from authlib.integrations.flask_oauth2.errors import _HTTPException
@@ -13,7 +15,6 @@ from gn_auth.auth.db.sqlite3 import with_db_connection
from gn_auth.auth.authorisation.roles import Role
from gn_auth.auth.authorisation.errors import InvalidData, InconsistencyError, AuthorisationError
-from gn_auth.auth.dictify import dictify
from gn_auth.auth.authentication.oauth2.resource_server import require_oauth
from gn_auth.auth.authentication.users import User, user_by_id, user_by_email
@@ -34,7 +35,7 @@ def list_resource_categories() -> Response:
db_uri = app.config["AUTH_DB"]
with db.connection(db_uri) as conn:
return jsonify(tuple(
- dictify(category) for category in resource_categories(conn)))
+ asdict(category) for category in resource_categories(conn)))
@resources.route("/create", methods=["POST"])
@require_oauth("profile group resource")
@@ -53,7 +54,7 @@ def create_resource() -> Response:
resource_category_by_id(conn, resource_category_id),
the_token.user,
(form.get("public") == "on"))
- return jsonify(dictify(resource))
+ return jsonify(asdict(resource))
except sqlite3.IntegrityError as sql3ie:
if sql3ie.args[0] == ("UNIQUE constraint failed: "
"resources.resource_name"):
@@ -70,8 +71,11 @@ def view_resource(resource_id: uuid.UUID) -> Response:
with require_oauth.acquire("profile group resource") as the_token:
db_uri = app.config["AUTH_DB"]
with db.connection(db_uri) as conn:
- return jsonify(dictify(resource_by_id(
- conn, the_token.user, resource_id)))
+ return jsonify(
+ asdict(
+ resource_by_id(conn, the_token.user, resource_id)
+ )
+ )
def __safe_get_requests_page__(key: str = "page") -> int:
"""Get the results page if it exists or default to the first page."""
@@ -325,7 +329,7 @@ def toggle_public(resource_id: uuid.UUID) -> Response:
resource = with_db_connection(__toggle__)
return jsonify({
- "resource": dictify(resource),
+ "resource": asdict(resource),
"description": (
"Made resource public" if resource.public
else "Made resource private")})
diff --git a/gn_auth/auth/authorisation/users/views.py b/gn_auth/auth/authorisation/users/views.py
index f9353eb..7a32292 100644
--- a/gn_auth/auth/authorisation/users/views.py
+++ b/gn_auth/auth/authorisation/users/views.py
@@ -144,7 +144,7 @@ def user_resources() -> Response:
db_uri = current_app.config["AUTH_DB"]
with db.connection(db_uri) as conn:
return jsonify([
- dictify(resource) for resource in
+ asdict(resource) for resource in
_user_resources(conn, the_token.user)])
@users.route("group/join-request", methods=["GET"])