Skip to content

Commit 1178246

Browse files
authored
Merge pull request #167 from netboxlabs/revert-163-npl-389-unique
Revert "Check if unique flag raise validation error if non unique data"
2 parents 8abaefa + 1f0197b commit 1178246

File tree

1 file changed

+5
-53
lines changed

1 file changed

+5
-53
lines changed

netbox_custom_objects/models.py

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# from django.contrib.contenttypes.management import create_contenttypes
1313
from django.contrib.contenttypes.models import ContentType
1414
from django.core.validators import RegexValidator, ValidationError
15-
from django.db import connection, models, IntegrityError
15+
from django.db import connection, models
1616
from django.db.models import Q
1717
from django.db.models.functions import Lower
1818
from django.db.models.signals import pre_delete
@@ -52,13 +52,6 @@
5252
from netbox_custom_objects.constants import APP_LABEL, RESERVED_FIELD_NAMES
5353
from netbox_custom_objects.field_types import FIELD_TYPE_CLASS
5454

55-
56-
class UniquenessConstraintTestError(Exception):
57-
"""Custom exception used to signal successful uniqueness constraint test."""
58-
59-
pass
60-
61-
6255
USER_TABLE_DATABASE_NAME_PREFIX = "custom_objects_"
6356

6457

@@ -317,10 +310,7 @@ def _fetch_and_generate_field_attrs(
317310
for field in fields:
318311
field_type = FIELD_TYPE_CLASS[field.type]()
319312
if skip_object_fields:
320-
if field.type in [
321-
CustomFieldTypeChoices.TYPE_OBJECT,
322-
CustomFieldTypeChoices.TYPE_MULTIOBJECT,
323-
]:
313+
if field.type in [CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT]:
324314
continue
325315

326316
field_name = field.name
@@ -463,9 +453,7 @@ def get_model(
463453
"custom_object_type_id": self.id,
464454
}
465455

466-
field_attrs = self._fetch_and_generate_field_attrs(
467-
fields, skip_object_fields=skip_object_fields
468-
)
456+
field_attrs = self._fetch_and_generate_field_attrs(fields, skip_object_fields=skip_object_fields)
469457

470458
attrs.update(**field_attrs)
471459

@@ -599,7 +587,7 @@ class CustomObjectTypeField(CloningMixin, ExportTemplatesMixin, ChangeLoggedMode
599587
name = models.CharField(
600588
verbose_name=_("name"),
601589
max_length=50,
602-
help_text=_('Internal field name, e.g. "vendor_label"'),
590+
help_text=_("Internal field name, e.g. \"vendor_label\""),
603591
validators=(
604592
RegexValidator(
605593
regex=r"^[a-z0-9_]+$",
@@ -628,9 +616,7 @@ class CustomObjectTypeField(CloningMixin, ExportTemplatesMixin, ChangeLoggedMode
628616
verbose_name=_("group name"),
629617
max_length=50,
630618
blank=True,
631-
help_text=_(
632-
"Custom object fields within the same group will be displayed together"
633-
),
619+
help_text=_("Custom object fields within the same group will be displayed together"),
634620
)
635621
description = models.CharField(
636622
verbose_name=_("description"), max_length=200, blank=True
@@ -886,40 +872,6 @@ def clean(self):
886872
{"unique": _("Uniqueness cannot be enforced for boolean fields")}
887873
)
888874

889-
# Check if uniqueness constraint can be applied when changing from non-unique to unique
890-
if (
891-
self.pk
892-
and self.unique
893-
and not self.original.unique
894-
and not self._state.adding
895-
):
896-
field_type = FIELD_TYPE_CLASS[self.type]()
897-
model_field = field_type.get_model_field(self)
898-
model = self.custom_object_type.get_model()
899-
model_field.contribute_to_class(model, self.name)
900-
901-
old_field = field_type.get_model_field(self.original)
902-
old_field.contribute_to_class(model, self._original_name)
903-
904-
try:
905-
with connection.schema_editor() as test_schema_editor:
906-
test_schema_editor.alter_field(model, old_field, model_field)
907-
# If we get here, the constraint was applied successfully
908-
# Now raise a custom exception to rollback the test transaction
909-
raise UniquenessConstraintTestError()
910-
except UniquenessConstraintTestError:
911-
# The constraint can be applied, validation passes
912-
pass
913-
except IntegrityError:
914-
# The constraint cannot be applied due to existing non-unique values
915-
raise ValidationError(
916-
{
917-
"unique": _(
918-
"Custom objects with non-unique values already exist so this action isn't permitted"
919-
)
920-
}
921-
)
922-
923875
# Choice set must be set on selection fields, and *only* on selection fields
924876
if self.type in (
925877
CustomFieldTypeChoices.TYPE_SELECT,

0 commit comments

Comments
 (0)