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.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.core.exceptions import ValidationError 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.functional import cached_property
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from encrypted_fields.fields import EncryptedJSONField 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): def create_instance(cls, name, organization, context, created_by, spec_data):
# Ensure the namespace exists # Ensure the namespace exists
context.control_plane.get_or_create_namespace(organization.namespace) 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"] group = context.service_definition.api_definition["group"]
version = context.service_definition.api_definition["version"] version = context.service_definition.api_definition["version"]
@ -521,10 +534,4 @@ class ServiceInstance(ServalaModelMixin, models.Model):
plural=plural, plural=plural,
body=create_data, body=create_data,
) )
return instance
return cls.objects.create(
name=name,
organization=organization,
created_by=created_by,
context=context,
)

View file

@ -135,7 +135,8 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
) )
return redirect(service_instance.urls.base) return redirect(service_instance.urls.base)
except Exception as e: 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 # If the form is not valid or if the service creation failed, we render it again
context["service_form"] = form context["service_form"] = form