From c294f6fa425c47bb33581077848634a1143cedc0 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 16 May 2023 14:01:12 +0300 Subject: auth: View a collection by its ID. --- gn3/auth/authorisation/users/collections/models.py | 18 ++++++++++++++++++ gn3/auth/authorisation/users/collections/views.py | 21 ++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) (limited to 'gn3/auth') diff --git a/gn3/auth/authorisation/users/collections/models.py b/gn3/auth/authorisation/users/collections/models.py index 46dfb53..a0c3ebf 100644 --- a/gn3/auth/authorisation/users/collections/models.py +++ b/gn3/auth/authorisation/users/collections/models.py @@ -6,6 +6,8 @@ from datetime import datetime from redis import Redis from email_validator import validate_email, EmailNotValidError +from gn3.auth.authorisation.errors import InvalidData, NotFoundError + from ..models import User class CollectionJSONEncoder(json.JSONEncoder): @@ -127,3 +129,19 @@ def create_collection(rconn: Redis, user: User, name: str, traits: tuple) -> dic "num_members": len(traits), "members": traits }) + +def get_collection(rconn: Redis, user: User, collection_id: UUID) -> dict: + """Retrieve the collection with ID `collection_id`.""" + colls = tuple(coll for coll in user_collections(rconn, user) + if coll["id"] == collection_id) + if len(colls) == 0: + raise NotFoundError( + f"Could not find a collection with ID `{collection_id}` for user " + f"with ID `{user.user_id}`") + if len(colls) > 1: + err = InvalidData( + "More than one collection was found having the ID " + f"`{collection_id}` for user with ID `{user.user_id}`.") + err.error_code = 513 + raise err + return colls[0] diff --git a/gn3/auth/authorisation/users/collections/views.py b/gn3/auth/authorisation/users/collections/views.py index cd9458e..8ec6b46 100644 --- a/gn3/auth/authorisation/users/collections/views.py +++ b/gn3/auth/authorisation/users/collections/views.py @@ -12,7 +12,7 @@ from gn3.auth.authorisation.errors import NotFoundError from gn3.auth.authentication.users import User, user_by_id from gn3.auth.authentication.oauth2.resource_server import require_oauth -from .models import user_collections, create_collection +from .models import get_collection, user_collections, create_collection collections = Blueprint("collections", __name__) @@ -71,3 +71,22 @@ def new_user_collection() -> Response: redisconn, name, traits)) return jsonify(__new_collection_as_anonymous_user__( redisconn, name, traits)) + +@collections.route("//view", methods=["POST"]) +@require_json +def view_collection(collection_id: UUID) -> Response: + """View a particular collection""" + with (Redis.from_url(current_app.config["REDIS_URI"], + decode_responses=True) as redisconn): + if bool(request.headers.get("Authorization")): + with require_oauth.acquire("profile user") as token: + return jsonify(get_collection(redisconn, + token.user, + collection_id)) + return jsonify(get_collection( + redisconn, + User( + UUID(request.json.get("anon_id")),#type: ignore[union-attr] + "anon@ymous.user", + "Anonymous User"), + collection_id)) -- cgit v1.2.3