diff --git a/netbox/utilities/choices.py b/netbox/utilities/choices.py index 2151ff4a9..6e6afe3e2 100644 --- a/netbox/utilities/choices.py +++ b/netbox/utilities/choices.py @@ -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 diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index 007d87668..a14ec9305 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -9,6 +9,7 @@ from django.conf import settings from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput from mptt.forms import TreeNodeMultipleChoiceField +from .choices import unpack_grouped_choices from .constants import * from .validators import EnhancedURLValidator @@ -119,43 +120,6 @@ def add_blank_choice(choices): return ((None, '---------'),) + tuple(choices) -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 key == 1300: - breakme = True - 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 - - # # Widgets # diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 7b1e059a6..c4b3bb6ea 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -7,7 +7,7 @@ from django.utils.html import strip_tags from django.utils.safestring import mark_safe from markdown import markdown -from utilities.forms import unpack_grouped_choices +from utilities.choices import unpack_grouped_choices from utilities.utils import foreground_color