mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 08:46:10 -06:00
Add order_alphanetically boolean for choice sets
This commit is contained in:
parent
9f3ffb70a7
commit
9aff7e7103
@ -135,7 +135,8 @@ class CustomFieldChoiceSetSerializer(ValidatedModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = CustomFieldChoiceSet
|
model = CustomFieldChoiceSet
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'url', 'display', 'name', 'description', 'extra_choices', 'created', 'last_updated',
|
'id', 'url', 'display', 'name', 'description', 'extra_choices', 'order_alphabetically', 'created',
|
||||||
|
'last_updated',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ class CustomFieldChoiceSetFilterSet(BaseFilterSet):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = CustomFieldChoiceSet
|
model = CustomFieldChoiceSet
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'name', 'description',
|
'id', 'name', 'description', 'order_alphabetically',
|
||||||
]
|
]
|
||||||
|
|
||||||
def search(self, queryset, name, value):
|
def search(self, queryset, name, value):
|
||||||
|
@ -65,6 +65,10 @@ class CustomFieldChoiceSetBulkEditForm(BulkEditForm):
|
|||||||
description = forms.CharField(
|
description = forms.CharField(
|
||||||
required=False
|
required=False
|
||||||
)
|
)
|
||||||
|
order_alphabetically = forms.NullBooleanField(
|
||||||
|
required=False,
|
||||||
|
widget=BulkEditNullBooleanSelect()
|
||||||
|
)
|
||||||
|
|
||||||
nullable_fields = ('description',)
|
nullable_fields = ('description',)
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ class CustomFieldChoiceSetImportForm(CSVModelForm):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = CustomFieldChoiceSet
|
model = CustomFieldChoiceSet
|
||||||
fields = (
|
fields = (
|
||||||
'name', 'description', 'extra_choices',
|
'name', 'description', 'extra_choices', 'order_alphabetically',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ class CustomFieldChoiceSetForm(BootstrapMixin, forms.ModelForm):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CustomFieldChoiceSet
|
model = CustomFieldChoiceSet
|
||||||
fields = ('name', 'description', 'extra_choices')
|
fields = ('name', 'description', 'extra_choices', 'order_alphabetically')
|
||||||
|
|
||||||
|
|
||||||
class CustomLinkForm(BootstrapMixin, forms.ModelForm):
|
class CustomLinkForm(BootstrapMixin, forms.ModelForm):
|
||||||
|
@ -43,6 +43,7 @@ class Migration(migrations.Migration):
|
|||||||
('name', models.CharField(max_length=100, unique=True)),
|
('name', models.CharField(max_length=100, unique=True)),
|
||||||
('description', models.CharField(blank=True, max_length=200)),
|
('description', models.CharField(blank=True, max_length=200)),
|
||||||
('extra_choices', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=100), size=None)),
|
('extra_choices', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=100), size=None)),
|
||||||
|
('order_alphabetically', models.BooleanField(default=False)),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'ordering': ('name',),
|
'ordering': ('name',),
|
||||||
|
@ -649,6 +649,10 @@ class CustomFieldChoiceSet(ChangeLoggedModel):
|
|||||||
base_field=models.CharField(max_length=100),
|
base_field=models.CharField(max_length=100),
|
||||||
help_text=_('Comma-separated list of available choices (for selection fields)')
|
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:
|
class Meta:
|
||||||
ordering = ('name',)
|
ordering = ('name',)
|
||||||
@ -662,3 +666,11 @@ class CustomFieldChoiceSet(ChangeLoggedModel):
|
|||||||
@property
|
@property
|
||||||
def choices(self):
|
def choices(self):
|
||||||
return self.extra_choices
|
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)
|
||||||
|
@ -98,7 +98,8 @@ class CustomFieldChoiceSetTable(NetBoxTable):
|
|||||||
class Meta(NetBoxTable.Meta):
|
class Meta(NetBoxTable.Meta):
|
||||||
model = CustomFieldChoiceSet
|
model = CustomFieldChoiceSet
|
||||||
fields = (
|
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')
|
default_columns = ('pk', 'name', 'choice_count', 'description')
|
||||||
|
|
||||||
|
@ -17,6 +17,10 @@
|
|||||||
<th scope="row">Description</th>
|
<th scope="row">Description</th>
|
||||||
<td>{{ object.description|markdown|placeholder }}</td>
|
<td>{{ object.description|markdown|placeholder }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row">Order Alphabetically</th>
|
||||||
|
<td>{% checkmark object.order_alphabetically %}</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="row">Used by</th>
|
<th scope="row">Used by</th>
|
||||||
<td>{# TODO #}</td>
|
<td>{# TODO #}</td>
|
||||||
|
Loading…
Reference in New Issue
Block a user