about summary refs log tree commit diff
path: root/gn_auth/auth/authorisation/users/collections
diff options
context:
space:
mode:
Diffstat (limited to 'gn_auth/auth/authorisation/users/collections')
-rw-r--r--gn_auth/auth/authorisation/users/collections/models.py14
-rw-r--r--gn_auth/auth/authorisation/users/collections/views.py1
2 files changed, 10 insertions, 5 deletions
diff --git a/gn_auth/auth/authorisation/users/collections/models.py b/gn_auth/auth/authorisation/users/collections/models.py
index b4a24f3..63443ef 100644
--- a/gn_auth/auth/authorisation/users/collections/models.py
+++ b/gn_auth/auth/authorisation/users/collections/models.py
@@ -33,7 +33,7 @@ def __valid_email__(email:str) -> bool:
 def __toggle_boolean_field__(
         rconn: Redis, email: str, field: str):
     """Toggle the valuen of a boolean field"""
-    mig_dict = json.loads(rconn.hget("migratable-accounts", email) or "{}")
+    mig_dict = json.loads(rconn.hget("migratable-accounts", email) or "{}")  # type: ignore
     if bool(mig_dict):
         rconn.hset("migratable-accounts", email,
                    json.dumps({**mig_dict, field: not mig_dict.get(field, True)}))
@@ -52,7 +52,7 @@ def __build_email_uuid_bridge__(rconn: Redis):
             "resources_migrated": False
         } for account in (
             acct for acct in
-            (json.loads(usr) for usr in rconn.hgetall("users").values())
+            (json.loads(usr) for usr in rconn.hgetall("users").values())  # type: ignore
             if (bool(acct.get("email_address", False)) and
                 __valid_email__(acct["email_address"])))
     }
@@ -66,7 +66,7 @@ def __retrieve_old_accounts__(rconn: Redis) -> dict:
     accounts = rconn.hgetall("migratable-accounts")
     if accounts:
         return {
-            key: json.loads(value) for key, value in accounts.items()
+            key: json.loads(value) for key, value in accounts.items()  # type: ignore
         }
     return __build_email_uuid_bridge__(rconn)
 
@@ -91,13 +91,13 @@ 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_REDIS_COLLECTIONS_KEY__, str(old_user_id)) or "[]"))
+                     __OLD_REDIS_COLLECTIONS_KEY__, str(old_user_id)) or "[]"))  # type: ignore
 
 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
-        "[]"))
+        "[]"))  # type: ignore
     old_accounts = __retrieve_old_accounts__(rconn)
     if (user.email in old_accounts and
         not old_accounts[user.email]["collections-migrated"]):
@@ -205,8 +205,10 @@ def add_traits(rconn: Redis,
     mod_col = tuple(coll for coll in ucolls if coll["id"] == collection_id)
     __raise_if_not_single_collection__(user, collection_id, mod_col)
     new_members = tuple(set(tuple(mod_col[0]["members"]) + traits))
+    now = datetime.utcnow()
     new_coll = {
         **mod_col[0],
+        "changed": now,
         "members": new_members,
         "num_members": len(new_members)
     }
@@ -233,8 +235,10 @@ def remove_traits(rconn: Redis,
     __raise_if_not_single_collection__(user, collection_id, mod_col)
     new_members = tuple(
         trait for trait in mod_col[0]["members"] if trait not in traits)
+    now = datetime.utcnow()
     new_coll = {
         **mod_col[0],
+        "changed": now,
         "members": new_members,
         "num_members": len(new_members)
     }
diff --git a/gn_auth/auth/authorisation/users/collections/views.py b/gn_auth/auth/authorisation/users/collections/views.py
index eeae91d..f619c3d 100644
--- a/gn_auth/auth/authorisation/users/collections/views.py
+++ b/gn_auth/auth/authorisation/users/collections/views.py
@@ -113,6 +113,7 @@ def import_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"))
+        anon_colls = tuple(coll for coll in anon_colls if coll['num_members'] > 0)
         save_collections(
             redisconn,
             token.user,