1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #12194: Add pre-defined custom field choices (#13219)

* Initial work on custom field choice sets

* Rename choices to extra_choices (prep for #12194)

* Remove CustomField.choices

* Add & update tests

* Clean up table columns

* Add order_alphanetically boolean for choice sets

* Introduce ArrayColumn for choice lists

* Show dependent custom fields on choice set view

* Update custom fields documentation

* Introduce ArrayWidget for more convenient editing of choices

* Incorporate PR feedback

* Misc cleanup

* Initial work on predefined choices for custom fields

* Misc cleanup

* Add IATA airport codes

* #13241: Add support for custom field choice labels

* Restore ArrayColumn

* Misc cleanup

* Change extra_choices back to a nested ArrayField to preserve choice ordering

* Hack to bypass GraphQL API test utility absent support for nested ArrayFields
This commit is contained in:
Jeremy Stretch
2023-07-28 11:24:21 -04:00
committed by GitHub
parent 9d3bb585a2
commit cf1b1a83eb
27 changed files with 121940 additions and 100 deletions

View File

@@ -9,8 +9,8 @@ from django.db.models import DateField, DateTimeField
from django.template import Context, Template
from django.urls import reverse
from django.utils.dateparse import parse_date
from django.utils.html import escape
from django.utils.formats import date_format
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django_tables2.columns import library
from django_tables2.utils import Accessor
@@ -24,6 +24,7 @@ __all__ = (
'ArrayColumn',
'BooleanColumn',
'ChoiceFieldColumn',
'ChoicesColumn',
'ColorColumn',
'ColoredLabelColumn',
'ContentTypeColumn',
@@ -598,16 +599,49 @@ class ArrayColumn(tables.Column):
"""
List array items as a comma-separated list.
"""
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:
omitted_count = len(value) - self.max_items
value = value[:self.max_items - 1]
# 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)
class ChoicesColumn(tables.Column):
"""
Display the human-friendly labels of a set of choices.
"""
def __init__(self, *args, max_items=None, **kwargs):
self.max_items = max_items
super().__init__(*args, **kwargs)
def render(self, value):
omitted_count = 0
value = [v[1] for v in value]
# 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)')
# Annotate omitted items (if applicable)
if omitted_count > 0:
value.append(f'({omitted_count} more)')
return ', '.join(value)