Fixing error 500 when using duplicated names for project points
parent
57777fc54c
commit
4996ae3a24
|
@ -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:
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue