Skip to content

Commit

Permalink
#8198: Rename CustomField.validation_unique to unique (#17325)
Browse files Browse the repository at this point in the history
* #8198: Rename CustomField.validation_unique to unique

* Update CustomField model documentation
  • Loading branch information
jeremystretch authored Aug 30, 2024
1 parent 8fff4e2 commit b4dd57f
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 62 deletions.
10 changes: 5 additions & 5 deletions docs/models/extras/customfield.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ A numeric weight used to override alphabetic ordering of fields by name. Custom

### Required

If checked, this custom field must be populated with a valid value for the object to pass validation.
If enabled, this custom field must be populated with a valid value for the object to pass validation.

### Unique

If enabled, each object must have a unique value set for this custom field (per object type).

### Description

Expand Down Expand Up @@ -116,7 +120,3 @@ For numeric custom fields only. The maximum valid value (optional).
### Validation Regex

For string-based custom fields only. A regular expression used to validate the field's value (optional).

### Uniqueness Validation

If enabled, each object must have a unique value set for this custom field (per object type).
7 changes: 4 additions & 3 deletions netbox/extras/api/serializers_/customfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ class Meta:
model = CustomField
fields = [
'id', 'url', 'display_url', 'display', 'object_types', 'type', 'related_object_type', 'data_type',
'name', 'label', 'group_name', 'description', 'required', 'search_weight', 'filter_logic', 'ui_visible',
'ui_editable', 'is_cloneable', 'default', 'related_object_filter', 'weight', 'validation_minimum', 'validation_maximum',
'validation_regex', 'validation_unique', 'choice_set', 'comments', 'created', 'last_updated',
'name', 'label', 'group_name', 'description', 'required', 'unique', 'search_weight', 'filter_logic',
'ui_visible', 'ui_editable', 'is_cloneable', 'default', 'related_object_filter', 'weight',
'validation_minimum', 'validation_maximum', 'validation_regex', 'choice_set', 'comments', 'created',
'last_updated',
]
brief_fields = ('id', 'url', 'display', 'name', 'description')

Expand Down
4 changes: 2 additions & 2 deletions netbox/extras/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ class CustomFieldFilterSet(ChangeLoggedModelFilterSet):
class Meta:
model = CustomField
fields = (
'id', 'name', 'label', 'group_name', 'required', 'search_weight', 'filter_logic', 'ui_visible',
'id', 'name', 'label', 'group_name', 'required', 'unique', 'search_weight', 'filter_logic', 'ui_visible',
'ui_editable', 'weight', 'is_cloneable', 'description', 'validation_minimum', 'validation_maximum',
'validation_regex', 'validation_unique',
'validation_regex',
)

def search(self, queryset, name, value):
Expand Down
16 changes: 7 additions & 9 deletions netbox/extras/forms/bulk_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class CustomFieldBulkEditForm(BulkEditForm):
required=False,
widget=BulkEditNullBooleanSelect()
)
unique = forms.NullBooleanField(
label=_('Must be unique'),
required=False,
widget=BulkEditNullBooleanSelect()
)
weight = forms.IntegerField(
label=_('Weight'),
required=False
Expand Down Expand Up @@ -79,19 +84,12 @@ class CustomFieldBulkEditForm(BulkEditForm):
label=_('Validation regex'),
required=False
)
validation_unique = forms.NullBooleanField(
label=_('Must be unique'),
required=False,
widget=BulkEditNullBooleanSelect()
)
comments = CommentField()

fieldsets = (
FieldSet('group_name', 'description', 'weight', 'choice_set', name=_('Attributes')),
FieldSet('group_name', 'description', 'weight', 'required', 'unique', 'choice_set', name=_('Attributes')),
FieldSet('ui_visible', 'ui_editable', 'is_cloneable', name=_('Behavior')),
FieldSet(
'validation_minimum', 'validation_maximum', 'validation_regex', 'validation_unique', name=_('Validation')
),
FieldSet('validation_minimum', 'validation_maximum', 'validation_regex', name=_('Validation')),
)
nullable_fields = ('group_name', 'description', 'choice_set')

