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

Move unpack_grouped_choices() to utilities.choices

This commit is contained in:
Jeremy Stretch
2020-01-15 09:36:39 -05:00
parent b538495a29
commit 826f4d313d
3 changed files with 37 additions and 41 deletions

View File

@@ -1,6 +1,3 @@
from utilities.forms import unpack_grouped_choices
class ChoiceSetMeta(type):
"""
Metaclass for ChoiceSet
@@ -46,3 +43,38 @@ class ChoiceSet(metaclass=ChoiceSetMeta):
(id, slug) for slug, id in cls.LEGACY_MAP.items()
])
return legacy_map.get(legacy_id)
def unpack_grouped_choices(choices):
"""
Unpack a grouped choices hierarchy into a flat list of two-tuples. For example:
choices = (
('Foo', (
(1, 'A'),
(2, 'B')
)),
('Bar', (
(3, 'C'),
(4, 'D')
))
)
becomes:
choices = (
(1, 'A'),
(2, 'B'),
(3, 'C'),
(4, 'D')
)
"""
unpacked_choices = []
for key, value in choices:
if isinstance(value, (list, tuple)):
# Entered an optgroup
for optgroup_key, optgroup_value in value:
unpacked_choices.append((optgroup_key, optgroup_value))
else:
unpacked_choices.append((key, value))
return unpacked_choices