diff --git a/taiga/projects/serializers.py b/taiga/projects/serializers.py index a06f9a15..add42979 100644 --- a/taiga/projects/serializers.py +++ b/taiga/projects/serializers.py @@ -39,6 +39,23 @@ class PointsSerializer(ModelSerializer): class Meta: model = models.Points + def validate_name(self, attrs, source): + """ + Check the points name is not duplicated in the project on creation + """ + qs = None + # If the user story status exists: + if self.object and attrs.get("name", None): + qs = models.Points.objects.filter(project=self.object.project, name=attrs[source]) + + if not self.object and attrs.get("project", None) and attrs.get("name", None): + qs = models.Points.objects.filter(project=attrs["project"], name=attrs[source]) + + if qs and qs.exists(): + raise serializers.ValidationError("Name duplicated for the project") + + return attrs + class UserStoryStatusSerializer(ModelSerializer): class Meta: @@ -117,7 +134,7 @@ class IssueStatusSerializer(ModelSerializer): raise serializers.ValidationError("Name duplicated for the project") return attrs - + class IssueTypeSerializer(ModelSerializer): class Meta: diff --git a/tests/integration/test_projects.py b/tests/integration/test_projects.py index e268f1fe..6ad65fbe 100644 --- a/tests/integration/test_projects.py +++ b/tests/integration/test_projects.py @@ -83,3 +83,16 @@ def test_issue_status_slug_generation(client): response = client.json.patch(url, json.dumps(data)) assert response.status_code == 200 assert response.data["slug"] == "new-status" + + +def test_points_name_duplicated(client): + point_1 = f.PointsFactory() + point_2 = f.PointsFactory(project=point_1.project) + + client.login(point_1.project.owner) + + url = reverse("points-detail", kwargs={"pk": point_2.pk}) + data = {"name": point_1.name} + response = client.json.patch(url, json.dumps(data)) + assert response.status_code == 400 + assert response.data["name"][0] == "Name duplicated for the project"