Expand Down
7 changes: 3 additions & 4 deletions netbox/extras/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ class CustomFieldImportForm(CSVModelForm):
class Meta:
model = CustomField
fields = (
'name', 'label', 'group_name', 'type', 'object_types', 'related_object_type', 'required', 'description',
'search_weight', 'filter_logic', 'default', 'choice_set', 'weight', 'validation_minimum',
'validation_maximum', 'validation_regex', 'validation_unique', 'ui_visible', 'ui_editable', 'is_cloneable',
'comments',
'name', 'label', 'group_name', 'type', 'object_types', 'related_object_type', 'required', 'unique',
'description', 'search_weight', 'filter_logic', 'default', 'choice_set', 'weight', 'validation_minimum',
'validation_maximum', 'validation_regex', 'ui_visible', 'ui_editable', 'is_cloneable', 'comments',
)


Expand Down
23 changes: 11 additions & 12 deletions netbox/extras/forms/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ class CustomFieldFilterForm(SavedFiltersMixin, FilterForm):
fieldsets = (
FieldSet('q', 'filter_id'),
FieldSet(
'type', 'related_object_type_id', 'group_name', 'weight', 'required', 'choice_set_id', 'ui_visible',
'ui_editable', 'is_cloneable', name=_('Attributes')
),
FieldSet(
'validation_minimum', 'validation_maximum', 'validation_regex', 'validation_unique', name=_('Validation')
'type', 'related_object_type_id', 'group_name', 'weight', 'required', 'unique', 'choice_set_id',
name=_('Attributes')
),
FieldSet('ui_visible', 'ui_editable', 'is_cloneable', name=_('Behavior')),
FieldSet('validation_minimum', 'validation_maximum', 'validation_regex', name=_('Validation')),
)
related_object_type_id = ContentTypeMultipleChoiceField(
queryset=ObjectType.objects.with_feature('custom_fields'),
Expand All @@ -72,6 +71,13 @@ class CustomFieldFilterForm(SavedFiltersMixin, FilterForm):
choices=BOOLEAN_WITH_BLANK_CHOICES
)
)
unique = forms.NullBooleanField(
label=_('Must be unique'),
required=False,
widget=forms.Select(
choices=BOOLEAN_WITH_BLANK_CHOICES
)
)
choice_set_id = DynamicModelMultipleChoiceField(
queryset=CustomFieldChoiceSet.objects.all(),
required=False,
Expand Down Expand Up @@ -106,13 +112,6 @@ class CustomFieldFilterForm(SavedFiltersMixin, FilterForm):
label=_('Validation regex'),
required=False
)
validation_unique = forms.NullBooleanField(
label=_('Must be unique'),
required=False,
widget=forms.Select(
choices=BOOLEAN_WITH_BLANK_CHOICES
)
)


class CustomFieldChoiceSetFilterForm(SavedFiltersMixin, FilterForm):
Expand Down
4 changes: 2 additions & 2 deletions netbox/extras/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class CustomFieldForm(forms.ModelForm):

fieldsets = (
FieldSet(
'object_types', 'name', 'label', 'group_name', 'description', 'type', 'required', 'validation_unique',
'default', name=_('Custom Field')
'object_types', 'name', 'label', 'group_name', 'description', 'type', 'required', 'unique', 'default',
name=_('Custom Field')
),
FieldSet(
'search_weight', 'filter_logic', 'ui_visible', 'ui_editable', 'weight', 'is_cloneable', name=_('Behavior')
Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/migrations/0118_customfield_uniqueness.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Migration(migrations.Migration):
operations = [
migrations.AddField(
model_name='customfield',
name='validation_unique',
name='unique',
field=models.BooleanField(default=False),
),
]
22 changes: 11 additions & 11 deletions netbox/extras/models/customfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
required = models.BooleanField(
verbose_name=_('required'),
default=False,
help_text=_("If true, this field is required when creating new objects or editing an existing object.")
help_text=_("This field is required when creating new objects or editing an existing object.")
)
unique = models.BooleanField(
verbose_name=_('must be unique'),
default=False,
help_text=_("The value of this field must be unique for the assigned object")
)
search_weight = models.PositiveSmallIntegerField(
verbose_name=_('search weight'),
Expand Down Expand Up @@ -189,11 +194,6 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
'example, <code>^[A-Z]{3}$</code> will limit values to exactly three uppercase letters.'
)
)
validation_unique = models.BooleanField(
verbose_name=_('must be unique'),
default=False,
help_text=_('The value of this field must be unique for the assigned object')
)
choice_set = models.ForeignKey(
to='CustomFieldChoiceSet',
on_delete=models.PROTECT,
Expand Down Expand Up @@ -229,9 +229,9 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
objects = CustomFieldManager()

clone_fields = (
'object_types', 'type', 'related_object_type', 'group_name', 'description', 'required', 'search_weight',
'filter_logic', 'default', 'weight', 'validation_minimum', 'validation_maximum', 'validation_regex',
'validation_unique', 'choice_set', 'ui_visible', 'ui_editable', 'is_cloneable',
'object_types', 'type', 'related_object_type', 'group_name', 'description', 'required', 'unique',
'search_weight', 'filter_logic', 'default', 'weight', 'validation_minimum', 'validation_maximum',
'validation_regex', 'choice_set', 'ui_visible', 'ui_editable', 'is_cloneable',
)

class Meta:
Expand Down Expand Up @@ -349,9 +349,9 @@ def clean(self):
})

