Improve name uniqueness feedback

This commit is contained in:
Tobias Kunze 2025-04-03 17:54:31 +02:00
parent a2ac202f26
commit fb5a6e9a42
2 changed files with 17 additions and 9 deletions

View file

@ -3,7 +3,7 @@ import urlman
from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.db import models
from django.db import IntegrityError, models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from encrypted_fields.fields import EncryptedJSONField
@ -492,6 +492,19 @@ class ServiceInstance(ServalaModelMixin, models.Model):
def create_instance(cls, name, organization, context, created_by, spec_data):
# Ensure the namespace exists
context.control_plane.get_or_create_namespace(organization.namespace)
try:
instance = cls.objects.create(
name=name,
organization=organization,
created_by=created_by,
context=context,
)
except IntegrityError:
raise ValidationError(
_(
"An instance with this name already exists in this organization. Please choose a different name."
)
)
group = context.service_definition.api_definition["group"]
version = context.service_definition.api_definition["version"]
@ -521,10 +534,4 @@ class ServiceInstance(ServalaModelMixin, models.Model):
plural=plural,
body=create_data,
)
return cls.objects.create(
name=name,
organization=organization,
created_by=created_by,
context=context,
)
return instance

View file

@ -135,7 +135,8 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
)
return redirect(service_instance.urls.base)
except Exception as e:
messages.error(self.request, str(e))
error_message = getattr(e, "message", None) or str(e)
messages.error(self.request, error_message)
# If the form is not valid or if the service creation failed, we render it again
context["service_form"] = form