mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-18 19:32:24 -06:00
Add _choices endpoint tests for all apps
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
from utilities.forms import unpack_grouped_choices
|
||||
|
||||
|
||||
class ChoiceSetMeta(type):
|
||||
"""
|
||||
Metaclass for ChoiceSet
|
||||
@@ -20,6 +23,11 @@ class ChoiceSet(metaclass=ChoiceSetMeta):
|
||||
def values(cls):
|
||||
return [c[0] for c in cls.CHOICES]
|
||||
|
||||
@classmethod
|
||||
def as_dict(cls):
|
||||
# Unpack grouped choices before casting as a dict
|
||||
return dict(unpack_grouped_choices(cls.CHOICES))
|
||||
|
||||
@classmethod
|
||||
def slug_to_id(cls, slug):
|
||||
"""
|
||||
|
||||
@@ -35,3 +35,30 @@ def create_test_user(username='testuser', permissions=list()):
|
||||
user.user_permissions.add(perm)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
def choices_to_dict(choices_list):
|
||||
"""
|
||||
Convert a list of field choices to a dictionary suitable for direct comparison with a ChoiceSet. For example:
|
||||
|
||||
[
|
||||
{
|
||||
"value": "choice-1",
|
||||
"label": "First Choice"
|
||||
},
|
||||
{
|
||||
"value": "choice-2",
|
||||
"label": "Second Choice"
|
||||
}
|
||||
]
|
||||
|
||||
Becomes:
|
||||
|
||||
{
|
||||
"choice-1": "First Choice",
|
||||
"choice-2": "Second Choice
|
||||
}
|
||||
"""
|
||||
return {
|
||||
choice['value']: choice['label'] for choice in choices_list
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user