From 892f6f93dff7e00714152d8a94bb1ce02bc00dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Tue, 24 Feb 2015 09:29:31 +0100 Subject: [PATCH] US #55: Custom fields - Fix validations --- .../projects/custom_attributes/serializers.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/taiga/projects/custom_attributes/serializers.py b/taiga/projects/custom_attributes/serializers.py index b52a15be..641c1996 100644 --- a/taiga/projects/custom_attributes/serializers.py +++ b/taiga/projects/custom_attributes/serializers.py @@ -34,16 +34,16 @@ class BaseCustomAttributeSerializer(ModelSerializer): class Meta: read_only_fields = ('id', 'created_date', 'modified_date') - def validate(self, data): + def _validate_integrity_between_project_and_name(self, attrs, source): """ Check the name is not duplicated in the project. Check when: - create a new one - update the name - update the project (move to another project) """ - data_id = data.get("id", None) - data_name = data.get("name", None) - data_project = data.get("project", None) + data_id = attrs.get("id", None) + data_name = attrs.get("name", None) + data_project = attrs.get("project", None) if self.object: data_id = data_id or self.object.id @@ -54,9 +54,15 @@ class BaseCustomAttributeSerializer(ModelSerializer): qs = (model.objects.filter(project=data_project, name=data_name) .exclude(id=data_id)) if qs.exists(): - raise ValidationError(_("There is a custom field with the same name in this project.")) + raise ValidationError(_("Already exists one with the same name.")) - return data + return attrs + + def validate_name(self, attrs, source): + return self._validate_integrity_between_project_and_name(attrs, source) + + def validate_project(self, attrs, source): + return self._validate_integrity_between_project_and_name(attrs, source) class UserStoryCustomAttributeSerializer(BaseCustomAttributeSerializer):