Adding name validation to task status serializer and issue status serializer

remotes/origin/enhancement/email-actions
Alejandro Alonso 2014-11-06 14:02:29 +01:00 committed by David Barragán Merino
parent 2e78d51eda
commit 05bf0cab45
1 changed files with 33 additions and 0 deletions

View File

@ -68,6 +68,22 @@ class TaskStatusSerializer(ModelSerializer):
class Meta: class Meta:
model = models.TaskStatus model = models.TaskStatus
def validate_name(self, attrs, source):
"""
Check the task 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.TaskStatus.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.TaskStatus.objects.filter(project=attrs["project"], name=attrs[source])
if qs and qs.exists():
raise serializers.ValidationError("Name duplicated for the project")
return attrs
# Issues common serializers # Issues common serializers
@ -85,6 +101,23 @@ class IssueStatusSerializer(ModelSerializer):
class Meta: class Meta:
model = models.IssueStatus model = models.IssueStatus
def validate_name(self, attrs, source):
"""
Check the issue 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.IssueStatus.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.IssueStatus.objects.filter(project=attrs["project"], name=attrs[source])
if qs and qs.exists():
raise serializers.ValidationError("Name duplicated for the project")
return attrs
class IssueTypeSerializer(ModelSerializer): class IssueTypeSerializer(ModelSerializer):
class Meta: class Meta: