From 9aff7e710330418df2ca9fd747f64218f74a6cc2 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 17 Jul 2023 14:20:55 -0400 Subject: [PATCH] Add order_alphanetically boolean for choice sets --- netbox/extras/api/serializers.py | 3 ++- netbox/extras/filtersets.py | 2 +- netbox/extras/forms/bulk_edit.py | 4 ++++ netbox/extras/forms/bulk_import.py | 2 +- netbox/extras/forms/model_forms.py | 2 +- .../extras/migrations/0096_customfieldchoiceset.py | 1 + netbox/extras/models/customfields.py | 12 ++++++++++++ netbox/extras/tables/tables.py | 3 ++- netbox/templates/extras/customfieldchoiceset.html | 4 ++++ 9 files changed, 28 insertions(+), 5 deletions(-) diff --git a/netbox/extras/api/serializers.py b/netbox/extras/api/serializers.py index fc5f44358..fa6ce1545 100644 --- a/netbox/extras/api/serializers.py +++ b/netbox/extras/api/serializers.py @@ -135,7 +135,8 @@ class CustomFieldChoiceSetSerializer(ValidatedModelSerializer): class Meta: model = CustomFieldChoiceSet fields = [ - 'id', 'url', 'display', 'name', 'description', 'extra_choices', 'created', 'last_updated', + 'id', 'url', 'display', 'name', 'description', 'extra_choices', 'order_alphabetically', 'created', + 'last_updated', ] diff --git a/netbox/extras/filtersets.py b/netbox/extras/filtersets.py index b4a2a25e7..dd34c9ae4 100644 --- a/netbox/extras/filtersets.py +++ b/netbox/extras/filtersets.py @@ -109,7 +109,7 @@ class CustomFieldChoiceSetFilterSet(BaseFilterSet): class Meta: model = CustomFieldChoiceSet fields = [ - 'id', 'name', 'description', + 'id', 'name', 'description', 'order_alphabetically', ] def search(self, queryset, name, value): diff --git a/netbox/extras/forms/bulk_edit.py b/netbox/extras/forms/bulk_edit.py index 9a9844c88..b0c6b87ea 100644 --- a/netbox/extras/forms/bulk_edit.py +++ b/netbox/extras/forms/bulk_edit.py @@ -65,6 +65,10 @@ class CustomFieldChoiceSetBulkEditForm(BulkEditForm): description = forms.CharField( required=False ) + order_alphabetically = forms.NullBooleanField( + required=False, + widget=BulkEditNullBooleanSelect() + ) nullable_fields = ('description',) diff --git a/netbox/extras/forms/bulk_import.py b/netbox/extras/forms/bulk_import.py index ed5d82e35..b47fcba60 100644 --- a/netbox/extras/forms/bulk_import.py +++ b/netbox/extras/forms/bulk_import.py @@ -72,7 +72,7 @@ class CustomFieldChoiceSetImportForm(CSVModelForm): class Meta: model = CustomFieldChoiceSet fields = ( - 'name', 'description', 'extra_choices', + 'name', 'description', 'extra_choices', 'order_alphabetically', ) diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index 472080d50..0a1215370 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -87,7 +87,7 @@ class CustomFieldChoiceSetForm(BootstrapMixin, forms.ModelForm): class Meta: model = CustomFieldChoiceSet - fields = ('name', 'description', 'extra_choices') + fields = ('name', 'description', 'extra_choices', 'order_alphabetically') class CustomLinkForm(BootstrapMixin, forms.ModelForm): diff --git a/netbox/extras/migrations/0096_customfieldchoiceset.py b/netbox/extras/migrations/0096_customfieldchoiceset.py index 8b600410a..a1c02d994 100644 --- a/netbox/extras/migrations/0096_customfieldchoiceset.py +++ b/netbox/extras/migrations/0096_customfieldchoiceset.py @@ -43,6 +43,7 @@ class Migration(migrations.Migration): ('name', models.CharField(max_length=100, unique=True)), ('description', models.CharField(blank=True, max_length=200)), ('extra_choices', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=100), size=None)), + ('order_alphabetically', models.BooleanField(default=False)), ], options={ 'ordering': ('name',), diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index d24a45374..f706a649b 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -649,6 +649,10 @@ class CustomFieldChoiceSet(ChangeLoggedModel): base_field=models.CharField(max_length=100), help_text=_('Comma-separated list of available choices (for selection fields)') ) + order_alphabetically = models.BooleanField( + default=False, + help_text=_('Choices are automatically ordered alphabetically on save') + ) class Meta: ordering = ('name',) @@ -662,3 +666,11 @@ class CustomFieldChoiceSet(ChangeLoggedModel): @property def choices(self): return self.extra_choices + + def save(self, *args, **kwargs): + + # Sort choices if alphabetical ordering is enforced + if self.order_alphabetically: + self.extra_choices = sorted(self.choices) + + return super().save(*args, **kwargs) diff --git a/netbox/extras/tables/tables.py b/netbox/extras/tables/tables.py index a0cb82a90..b12d4cbbb 100644 --- a/netbox/extras/tables/tables.py +++ b/netbox/extras/tables/tables.py @@ -98,7 +98,8 @@ class CustomFieldChoiceSetTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = CustomFieldChoiceSet fields = ( - 'pk', 'id', 'name', 'description', 'choice_count', 'choices', 'extra_choices', 'created', 'last_updated', + 'pk', 'id', 'name', 'description', 'choice_count', 'choices', 'extra_choices', 'order_alphabetically', + 'created', 'last_updated', ) default_columns = ('pk', 'name', 'choice_count', 'description') diff --git a/netbox/templates/extras/customfieldchoiceset.html b/netbox/templates/extras/customfieldchoiceset.html index f2df0f67f..1bf1b0a1d 100644 --- a/netbox/templates/extras/customfieldchoiceset.html +++ b/netbox/templates/extras/customfieldchoiceset.html @@ -17,6 +17,10 @@ Description {{ object.description|markdown|placeholder }} + + Order Alphabetically + {% checkmark object.order_alphabetically %} + Used by {# TODO #}