From 027c60214cfdaf0c8a5ea9b8570c6de4682b7fbf Mon Sep 17 00:00:00 2001 From: Nathan Panchout Date: Wed, 30 Jul 2025 14:43:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(backend)=20add=20creator=20id=20field?= =?UTF-8?q?=20in=20Item=20serializer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added the creator id field to ListItemSerializer and ItemSerializer to enhance data representation and facilitate access to the creator's identifier in the serialized output. --- src/backend/core/api/serializers.py | 4 ++-- src/backend/core/models.py | 5 ++++- .../tests/items/test_api_items_children_list.py | 16 ++++++++++++++++ .../tests/items/test_api_items_favorite_list.py | 1 + .../core/tests/items/test_api_items_list.py | 3 +++ .../tests/items/test_api_items_list_filters.py | 2 ++ .../core/tests/items/test_api_items_retrieve.py | 10 ++++++++++ .../core/tests/items/test_api_items_trashbin.py | 1 + .../core/tests/items/test_api_items_tree.py | 13 +++++++++++++ 9 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/backend/core/api/serializers.py b/src/backend/core/api/serializers.py index d08578a2..7f8dca7f 100644 --- a/src/backend/core/api/serializers.py +++ b/src/backend/core/api/serializers.py @@ -27,8 +27,8 @@ class UserLiteSerializer(UserSerializer): class Meta: model = models.User - fields = ["full_name", "short_name"] - read_only_fields = ["full_name", "short_name"] + fields = ["id", "full_name", "short_name"] + read_only_fields = ["id", "full_name", "short_name"] class BaseAccessSerializer(serializers.ModelSerializer): diff --git a/src/backend/core/models.py b/src/backend/core/models.py index 236c71dc..e6b90f62 100644 --- a/src/backend/core/models.py +++ b/src/backend/core/models.py @@ -95,7 +95,10 @@ class ItemUploadStateChoices(models.TextChoices): PENDING = "pending", _("Pending") ANALYZING = "analyzing", _("Analyzing") SUSPICIOUS = "suspicious", _("Suspicious") - FILE_TOO_LARGE_TO_ANALYZE = "file_too_large_to_analyze", _("File too large to analyze") + FILE_TOO_LARGE_TO_ANALYZE = ( + "file_too_large_to_analyze", + _("File too large to analyze"), + ) READY = "ready", _("Ready") diff --git a/src/backend/core/tests/items/test_api_items_children_list.py b/src/backend/core/tests/items/test_api_items_children_list.py index dfb94b25..c3b19868 100644 --- a/src/backend/core/tests/items/test_api_items_children_list.py +++ b/src/backend/core/tests/items/test_api_items_children_list.py @@ -36,6 +36,7 @@ def test_api_items_children_list_anonymous_public_standalone(): "abilities": child1.get_abilities(AnonymousUser()), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -68,6 +69,7 @@ def test_api_items_children_list_anonymous_public_standalone(): "abilities": child2.get_abilities(AnonymousUser()), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, @@ -139,6 +141,7 @@ def test_api_items_children_list_anonymous_public_parent(): "abilities": child1.get_abilities(AnonymousUser()), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -169,6 +172,7 @@ def test_api_items_children_list_anonymous_public_parent(): "abilities": child2.get_abilities(AnonymousUser()), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, @@ -251,6 +255,7 @@ def test_api_items_children_list_authenticated_unrelated_public_or_authenticated "abilities": child1.get_abilities(user), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -283,6 +288,7 @@ def test_api_items_children_list_authenticated_unrelated_public_or_authenticated "abilities": child2.get_abilities(user), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, @@ -352,6 +358,7 @@ def test_api_items_children_list_authenticated_public_or_authenticated_parent( "abilities": child1.get_abilities(user), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -384,6 +391,7 @@ def test_api_items_children_list_authenticated_public_or_authenticated_parent( "abilities": child2.get_abilities(user), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, @@ -478,6 +486,7 @@ def test_api_items_children_list_authenticated_related_direct(): "abilities": child1.get_abilities(user), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -510,6 +519,7 @@ def test_api_items_children_list_authenticated_related_direct(): "abilities": child2.get_abilities(user), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, @@ -580,6 +590,7 @@ def test_api_items_children_list_authenticated_related_parent(): "abilities": child1.get_abilities(user), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -612,6 +623,7 @@ def test_api_items_children_list_authenticated_related_parent(): "abilities": child2.get_abilities(user), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, @@ -745,6 +757,7 @@ def test_api_items_children_list_authenticated_related_team_members( "abilities": child1.get_abilities(user), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -777,6 +790,7 @@ def test_api_items_children_list_authenticated_related_team_members( "abilities": child2.get_abilities(user), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, @@ -843,6 +857,7 @@ def test_api_items_children_list_filter_type(): "abilities": child1.get_abilities(user), "created_at": child1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child1.creator.id), "full_name": child1.creator.full_name, "short_name": child1.creator.short_name, }, @@ -888,6 +903,7 @@ def test_api_items_children_list_filter_type(): "abilities": child2.get_abilities(user), "created_at": child2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(child2.creator.id), "full_name": child2.creator.full_name, "short_name": child2.creator.short_name, }, diff --git a/src/backend/core/tests/items/test_api_items_favorite_list.py b/src/backend/core/tests/items/test_api_items_favorite_list.py index f11ea189..78061ef8 100644 --- a/src/backend/core/tests/items/test_api_items_favorite_list.py +++ b/src/backend/core/tests/items/test_api_items_favorite_list.py @@ -69,6 +69,7 @@ def test_api_item_favorite_list_authenticated_with_favorite(): "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, diff --git a/src/backend/core/tests/items/test_api_items_list.py b/src/backend/core/tests/items/test_api_items_list.py index 76a666fb..3655029d 100644 --- a/src/backend/core/tests/items/test_api_items_list.py +++ b/src/backend/core/tests/items/test_api_items_list.py @@ -88,6 +88,7 @@ def test_api_items_list_format(): "abilities": item2.get_abilities(user), "created_at": item2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item2.creator.id), "full_name": item2.creator.full_name, "short_name": item2.creator.short_name, }, @@ -118,6 +119,7 @@ def test_api_items_list_format(): "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -148,6 +150,7 @@ def test_api_items_list_format(): "abilities": item3.get_abilities(user), "created_at": item3.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item3.creator.id), "full_name": item3.creator.full_name, "short_name": item3.creator.short_name, }, diff --git a/src/backend/core/tests/items/test_api_items_list_filters.py b/src/backend/core/tests/items/test_api_items_list_filters.py index c781600a..4b6e4eef 100644 --- a/src/backend/core/tests/items/test_api_items_list_filters.py +++ b/src/backend/core/tests/items/test_api_items_list_filters.py @@ -220,6 +220,7 @@ def test_api_items_list_filter_is_creator_me_true(): # Ensure all results are created by the current user for result in results: assert result["creator"] == { + "id": str(user.id), "full_name": user.full_name, "short_name": user.short_name, } @@ -245,6 +246,7 @@ def test_api_items_list_filter_is_creator_me_false(): # Ensure all results are created by other users for result in results: assert result["creator"] != { + "id": str(user.id), "full_name": user.full_name, "short_name": user.short_name, } diff --git a/src/backend/core/tests/items/test_api_items_retrieve.py b/src/backend/core/tests/items/test_api_items_retrieve.py index cba74c92..7c632e7a 100644 --- a/src/backend/core/tests/items/test_api_items_retrieve.py +++ b/src/backend/core/tests/items/test_api_items_retrieve.py @@ -29,6 +29,7 @@ def test_api_items_retrieve_anonymous_public_standalone(): "abilities": item.get_abilities(AnonymousUser()), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -80,6 +81,7 @@ def test_api_items_retrieve_anonymous_public_parent(): "abilities": item.get_abilities(AnonymousUser()), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -176,6 +178,7 @@ def test_api_items_retrieve_authenticated_unrelated_public_or_authenticated(reac "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -233,6 +236,7 @@ def test_api_items_retrieve_authenticated_public_or_authenticated_parent(reach): "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -367,6 +371,7 @@ def test_api_items_retrieve_authenticated_related_direct(): "id": str(item.id), "abilities": item.get_abilities(user), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -426,6 +431,7 @@ def test_api_items_retrieve_authenticated_related_parent(): "id": str(item.id), "abilities": item.get_abilities(user), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -604,6 +610,7 @@ def test_api_items_retrieve_authenticated_related_team_members( "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -675,6 +682,7 @@ def test_api_items_retrieve_authenticated_related_team_administrators( "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -746,6 +754,7 @@ def test_api_items_retrieve_authenticated_related_team_owners( "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, @@ -1111,6 +1120,7 @@ def test_api_items_retrieve_file_with_url_property(upload_state): "abilities": item.get_abilities(user), "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, diff --git a/src/backend/core/tests/items/test_api_items_trashbin.py b/src/backend/core/tests/items/test_api_items_trashbin.py index 7e114c14..0082d144 100644 --- a/src/backend/core/tests/items/test_api_items_trashbin.py +++ b/src/backend/core/tests/items/test_api_items_trashbin.py @@ -67,6 +67,7 @@ def test_api_items_trashbin_format(settings): "created_at": item.created_at.isoformat().replace("+00:00", "Z"), "deleted_at": item.deleted_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(item.creator.id), "full_name": item.creator.full_name, "short_name": item.creator.short_name, }, diff --git a/src/backend/core/tests/items/test_api_items_tree.py b/src/backend/core/tests/items/test_api_items_tree.py index 34d71dc6..b13cf4ae 100644 --- a/src/backend/core/tests/items/test_api_items_tree.py +++ b/src/backend/core/tests/items/test_api_items_tree.py @@ -143,6 +143,7 @@ def test_items_api_anonymous_to_a_public_tree_structure(): "children": [], "created_at": level2_1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(level2_1.creator.id), "full_name": level2_1.creator.full_name, "short_name": level2_1.creator.short_name, }, @@ -174,6 +175,7 @@ def test_items_api_anonymous_to_a_public_tree_structure(): "children": [], "created_at": level2_2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(level2_2.creator.id), "full_name": level2_2.creator.full_name, "short_name": level2_2.creator.short_name, }, @@ -203,6 +205,7 @@ def test_items_api_anonymous_to_a_public_tree_structure(): ], "created_at": level1_2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(level1_2.creator.id), "full_name": level1_2.creator.full_name, "short_name": level1_2.creator.short_name, }, @@ -348,6 +351,7 @@ def test_items_api_tree_authenticated_direct_access(django_assert_num_queries): "+00:00", "Z" ), "creator": { + "id": str(level2_1.item.creator.id), "full_name": level2_1.item.creator.full_name, "short_name": level2_1.item.creator.short_name, }, @@ -386,6 +390,7 @@ def test_items_api_tree_authenticated_direct_access(django_assert_num_queries): "+00:00", "Z" ), "creator": { + "id": str(level3_1.item.creator.id), "full_name": level3_1.item.creator.full_name, "short_name": level3_1.item.creator.short_name, }, @@ -422,6 +427,7 @@ def test_items_api_tree_authenticated_direct_access(django_assert_num_queries): "+00:00", "Z" ), "creator": { + "id": str(level2_2.item.creator.id), "full_name": level2_2.item.creator.full_name, "short_name": level2_2.item.creator.short_name, }, @@ -455,6 +461,7 @@ def test_items_api_tree_authenticated_direct_access(django_assert_num_queries): "+00:00", "Z" ), "creator": { + "id": str(level1_1.item.creator.id), "full_name": level1_1.item.creator.full_name, "short_name": level1_1.item.creator.short_name, }, @@ -490,6 +497,7 @@ def test_items_api_tree_authenticated_direct_access(django_assert_num_queries): "+00:00", "Z" ), "creator": { + "id": str(level1_2.item.creator.id), "full_name": level1_2.item.creator.full_name, "short_name": level1_2.item.creator.short_name, }, @@ -525,6 +533,7 @@ def test_items_api_tree_authenticated_direct_access(django_assert_num_queries): "+00:00", "Z" ), "creator": { + "id": str(level1_3.item.creator.id), "full_name": level1_3.item.creator.full_name, "short_name": level1_3.item.creator.short_name, }, @@ -556,6 +565,7 @@ def test_items_api_tree_authenticated_direct_access(django_assert_num_queries): ], "created_at": root.item.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(root.item.creator.id), "full_name": root.item.creator.full_name, "short_name": root.item.creator.short_name, }, @@ -668,6 +678,7 @@ def test_api_items_tree_authenticated_with_access_authenticated(): assert response.json() == { "created_at": level1_1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(level1_1.creator.id), "full_name": level1_1.creator.full_name, "short_name": level1_1.creator.short_name, }, @@ -700,6 +711,7 @@ def test_api_items_tree_authenticated_with_access_authenticated(): "children": [], "created_at": level2_1.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(level2_1.creator.id), "full_name": level2_1.creator.full_name, "short_name": level2_1.creator.short_name, }, @@ -731,6 +743,7 @@ def test_api_items_tree_authenticated_with_access_authenticated(): "children": [], "created_at": level2_2.created_at.isoformat().replace("+00:00", "Z"), "creator": { + "id": str(level2_2.creator.id), "full_name": level2_2.creator.full_name, "short_name": level2_2.creator.short_name, },