aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/auth/authorisation/users
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-12-18 16:21:00 +0300
committerFrederick Muriuki Muriithi2023-12-18 16:21:00 +0300
commit5a644ebea8bf9708ec89674e5789a88add56b440 (patch)
treedc9d86f1cea42f6a7b64b59ccc2fb11db318ef08 /gn_auth/auth/authorisation/users
parent2290b274689114442c4fee87bfb1eb325c240c14 (diff)
downloadgn-auth-5a644ebea8bf9708ec89674e5789a88add56b440.tar.gz
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.
Diffstat (limited to 'gn_auth/auth/authorisation/users')
-rw-r--r--gn_auth/auth/authorisation/users/collections/models.py8
-rw-r--r--gn_auth/auth/authorisation/users/collections/views.py5
2 files changed, 7 insertions, 6 deletions
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."
})