Misc cleanup

This commit is contained in:
Jeremy Stretch 2023-07-19 15:02:34 -04:00
parent 4a271df635
commit 4dfeb2d0ed
6 changed files with 41 additions and 41 deletions

View File

@ -42,7 +42,8 @@ class Migration(migrations.Migration):
('last_updated', models.DateTimeField(auto_now=True, null=True)), ('last_updated', models.DateTimeField(auto_now=True, null=True)),
('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)), ('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)), ('order_alphabetically', models.BooleanField(default=False)),
], ],
options={ options={

View File

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

View File

@ -705,6 +705,6 @@ class CustomFieldChoiceSet(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel
# Sort choices if alphabetical ordering is enforced # Sort choices if alphabetical ordering is enforced
if self.order_alphabetically: if self.order_alphabetically:
self.extra_choices = sorted(self.choices) self.extra_choices = sorted(self.extra_choices)
return super().save(*args, **kwargs) return super().save(*args, **kwargs)

View File

@ -64,10 +64,13 @@ class CustomFieldTable(NetBoxTable):
) )
content_types = columns.ContentTypesColumn() content_types = columns.ContentTypesColumn()
required = columns.BooleanColumn() required = columns.BooleanColumn()
ui_visibility = columns.ChoiceFieldColumn(verbose_name="UI visibility") ui_visibility = columns.ChoiceFieldColumn(
verbose_name="UI visibility"
)
description = columns.MarkdownColumn() description = columns.MarkdownColumn()
choices = columns.ArrayColumn( choices = columns.ArrayColumn(
max_items=10, max_items=10,
func=lambda x: x[1],
orderable=False, orderable=False,
verbose_name=_('Choices') verbose_name=_('Choices')
) )
@ -88,11 +91,15 @@ class CustomFieldChoiceSetTable(NetBoxTable):
linkify=True linkify=True
) )
base_choices = columns.ChoiceFieldColumn() base_choices = columns.ChoiceFieldColumn()
extra_choices = columns.ArrayColumn(
max_items=10,
orderable=False
)
choices = columns.ArrayColumn( choices = columns.ArrayColumn(
max_items=10, max_items=10,
accessor=tables.A('extra_choices'), func=lambda x: x[1],
orderable=False, orderable=False,
verbose_name=_('Count') verbose_name=_('Choices')
) )
choice_count = tables.TemplateColumn( choice_count = tables.TemplateColumn(
accessor=tables.A('extra_choices'), accessor=tables.A('extra_choices'),
@ -105,8 +112,8 @@ class CustomFieldChoiceSetTable(NetBoxTable):
class Meta(NetBoxTable.Meta): class Meta(NetBoxTable.Meta):
model = CustomFieldChoiceSet model = CustomFieldChoiceSet
fields = ( fields = (
'pk', 'id', 'name', 'description', 'base_choices', 'choice_count', 'choices', 'order_alphabetically', 'pk', 'id', 'name', 'description', 'base_choices', 'extra_choices', 'choice_count', 'choices',
'created', 'last_updated', 'order_alphabetically', 'created', 'last_updated',
) )
default_columns = ('pk', 'name', 'base_choices', 'choice_count', 'description') default_columns = ('pk', 'name', 'base_choices', 'choice_count', 'description')

View File

@ -598,16 +598,25 @@ class ArrayColumn(tables.Column):
""" """
List array items as a comma-separated list. 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.max_items = max_items
self.func = func
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def render(self, value): def render(self, value):
omitted_count = 0
# Limit the returned items to the specified maximum number (if any)
if self.max_items: if self.max_items:
# Limit the returned items to the specified maximum number omitted_count = len(value) - self.max_items
omitted = len(value) - self.max_items
value = value[:self.max_items - 1] 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) return ', '.join(value)

View File

@ -19,7 +19,7 @@
</tr> </tr>
<tr> <tr>
<th scope="row">Base Choices</th> <th scope="row">Base Choices</th>
<td>{{ object.get_base_choices_display }}</td> <td>{{ object.get_base_choices_display|placeholder }}</td>
</tr> </tr>
<tr> <tr>
<th scope="row">Choices</th> <th scope="row">Choices</th>
@ -46,12 +46,19 @@
</div> </div>
<div class="col col-md-6"> <div class="col col-md-6">
<div class="card"> <div class="card">
<h5 class="card-header">Choices</h5> <h5 class="card-header">Choices ({{ object.choices|length }})</h5>
<div class="card-body"> <div class="card-body">
<table class="table table-hover attr-table"> <table class="table table-hover table-headings">
{% for choice in object.choices %} <thead>
<tr> <tr>
<td>{{ choice }}</td> <th>Value</th>
<th>Label</th>
</tr>
</thead>
{% for value, label in object.choices|slice:":100" %}
<tr>
<td>{{ value }}</td>
<td>{{ label }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>