aboutsummaryrefslogtreecommitdiff
path: root/gn3/auth
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/auth')
-rw-r--r--gn3/auth/authorisation/users/collections/models.py29
-rw-r--r--gn3/auth/authorisation/users/collections/views.py28
2 files changed, 54 insertions, 3 deletions
diff --git a/gn3/auth/authorisation/users/collections/models.py b/gn3/auth/authorisation/users/collections/models.py
index 28223cb..708986e 100644
--- a/gn3/auth/authorisation/users/collections/models.py
+++ b/gn3/auth/authorisation/users/collections/models.py
@@ -208,9 +208,9 @@ def add_traits(rconn: Redis,
return new_coll
def remove_traits(rconn: Redis,
- user: User,
- collection_id: UUID,
- traits: tuple[str, ...]) -> dict:
+ user: User,
+ collection_id: UUID,
+ traits: tuple[str, ...]) -> dict:
"""
Remove `traits` from the `user` collection identified by `collection_id`.
@@ -234,3 +234,26 @@ def remove_traits(rconn: Redis,
(tuple(coll for coll in ucolls if coll["id"] != collection_id) +
(new_coll,)))
return new_coll
+
+def change_name(rconn: Redis,
+ user: User,
+ collection_id: UUID,
+ new_name: str) -> dict:
+ """
+ Change the collection's name.
+
+ Returns: The collection with the new name.
+ """
+ ucolls = user_collections(rconn, user)
+ __raise_if_collections_empty__(user, ucolls)
+
+ mod_col = tuple(coll for coll in ucolls if coll["id"] == collection_id)
+ __raise_if_not_single_collection__(user, collection_id, mod_col)
+
+ new_coll = {**mod_col[0], "name": new_name}
+ save_collections(
+ rconn,
+ user,
+ (tuple(coll for coll in ucolls if coll["id"] != collection_id) +
+ (new_coll,)))
+ return new_coll
diff --git a/gn3/auth/authorisation/users/collections/views.py b/gn3/auth/authorisation/users/collections/views.py
index c8d470d..1fa25a3 100644
--- a/gn3/auth/authorisation/users/collections/views.py
+++ b/gn3/auth/authorisation/users/collections/views.py
@@ -14,6 +14,7 @@ from gn3.auth.authentication.oauth2.resource_server import require_oauth
from .models import (
add_traits,
+ change_name,
remove_traits,
get_collection,
user_collections,
@@ -209,3 +210,30 @@ def add_traits_to_collection(collection_id: UUID) -> Response:
"message": f"Added {len(the_traits)} traits to collection.",
"collection": coll
})
+
+@collections.route("/<uuid:collection_id>/rename", methods=["POST"])
+@require_json
+def rename_collection(collection_id: UUID) -> Response:
+ """Rename the given collection"""
+ if not bool(request.json["new_name"]):#type: ignore[index]
+ return jsonify({"message": "No new name to change to."})
+
+ new_name = request.json["new_name"]#type: ignore[index]
+ with (Redis.from_url(current_app.config["REDIS_URI"],
+ decode_responses=True) as redisconn):
+ if not bool(request.headers.get("Authorization")):
+ coll = change_name(redisconn,
+ User(UUID(request.json["anon_id"]),#type: ignore[index]
+ "anon@ymous.user",
+ "Anonymous User"),
+ collection_id,
+ new_name)
+ else:
+ with require_oauth.acquire("profile user") as token:
+ coll = change_name(
+ redisconn, token.user, collection_id, new_name)
+
+ return jsonify({
+ "message": "Collection rename successful.",
+ "collection": coll
+ })