Skip to content

Commit

Permalink
[AAP-23882] - Creating a new Team with the same name should not propa…
Browse files Browse the repository at this point in the history
…gate IntegrityError
  • Loading branch information
msmagnanijr committed Jun 11, 2024
1 parent 3572081 commit fcaba74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/aap_eda/api/serializers/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/aap_eda/core/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."]}
)

0 comments on commit fcaba74

Please sign in to comment.