From 4dfeb2d0eded7cd5160c1a469e9ccbc56ded771c Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 19 Jul 2023 15:02:34 -0400 Subject: [PATCH] Misc cleanup --- .../migrations/0096_customfieldchoiceset.py | 3 ++- ...8_customfieldchoiceset_add_base_choices.py | 24 ------------------- netbox/extras/models/customfields.py | 2 +- netbox/extras/tables/tables.py | 17 +++++++++---- netbox/netbox/tables/columns.py | 19 +++++++++++---- .../extras/customfieldchoiceset.html | 17 +++++++++---- 6 files changed, 41 insertions(+), 41 deletions(-) delete mode 100644 netbox/extras/migrations/0098_customfieldchoiceset_add_base_choices.py diff --git a/netbox/extras/migrations/0096_customfieldchoiceset.py b/netbox/extras/migrations/0096_customfieldchoiceset.py index dea6f02fc..d657cdd7d 100644 --- a/netbox/extras/migrations/0096_customfieldchoiceset.py +++ b/netbox/extras/migrations/0096_customfieldchoiceset.py @@ -42,7 +42,8 @@ class Migration(migrations.Migration): ('last_updated', models.DateTimeField(auto_now=True, null=True)), ('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)), + ('base_choices', models.CharField(blank=True, max_length=50)), + ('extra_choices', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=100), blank=True, null=True, size=None)), ('order_alphabetically', models.BooleanField(default=False)), ], options={ diff --git a/netbox/extras/migrations/0098_customfieldchoiceset_add_base_choices.py b/netbox/extras/migrations/0098_customfieldchoiceset_add_base_choices.py deleted file mode 100644 index 71ae946df..000000000 --- a/netbox/extras/migrations/0098_customfieldchoiceset_add_base_choices.py +++ /dev/null @@ -1,24 +0,0 @@ -# Generated by Django 4.1.10 on 2023-07-18 13:56 - -import django.contrib.postgres.fields -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('extras', '0097_customfield_remove_choices'), - ] - - operations = [ - migrations.AddField( - model_name='customfieldchoiceset', - name='base_choices', - field=models.CharField(blank=True, max_length=50), - ), - migrations.AlterField( - model_name='customfieldchoiceset', - name='extra_choices', - field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=100), blank=True, null=True, size=None), - ), - ] diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 1294049bb..4b2f38b54 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -705,6 +705,6 @@ class CustomFieldChoiceSet(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel # Sort choices if alphabetical ordering is enforced if self.order_alphabetically: - self.extra_choices = sorted(self.choices) + self.extra_choices = sorted(self.extra_choices) return super().save(*args, **kwargs) diff --git a/netbox/extras/tables/tables.py b/netbox/extras/tables/tables.py index 2ea16a9aa..73f831f96 100644 --- a/netbox/extras/tables/tables.py +++ b/netbox/extras/tables/tables.py @@ -64,10 +64,13 @@ class CustomFieldTable(NetBoxTable): ) content_types = columns.ContentTypesColumn() required = columns.BooleanColumn() - ui_visibility = columns.ChoiceFieldColumn(verbose_name="UI visibility") + ui_visibility = columns.ChoiceFieldColumn( + verbose_name="UI visibility" + ) description = columns.MarkdownColumn() choices = columns.ArrayColumn( max_items=10, + func=lambda x: x[1], orderable=False, verbose_name=_('Choices') ) @@ -88,11 +91,15 @@ class CustomFieldChoiceSetTable(NetBoxTable): linkify=True ) base_choices = columns.ChoiceFieldColumn() + extra_choices = columns.ArrayColumn( + max_items=10, + orderable=False + ) choices = columns.ArrayColumn( max_items=10, - accessor=tables.A('extra_choices'), + func=lambda x: x[1], orderable=False, - verbose_name=_('Count') + verbose_name=_('Choices') ) choice_count = tables.TemplateColumn( accessor=tables.A('extra_choices'), @@ -105,8 +112,8 @@ class CustomFieldChoiceSetTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = CustomFieldChoiceSet fields = ( - 'pk', 'id', 'name', 'description', 'base_choices', 'choice_count', 'choices', 'order_alphabetically', - 'created', 'last_updated', + 'pk', 'id', 'name', 'description', 'base_choices', 'extra_choices', 'choice_count', 'choices', + 'order_alphabetically', 'created', 'last_updated', ) default_columns = ('pk', 'name', 'base_choices', 'choice_count', 'description') diff --git a/netbox/netbox/tables/columns.py b/netbox/netbox/tables/columns.py index 1f698f396..c7cd490c2 100644 --- a/netbox/netbox/tables/columns.py +++ b/netbox/netbox/tables/columns.py @@ -598,16 +598,25 @@ class ArrayColumn(tables.Column): """ List array items as a comma-separated list. """ - def __init__(self, *args, max_items=None, **kwargs): + def __init__(self, *args, max_items=None, func=str, **kwargs): self.max_items = max_items + self.func = func super().__init__(*args, **kwargs) def render(self, value): + omitted_count = 0 + + # Limit the returned items to the specified maximum number (if any) if self.max_items: - # Limit the returned items to the specified maximum number - omitted = len(value) - self.max_items + omitted_count = len(value) - self.max_items value = value[:self.max_items - 1] - if omitted > 0: - value.append(f'({omitted} more)') + + # Apply custom processing function (if any) per item + if self.func: + value = [self.func(v) for v in value] + + # Annotate omitted items (if applicable) + if omitted_count > 0: + value.append(f'({omitted_count} more)') return ', '.join(value) diff --git a/netbox/templates/extras/customfieldchoiceset.html b/netbox/templates/extras/customfieldchoiceset.html index a4a226de1..c9f579192 100644 --- a/netbox/templates/extras/customfieldchoiceset.html +++ b/netbox/templates/extras/customfieldchoiceset.html @@ -19,7 +19,7 @@ Base Choices - {{ object.get_base_choices_display }} + {{ object.get_base_choices_display|placeholder }} Choices @@ -46,12 +46,19 @@
-
Choices
+
Choices ({{ object.choices|length }})
- - {% for choice in object.choices %} +
+ - + + + + + {% for value, label in object.choices|slice:":100" %} + + + {% endfor %}
{{ choice }}ValueLabel
{{ value }}{{ label }}