mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Closes #4857: Drop support for legacy numeric choice field values
This commit is contained in:
@ -103,18 +103,11 @@ class ChoiceField(serializers.Field):
|
||||
def to_representation(self, obj):
|
||||
if obj is '':
|
||||
return None
|
||||
data = OrderedDict([
|
||||
return OrderedDict([
|
||||
('value', obj),
|
||||
('label', self._choices[obj])
|
||||
])
|
||||
|
||||
# TODO: Remove in v2.8
|
||||
# Include legacy numeric ID (where applicable)
|
||||
if hasattr(self.choiceset, 'LEGACY_MAP') and obj in self.choiceset.LEGACY_MAP:
|
||||
data['id'] = self.choiceset.LEGACY_MAP.get(obj)
|
||||
|
||||
return data
|
||||
|
||||
def to_internal_value(self, data):
|
||||
if data is '':
|
||||
if self.allow_blank:
|
||||
@ -140,14 +133,10 @@ class ChoiceField(serializers.Field):
|
||||
try:
|
||||
if data in self._choices:
|
||||
return data
|
||||
# Check if data is a legacy numeric ID
|
||||
slug = self.choiceset.id_to_slug(data)
|
||||
if slug is not None:
|
||||
return slug
|
||||
except TypeError: # Input is an unhashable type
|
||||
pass
|
||||
|
||||
raise ValidationError("{} is not a valid choice.".format(data))
|
||||
raise ValidationError(f"{data} is not a valid choice.")
|
||||
|
||||
@property
|
||||
def choices(self):
|
||||
|
@ -14,7 +14,6 @@ class ChoiceSetMeta(type):
|
||||
class ChoiceSet(metaclass=ChoiceSetMeta):
|
||||
|
||||
CHOICES = list()
|
||||
LEGACY_MAP = dict()
|
||||
|
||||
@classmethod
|
||||
def values(cls):
|
||||
@ -25,25 +24,6 @@ class ChoiceSet(metaclass=ChoiceSetMeta):
|
||||
# Unpack grouped choices before casting as a dict
|
||||
return dict(unpack_grouped_choices(cls.CHOICES))
|
||||
|
||||
@classmethod
|
||||
def slug_to_id(cls, slug):
|
||||
"""
|
||||
Return the legacy integer value corresponding to a slug.
|
||||
"""
|
||||
return cls.LEGACY_MAP.get(slug)
|
||||
|
||||
@classmethod
|
||||
def id_to_slug(cls, legacy_id):
|
||||
"""
|
||||
Return the slug value corresponding to a legacy integer value.
|
||||
"""
|
||||
if legacy_id in cls.LEGACY_MAP.values():
|
||||
# Invert the legacy map to allow lookup by integer
|
||||
legacy_map = dict([
|
||||
(id, slug) for slug, id in cls.LEGACY_MAP.items()
|
||||
])
|
||||
return legacy_map.get(legacy_id)
|
||||
|
||||
|
||||
def unpack_grouped_choices(choices):
|
||||
"""
|
||||
|
@ -11,6 +11,7 @@ class ExampleChoices(ChoiceSet):
|
||||
CHOICE_1 = 1
|
||||
CHOICE_2 = 2
|
||||
CHOICE_3 = 3
|
||||
|
||||
CHOICES = (
|
||||
('Letters', (
|
||||
(CHOICE_A, 'A'),
|
||||
@ -23,14 +24,6 @@ class ExampleChoices(ChoiceSet):
|
||||
(CHOICE_3, 'Three'),
|
||||
)),
|
||||
)
|
||||
LEGACY_MAP = {
|
||||
CHOICE_A: 101,
|
||||
CHOICE_B: 102,
|
||||
CHOICE_C: 103,
|
||||
CHOICE_1: 201,
|
||||
CHOICE_2: 202,
|
||||
CHOICE_3: 203,
|
||||
}
|
||||
|
||||
|
||||
class ChoiceSetTestCase(TestCase):
|
||||
@ -42,9 +35,3 @@ class ChoiceSetTestCase(TestCase):
|
||||
self.assertEqual(ExampleChoices.as_dict(), {
|
||||
'a': 'A', 'b': 'B', 'c': 'C', 1: 'One', 2: 'Two', 3: 'Three'
|
||||
})
|
||||
|
||||
def test_slug_to_id(self):
|
||||
self.assertEqual(ExampleChoices.slug_to_id('a'), 101)
|
||||
|
||||
def test_id_to_slug(self):
|
||||
self.assertEqual(ExampleChoices.id_to_slug(101), 'a')
|
||||
|
Reference in New Issue
Block a user