|
12 | 12 | # from django.contrib.contenttypes.management import create_contenttypes |
13 | 13 | from django.contrib.contenttypes.models import ContentType |
14 | 14 | from django.core.validators import RegexValidator, ValidationError |
15 | | -from django.db import connection, models, IntegrityError |
| 15 | +from django.db import connection, models |
16 | 16 | from django.db.models import Q |
17 | 17 | from django.db.models.functions import Lower |
18 | 18 | from django.db.models.signals import pre_delete |
|
52 | 52 | from netbox_custom_objects.constants import APP_LABEL, RESERVED_FIELD_NAMES |
53 | 53 | from netbox_custom_objects.field_types import FIELD_TYPE_CLASS |
54 | 54 |
|
55 | | - |
56 | | -class UniquenessConstraintTestError(Exception): |
57 | | - """Custom exception used to signal successful uniqueness constraint test.""" |
58 | | - |
59 | | - pass |
60 | | - |
61 | | - |
62 | 55 | USER_TABLE_DATABASE_NAME_PREFIX = "custom_objects_" |
63 | 56 |
|
64 | 57 |
|
@@ -317,10 +310,7 @@ def _fetch_and_generate_field_attrs( |
317 | 310 | for field in fields: |
318 | 311 | field_type = FIELD_TYPE_CLASS[field.type]() |
319 | 312 | 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]: |
324 | 314 | continue |
325 | 315 |
|
326 | 316 | field_name = field.name |
@@ -463,9 +453,7 @@ def get_model( |
463 | 453 | "custom_object_type_id": self.id, |
464 | 454 | } |
465 | 455 |
|
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) |
469 | 457 |
|
470 | 458 | attrs.update(**field_attrs) |
471 | 459 |
|
@@ -599,7 +587,7 @@ class CustomObjectTypeField(CloningMixin, ExportTemplatesMixin, ChangeLoggedMode |
599 | 587 | name = models.CharField( |
600 | 588 | verbose_name=_("name"), |
601 | 589 | max_length=50, |
602 | | - help_text=_('Internal field name, e.g. "vendor_label"'), |
| 590 | + help_text=_("Internal field name, e.g. \"vendor_label\""), |
603 | 591 | validators=( |
604 | 592 | RegexValidator( |
605 | 593 | regex=r"^[a-z0-9_]+$", |
@@ -628,9 +616,7 @@ class CustomObjectTypeField(CloningMixin, ExportTemplatesMixin, ChangeLoggedMode |
628 | 616 | verbose_name=_("group name"), |
629 | 617 | max_length=50, |
630 | 618 | 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"), |
634 | 620 | ) |
635 | 621 | description = models.CharField( |
636 | 622 | verbose_name=_("description"), max_length=200, blank=True |
@@ -886,40 +872,6 @@ def clean(self): |
886 | 872 | {"unique": _("Uniqueness cannot be enforced for boolean fields")} |
887 | 873 | ) |
888 | 874 |
|
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 | | - |
923 | 875 | # Choice set must be set on selection fields, and *only* on selection fields |
924 | 876 | if self.type in ( |
925 | 877 | CustomFieldTypeChoices.TYPE_SELECT, |
|
0 commit comments