From 5a644ebea8bf9708ec89674e5789a88add56b440 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 18 Dec 2023 16:21:00 +0300 Subject: Provide the correct Redis key Previously, when the user would request to either import or delete the collections they had created before logging in, the system would try deleting the collections from the wrong key, leading to the collections still showing up. This commit fixes that by providing the code with the correct Redis key to act upon. --- gn_auth/auth/authorisation/users/collections/models.py | 8 ++++---- gn_auth/auth/authorisation/users/collections/views.py | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'gn_auth/auth') diff --git a/gn_auth/auth/authorisation/users/collections/models.py b/gn_auth/auth/authorisation/users/collections/models.py index 9157100..9397094 100644 --- a/gn_auth/auth/authorisation/users/collections/models.py +++ b/gn_auth/auth/authorisation/users/collections/models.py @@ -10,8 +10,8 @@ from ...errors import InvalidData, NotFoundError from ..models import User +REDIS_COLLECTIONS_KEY = "collections2" __OLD_REDIS_COLLECTIONS_KEY__ = "collections" -__REDIS_COLLECTIONS_KEY__ = "collections2" class CollectionJSONEncoder(json.JSONEncoder): """Serialise collection objects into JSON.""" @@ -96,7 +96,7 @@ def __retrieve_old_user_collections__(rconn: Redis, old_user_id: UUID) -> tuple: def user_collections(rconn: Redis, user: User) -> tuple[dict, ...]: """Retrieve current user collections.""" collections = tuple(parse_collection(coll) for coll in json.loads( - rconn.hget(__REDIS_COLLECTIONS_KEY__, str(user.user_id)) or + rconn.hget(REDIS_COLLECTIONS_KEY, str(user.user_id)) or "[]")) old_accounts = __retrieve_old_accounts__(rconn) if (user.email in old_accounts and @@ -109,7 +109,7 @@ def user_collections(rconn: Redis, user: User) -> tuple[dict, ...]: }.values()) __toggle_boolean_field__(rconn, user.email, "collections-migrated") rconn.hset( - __REDIS_COLLECTIONS_KEY__, + REDIS_COLLECTIONS_KEY, key=str(user.user_id), value=json.dumps(collections, cls=CollectionJSONEncoder)) return collections @@ -117,7 +117,7 @@ def user_collections(rconn: Redis, user: User) -> tuple[dict, ...]: def save_collections(rconn: Redis, user: User, collections: tuple[dict, ...]) -> tuple[dict, ...]: """Save the `collections` to redis.""" rconn.hset( - __REDIS_COLLECTIONS_KEY__, + REDIS_COLLECTIONS_KEY, str(user.user_id), json.dumps(collections, cls=CollectionJSONEncoder)) return collections diff --git a/gn_auth/auth/authorisation/users/collections/views.py b/gn_auth/auth/authorisation/users/collections/views.py index a1cc30d..6c68b35 100644 --- a/gn_auth/auth/authorisation/users/collections/views.py +++ b/gn_auth/auth/authorisation/users/collections/views.py @@ -21,6 +21,7 @@ from .models import ( user_collections, save_collections, create_collection, + REDIS_COLLECTIONS_KEY, delete_collections as _delete_collections) collections = Blueprint("collections", __name__) @@ -116,7 +117,7 @@ def import_anonymous() -> Response: token.user, (user_collections(redisconn, token.user) + anon_colls)) - redisconn.hdel("collections", str(anon_id)) + redisconn.hdel(REDIS_COLLECTIONS_KEY, str(anon_id)) return jsonify({ "message": f"Import of {len(anon_colls)} was successful." }) @@ -132,7 +133,7 @@ def delete_anonymous() -> Response: anon_id = UUID(request.json.get("anon_id"))#type: ignore[union-attr] anon_colls = user_collections(redisconn, User( anon_id, "anon@ymous.user", "Anonymous User")) - redisconn.hdel("collections", str(anon_id)) + redisconn.hdel(REDIS_COLLECTIONS_KEY, str(anon_id)) return jsonify({ "message": f"Deletion of {len(anon_colls)} was successful." }) -- cgit v1.2.3