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

Support CSS class definition directly in CHOICES iterable

This commit is contained in:
jeremystretch
2021-12-16 10:03:23 -05:00
parent 419f86a4a5
commit 0d3b50a5e5
19 changed files with 110 additions and 214 deletions

View File

@@ -8,20 +8,37 @@ class ChoiceSetMeta(type):
def __new__(mcs, name, bases, attrs):
# Extend static choices with any configured choices
if 'key' in attrs:
key = attrs.get('key')
if key:
try:
attrs['CHOICES'].extend(settings.FIELD_CHOICES[attrs['key']])
attrs['CHOICES'].extend(settings.FIELD_CHOICES[key])
except KeyError:
pass
# Define choice tuples
# TODO: Support optgroup nesting
attrs['_choices'] = [
(c[0], c[1]) for c in attrs['CHOICES']
]
# Define color maps
# TODO: Support optgroup nesting
colors = {}
for c in attrs['CHOICES']:
try:
colors[c[0]] = c[2]
except IndexError:
pass
attrs['colors'] = colors
return super().__new__(mcs, name, bases, attrs)
def __call__(cls, *args, **kwargs):
# Django will check if a 'choices' value is callable, and if so assume that it returns an iterable
return getattr(cls, 'CHOICES', ())
return getattr(cls, '_choices', ())
def __iter__(cls):
choices = getattr(cls, 'CHOICES', ())
choices = getattr(cls, '_choices', ())
return iter(choices)