diff options
author | Frederick Muriuki Muriithi | 2023-06-23 13:14:08 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-06-23 13:14:08 +0300 |
commit | d46d1eb47f9ee18518894d850a18cd952231dddc (patch) | |
tree | e7e986686455276220791b34ee5906819026e2ed /gn3 | |
parent | b7a000b2b695cf5d504b287c0ce7b5125cb3a80f (diff) | |
download | genenetwork3-d46d1eb47f9ee18518894d850a18cd952231dddc.tar.gz |
Fetch all collections, rename variables
Fetch from `__REDIS_COLLECTION_KEY__` not `"collections"` to ensure all
collections are retrieved correctly.
Rename from `__*_DOC__` to `__REDIS_*_KEY__` to more clearly express what the
variables are about.
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/auth/authorisation/users/collections/models.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/gn3/auth/authorisation/users/collections/models.py b/gn3/auth/authorisation/users/collections/models.py index b6598a6..7577fa8 100644 --- a/gn3/auth/authorisation/users/collections/models.py +++ b/gn3/auth/authorisation/users/collections/models.py @@ -10,8 +10,8 @@ from gn3.auth.authorisation.errors import InvalidData, NotFoundError from ..models import User -__OLD_COLLECTIONS_DOC__ = "collections" -__COLLECTIONS_DOC__ = "collections2" +__OLD_REDIS_COLLECTIONS_KEY__ = "collections" +__REDIS_COLLECTIONS_KEY__ = "collections2" class CollectionJSONEncoder(json.JSONEncoder): """Serialise collection objects into JSON.""" @@ -91,22 +91,25 @@ def __retrieve_old_user_collections__(rconn: Redis, old_user_id: UUID) -> tuple: """Retrieve any old collections relating to the user.""" return tuple(parse_collection(coll) for coll in json.loads(rconn.hget( - __OLD_COLLECTIONS_DOC__, str(old_user_id)) or "[]")) + __OLD_REDIS_COLLECTIONS_KEY__, str(old_user_id)) or "[]")) 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("collections", 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 not old_accounts[user.email]["collections-migrated"]): old_user_id = old_accounts[user.email]["user_id"] - collections = tuple(collections + __retrieve_old_user_collections__( - rconn, UUID(old_user_id))) + collections = tuple({ + coll["id"]: coll for coll in ( + collections + __retrieve_old_user_collections__( + rconn, UUID(old_user_id))) + }.values()) __toggle_boolean_field__(rconn, user.email, "collections-migrated") rconn.hset( - __COLLECTIONS_DOC__, + __REDIS_COLLECTIONS_KEY__, key=str(user.user_id), value=json.dumps(collections, cls=CollectionJSONEncoder)) return collections @@ -114,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( - __COLLECTIONS_DOC__, + __REDIS_COLLECTIONS_KEY__, str(user.user_id), json.dumps(collections, cls=CollectionJSONEncoder)) return collections |