Skip to content

Commit

Permalink
Merge pull request bigjason#46 from ashwch/nullboolean-support
Browse files Browse the repository at this point in the history
Nullboolean support -- thanks @ashwch
  • Loading branch information
sergei-maertens committed Feb 13, 2017
2 parents ee1be5f + a106b87 commit 4c2cf22
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Many thanks to the various contributors:
* https://github.com/soulne4ny
* https://github.com/vaad2
* https://github.com/WoLpH
* https://github.com/ashwch
6 changes: 4 additions & 2 deletions djchoices/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def __get__(self, obj, objtype):

# End Support Functionality

sentinel = object()


class ChoiceItem(object):
"""
Expand All @@ -44,7 +46,7 @@ class ChoiceItem(object):
"""
order = 0

def __init__(self, value=None, label=None, order=None):
def __init__(self, value=sentinel, label=None, order=None):
self.value = value
self.label = label

Expand Down Expand Up @@ -93,7 +95,7 @@ def __new__(cls, name, bases, attrs):
# TODO: mark translatable by default?
label = cls.name_clean.sub(" ", field_name)

val0 = label if val.value is None else val.value
val0 = label if val.value is sentinel else val.value
choices.append((val0, label))
attrs[field_name] = StaticProp(val0)
setattr(labels, field_name, label)
Expand Down
14 changes: 14 additions & 0 deletions djchoices/tests/test_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class EmptyValueClass(DjangoChoices):
Option2 = ChoiceItem()
Option3 = ChoiceItem()

class NullBooleanValueClass(DjangoChoices):
Option1 = ChoiceItem(None, "Pending")
Option2 = ChoiceItem(True, "Successful")
Option3 = ChoiceItem(False, "Failed")


class DjangoChoices(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -165,6 +170,15 @@ def test_empty_value_class(self):
self.assertEqual(choices[1][0], "Option2")
self.assertEqual(choices[2][0], "Option3")

def test_null_boolean_value_class(self):
choices = NullBooleanValueClass.choices
self.assertEqual(choices[0][0], None)
self.assertEqual(choices[1][0], True)
self.assertEqual(choices[2][0], False)
self.assertEqual(choices[0][1], "Pending")
self.assertEqual(choices[1][1], "Successful")
self.assertEqual(choices[2][1], "Failed")

@unittest.skipUnless(*has_new_migrations())
def test_deconstructible_validator(self):
deconstructed = NumericTestClass.validator.deconstruct()
Expand Down

0 comments on commit 4c2cf22

Please sign in to comment.