(teams) add treebeard data to serializers

This will allow the frontend to represent teams as a
tree if needed.
This commit is contained in:
Quentin BEY
2025-01-07 15:45:37 +01:00
committed by BEY Quentin
parent 45fd10fd2d
commit 4ccea4655b
8 changed files with 66 additions and 15 deletions

View File

@@ -259,18 +259,24 @@ class TeamSerializer(serializers.ModelSerializer):
model = models.Team
fields = [
"id",
"name",
"accesses",
"abilities",
"accesses",
"created_at",
"updated_at",
"depth",
"name",
"numchild",
"path",
"service_providers",
"updated_at",
]
read_only_fields = [
"id",
"accesses",
"abilities",
"accesses",
"created_at",
"depth",
"numchild",
"path",
"updated_at",
]

View File

@@ -12,13 +12,19 @@ class TeamSerializer(serializers.ModelSerializer):
model = models.Team
fields = [
"id",
"name",
"created_at",
"depth",
"name",
"numchild",
"path",
"updated_at",
]
read_only_fields = [
"id",
"created_at",
"depth",
"numchild",
"path",
"updated_at",
]

View File

@@ -59,7 +59,10 @@ def test_api_teams_create_authenticated_new_service_provider(
assert response.json() == {
"created_at": team.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"id": str(team.pk),
"depth": team.depth,
"name": "my team",
"numchild": team.numchild,
"path": team.path,
"updated_at": team.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
}

View File

@@ -80,26 +80,38 @@ def test_api_teams_list_authenticated( # pylint: disable=too-many-locals
"results": [
{
"created_at": team_1.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": team_1.depth,
"id": str(team_1.pk),
"name": team_1.name,
"numchild": team_1.numchild,
"path": team_1.path,
"updated_at": team_1.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},
{
"created_at": team_2.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": team_2.depth,
"id": str(team_2.pk),
"name": team_2.name,
"numchild": team_2.numchild,
"path": team_2.path,
"updated_at": team_2.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},
{
"created_at": team_3.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": team_3.depth,
"id": str(team_3.pk),
"name": team_3.name,
"numchild": team_3.numchild,
"path": team_3.path,
"updated_at": team_3.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},
{
"created_at": team_4.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": team_4.depth,
"id": str(team_4.pk),
"name": team_4.name,
"numchild": team_4.numchild,
"path": team_4.path,
"updated_at": team_4.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},
],

View File

@@ -66,9 +66,12 @@ def test_api_teams_retrieve_authenticated_related(
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"created_at": team.created_at.isoformat().replace("+00:00", "Z"),
"depth": 1,
"id": str(team.id),
"name": team.name,
"created_at": team.created_at.isoformat().replace("+00:00", "Z"),
"numchild": 0,
"path": team.path,
"updated_at": team.updated_at.isoformat().replace("+00:00", "Z"),
}
@@ -137,9 +140,12 @@ def test_api_teams_retrieve_authenticated_related_parent_same_organization(
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"created_at": first_team.created_at.isoformat().replace("+00:00", "Z"),
"depth": 2,
"id": str(first_team.pk),
"name": first_team.name,
"created_at": first_team.created_at.isoformat().replace("+00:00", "Z"),
"numchild": 1,
"path": first_team.path,
"updated_at": first_team.updated_at.isoformat().replace("+00:00", "Z"),
}

View File

@@ -190,8 +190,11 @@ def test_api_teams_list_authenticated_team_tree(client, role, local_team_abiliti
"abilities": local_team_abilities,
"accesses": [str(user_access.pk)],
"created_at": second_team.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": 3,
"id": str(second_team.pk),
"name": "Second",
"numchild": 1,
"path": second_team.path,
"service_providers": [],
"updated_at": second_team.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},
@@ -206,8 +209,11 @@ def test_api_teams_list_authenticated_team_tree(client, role, local_team_abiliti
},
"accesses": [],
"created_at": first_team.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": 2,
"id": str(first_team.pk),
"name": "First",
"numchild": 1,
"path": first_team.path,
"service_providers": [],
"updated_at": first_team.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},
@@ -222,8 +228,11 @@ def test_api_teams_list_authenticated_team_tree(client, role, local_team_abiliti
},
"accesses": [],
"created_at": root_team.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": 1,
"id": str(root_team.pk),
"name": "Root",
"numchild": 1,
"path": root_team.path,
"service_providers": [],
"updated_at": root_team.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},
@@ -305,8 +314,11 @@ def test_api_teams_list_authenticated_team_different_organization(
"abilities": local_team_abilities,
"accesses": [str(user_access.pk)],
"created_at": second_team.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"depth": 3,
"id": str(second_team.pk),
"name": "Second",
"numchild": 1,
"path": second_team.path,
"service_providers": [],
"updated_at": second_team.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
},

View File

@@ -67,12 +67,15 @@ def test_api_teams_retrieve_authenticated_related():
]
)
assert response.json() == {
"id": str(team.id),
"name": team.name,
"abilities": team.get_abilities(user),
"created_at": team.created_at.isoformat().replace("+00:00", "Z"),
"updated_at": team.updated_at.isoformat().replace("+00:00", "Z"),
"depth": 1,
"id": str(team.id),
"name": team.name,
"numchild": 0,
"path": team.path,
"service_providers": [],
"updated_at": team.updated_at.isoformat().replace("+00:00", "Z"),
}
@@ -104,13 +107,16 @@ def test_api_teams_retrieve_authenticated_related_parent(client, role):
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"id": str(first_team.pk),
"name": first_team.name,
"abilities": abilities,
"accesses": [],
"created_at": first_team.created_at.isoformat().replace("+00:00", "Z"),
"updated_at": first_team.updated_at.isoformat().replace("+00:00", "Z"),
"depth": 2,
"id": str(first_team.pk),
"name": first_team.name,
"numchild": 1,
"path": first_team.path,
"service_providers": [],
"updated_at": first_team.updated_at.isoformat().replace("+00:00", "Z"),
}

View File

@@ -119,7 +119,7 @@ def test_api_teams_update_authenticated_administrators():
team.refresh_from_db()
final_values = serializers.TeamSerializer(instance=team).data
for key, value in final_values.items():
if key in ["id", "accesses", "created_at"]:
if key in ["id", "accesses", "created_at", "depth", "path", "numchild"]:
assert value == initial_values[key]
elif key == "updated_at":
assert value > initial_values[key]
@@ -152,7 +152,7 @@ def test_api_teams_update_authenticated_owners():
team.refresh_from_db()
team_values = serializers.TeamSerializer(instance=team).data
for key, value in team_values.items():
if key in ["id", "accesses", "created_at"]:
if key in ["id", "accesses", "created_at", "depth", "path", "numchild"]:
assert value == old_team_values[key]
elif key == "updated_at":
assert value > old_team_values[key]