# Uniqueness can not be enforced for boolean fields
if self.validation_unique and self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
if self.unique and self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
raise ValidationError({
'validation_unique': _("Uniqueness cannot be enforced for boolean fields")
'unique': _("Uniqueness cannot be enforced for boolean fields")
})

# Choice set must be set on selection fields, and *only* on selection fields
Expand Down
17 changes: 10 additions & 7 deletions netbox/extras/tables/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class CustomFieldTable(NetBoxTable):
verbose_name=_('Required'),
false_mark=None
)
unique = columns.BooleanColumn(
verbose_name=_('Validate Uniqueness'),
false_mark=None
)
ui_visible = columns.ChoiceFieldColumn(
verbose_name=_('Visible')
)
Expand Down Expand Up @@ -99,19 +103,18 @@ class CustomFieldTable(NetBoxTable):
validation_regex = tables.Column(
verbose_name=_('Validation Regex'),
)
validation_unique = columns.BooleanColumn(
verbose_name=_('Validate Uniqueness'),
)

class Meta(NetBoxTable.Meta):
model = CustomField
fields = (
'pk', 'id', 'name', 'object_types', 'label', 'type', 'related_object_type', 'group_name', 'required',
'default', 'description', 'search_weight', 'filter_logic', 'ui_visible', 'ui_editable', 'is_cloneable',
'weight', 'choice_set', 'choices', 'validation_minimum', 'validation_maximum', 'validation_regex',
'validation_unique', 'comments', 'created', 'last_updated',
'unique', 'default', 'description', 'search_weight', 'filter_logic', 'ui_visible', 'ui_editable',
'is_cloneable', 'weight', 'choice_set', 'choices', 'validation_minimum', 'validation_maximum',
'validation_regex', 'comments', 'created', 'last_updated',
)
default_columns = (
'pk', 'name', 'object_types', 'label', 'group_name', 'type', 'required', 'unique', 'description',
)
default_columns = ('pk', 'name', 'object_types', 'label', 'group_name', 'type', 'required', 'description')


class CustomFieldChoiceSetTable(NetBoxTable):
Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/tests/test_customfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ def test_regex_validation(self):
def test_uniqueness_validation(self):
# Create a unique custom field
cf_text = CustomField.objects.get(name='text_field')
cf_text.validation_unique = True
cf_text.unique = True
cf_text.save()

# Set a value on site 1
Expand Down
2 changes: 1 addition & 1 deletion netbox/netbox/models/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def clean(self):
))

# Validate uniqueness if enforced
if custom_fields[field_name].validation_unique and value not in CUSTOMFIELD_EMPTY_VALUES:
if custom_fields[field_name].unique and value not in CUSTOMFIELD_EMPTY_VALUES:
if self._meta.model.objects.exclude(pk=self.pk).filter(**{
f'custom_field_data__{field_name}': value
}).exists():
Expand Down
8 changes: 4 additions & 4 deletions netbox/templates/extras/customfield.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ <h2 class="card-header">{% trans "Custom Field" %}</h2>
<th scope="row">{% trans "Required" %}</th>
<td>{% checkmark object.required %}</td>
</tr>
<tr>
<th scope="row">{% trans "Must be Unique" %}</th>
<td>{% checkmark object.unique %}</td>
</tr>
<tr>
<th scope="row">{% trans "Cloneable" %}</th>
<td>{% checkmark object.is_cloneable %}</td>
Expand Down Expand Up @@ -128,10 +132,6 @@ <h2 class="card-header">{% trans "Validation Rules" %}</h2>
{% endif %}
</td>
</tr>
<tr>
<th scope="row">{% trans "Must be Unique" %}</th>
<td>{% checkmark object.validation_unique %}</td>
</tr>
</table>
</div>
<div class="card">
Expand Down

0 comments on commit b4dd57f

Please sign in to comment.