Fixing validator for milestone creation

remotes/origin/enhancement/email-actions
Alejandro Alonso 2014-07-07 11:05:16 +02:00
parent 9b8531d562
commit 9c5bb7ff55
1 changed files with 11 additions and 4 deletions

View File

@ -47,10 +47,17 @@ class MilestoneSerializer(serializers.ModelSerializer):
def validate_name(self, attrs, source):
"""
Check the milestone name is not duplicated in the project
Check the milestone name is not duplicated in the project on creation
"""
qs = models.Milestone.objects.filter(project=attrs["project"], name=attrs[source])
if qs.exists():
raise serializers.ValidationError("Name duplicated for the project")
qs = None
# If the milestone exists:
if self.object and attrs.get("name", None):
qs = models.Milestone.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.Milestone.objects.filter(project=attrs["project"], name=attrs[source])
if qs and qs.exists():
raise serializers.ValidationError("Name duplicated for the project")
return attrs