Add order_alphanetically boolean for choice sets

This commit is contained in:
Jeremy Stretch 2023-07-17 14:20:55 -04:00
parent 9f3ffb70a7
commit 9aff7e7103
9 changed files with 28 additions and 5 deletions

View File

@ -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',
]

View File

@ -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):

View File

@ -65,6 +65,10 @@ class CustomFieldChoiceSetBulkEditForm(BulkEditForm):
description = forms.CharField(
required=False
)
order_alphabetically = forms.NullBooleanField(
required=False,
widget=BulkEditNullBooleanSelect()
)
nullable_fields = ('description',)

View File

@ -72,7 +72,7 @@ class CustomFieldChoiceSetImportForm(CSVModelForm):
class Meta:
model = CustomFieldChoiceSet
fields = (
'name', 'description', 'extra_choices',
'name', 'description', 'extra_choices', 'order_alphabetically',
)

View File

@ -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):

View File

@ -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',),

View File

@ -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)

View File

@ -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')

View File

@ -17,6 +17,10 @@
<th scope="row">Description</th>
<td>{{ object.description|markdown|placeholder }}</td>
</tr>
<tr>
<th scope="row">Order Alphabetically</th>
<td>{% checkmark object.order_alphabetically %}</td>
</tr>
<tr>
<th scope="row">Used by</th>
<td>{# TODO #}</td>