Skip to content

Commit

Permalink
WIP - Allow inference of booleans in enums
Browse files Browse the repository at this point in the history
Where a field is a BooleanField or a NullBooleanField inference should
generate a lookup list with `[Yes, No]` and `[Yes, No, Unknown]` as
options (for radio buttons).

This will require changes to templates/radio.html so that Unkown does
not use Unknown as a value, instead leaving the value empty so that we
know the intended value is None.

This would be easier if lookuplists were tuples (like choices in Django)
so I'm considering implementing support for lists of tuples (as well as
the current list of strings) for lookuplists - the alternative is too
much logic in the template and special-casing the string 'Unknown'

Will fix #1540
  • Loading branch information
rossjones committed Jun 18, 2018
1 parent 137c5dc commit a66f108
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion opal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,18 @@ def get_field_description(cls, name):
@classmethod
def get_field_enum(cls, name):
field = cls._get_field(name)
choices = getattr(field, "choices", [])

choices = getattr(field, "choices", [])
if choices:
return [i[1] for i in choices]

yes_no = ["Yes", "No"]
if cls._get_field_type(name) == models.fields.BooleanField:
return yes_no
elif cls._get_field_type(name) == models.fields.NullBooleanField:
return yes_no + ["Unknown"]


@classmethod
def get_lookup_list_api_name(cls, field_name):
lookup_list = None
Expand Down
1 change: 1 addition & 0 deletions opal/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class HatWearer(models.EpisodeSubrecord):
name = dmodels.CharField(max_length=200)
hats = dmodels.ManyToManyField(Hat, related_name="hat_wearers")
wearing_a_hat = dmodels.BooleanField(default=True)
suits_a_hat = dmodels.NullBooleanField()

class Meta:
verbose_name = 'Wearer of Hats'
Expand Down
14 changes: 14 additions & 0 deletions opal/tests/test_templatetags_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,20 @@ def test_radio_infer_lookuplists(self):
rendered = template.render(Context({}))
self.assertIn('purple', rendered)

def test_radio_infer_lookuplists_with_boolean(self):
template = Template('{% load forms %}{% radio field="HatWearer.wearing_a_hat" %}')
rendered = template.render(Context({}))
self.assertIn('Yes', rendered)
self.assertIn('No', rendered)
self.assertNotIn('Unknown', rendered)

def test_radio_infer_lookuplists_with_quantum_boolean(self):
template = Template('{% load forms %}{% radio field="HatWearer.suits_a_hat" %}')
rendered = template.render(Context({}))
self.assertIn('Yes', rendered)
self.assertIn('No', rendered)
self.assertIn('Unknown', rendered)

def test_element_name(self):
tpl = Template('{% load forms %}{% radio label="hai" model="bai" element_name="onions"%}')
rendered = tpl.render(Context({}))
Expand Down

0 comments on commit a66f108

Please sign in to comment.