diff --git a/src/aap_eda/api/serializers/team.py b/src/aap_eda/api/serializers/team.py index ec8aeebb8..5153b6902 100644 --- a/src/aap_eda/api/serializers/team.py +++ b/src/aap_eda/api/serializers/team.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from rest_framework import serializers +from rest_framework.exceptions import ValidationError from aap_eda.api.serializers.organization import OrganizationRefSerializer from aap_eda.core import models, validators @@ -57,9 +58,9 @@ class Meta: def validate(self, data): self.validate_shared_resource() + validators.check_if_team_name_exists(data['name'], data['organization_id']) return data - class TeamDetailSerializer(serializers.ModelSerializer): organization = OrganizationRefSerializer() resource = AnsibleResourceFieldSerializer(read_only=True) diff --git a/src/aap_eda/core/validators.py b/src/aap_eda/core/validators.py index 2a60dbe43..6d6172df4 100644 --- a/src/aap_eda/core/validators.py +++ b/src/aap_eda/core/validators.py @@ -235,3 +235,9 @@ def check_credential_types( raise serializers.ValidationError( f"The type of credential can only be one of {names}" ) + +def check_if_team_name_exists(name: str, organization_id: int): + if models.Team.objects.filter(name=name, organization_id=organization_id).exists(): + raise serializers.ValidationError( + {"name": ["A team with this name already exists in the organization."]} + )