Skip to content

Commit

Permalink
Handle choice groups in active filters and support dictionaries as c…
Browse files Browse the repository at this point in the history
…hoices (#12032)

Co-authored-by: SebCorbin <sebastien@rgoods.com>
  • Loading branch information
saevarom and SebCorbin committed Jun 13, 2024
1 parent 13a8402 commit 98a29a0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
~~~~~~~~~~~~~~~~~~

* Fix: Allow renditions of `.ico` images (Julie Rymer)

* Fix: Handle choice groups as dictionaries in active filters (Sébastien Corbin)

6.1.2 (30.05.2024)
~~~~~~~~~~~~~~~~~~
Expand Down
5 changes: 3 additions & 2 deletions wagtail/admin/views/generic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from wagtail.admin.ui.tables import Column, Table
from wagtail.admin.utils import get_valid_next_url_from_request
from wagtail.admin.widgets.button import ButtonWithDropdown
from wagtail.utils.utils import flatten_choices


class WagtailAdminTemplateMixin(TemplateResponseMixin, ContextMixin):
Expand Down Expand Up @@ -288,7 +289,7 @@ def active_filters(self):
)
)
elif isinstance(filter_def, MultipleChoiceFilter):
choices = {str(id): label for id, label in filter_def.field.choices}
choices = flatten_choices(filter_def.field.choices)
for item in value:
filters.append(
ActiveFilter(
Expand Down Expand Up @@ -326,7 +327,7 @@ def active_filters(self):
)
)
elif isinstance(filter_def, ChoiceFilter):
choices = {str(id): label for id, label in filter_def.field.choices}
choices = flatten_choices(filter_def.field.choices)
filters.append(
ActiveFilter(
bound_field.auto_id,
Expand Down
29 changes: 28 additions & 1 deletion wagtail/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from wagtail.models import Page, Site
from wagtail.utils.file import hash_filelike
from wagtail.utils.utils import deep_update
from wagtail.utils.utils import deep_update, flatten_choices


class TestCamelCaseToUnderscore(TestCase):
Expand Down Expand Up @@ -575,3 +575,30 @@ def read(self, bytes):
hash_filelike(FakeLargeFile()),
"bd36f0c5a02cd6e9e34202ea3ff8db07b533e025",
)


class TestFlattenChoices(SimpleTestCase):
def test_tuple_choices(self):
choices = [(1, "1st"), (2, "2nd")]
self.assertEqual(flatten_choices(choices), {"1": "1st", "2": "2nd"})

def test_grouped_tuple_choices(self):
choices = [("Group", [(1, "1st"), (2, "2nd")])]
self.assertEqual(flatten_choices(choices), {"1": "1st", "2": "2nd"})

def test_dictionary_choices(self):
choices = {
"Martial Arts": {"judo": "Judo", "karate": "Karate"},
"Racket": {"badminton": "Badminton", "tennis": "Tennis"},
"unknown": "Unknown",
}
self.assertEqual(
flatten_choices(choices),
{
"judo": "Judo",
"karate": "Karate",
"badminton": "Badminton",
"tennis": "Tennis",
"unknown": "Unknown",
},
)
26 changes: 26 additions & 0 deletions wagtail/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,29 @@ def deep_update(source, overrides):
else:
source[key] = overrides[key]
return source


def flatten_choices(choices):
"""
Convert potentially grouped choices into a flat dict of choices.
flatten_choices([(1, '1st'), (2, '2nd')]) -> {1: '1st', 2: '2nd'}
flatten_choices([('Group', [(1, '1st'), (2, '2nd')])]) -> {1: '1st', 2: '2nd'}
flatten_choices({'Group': {'1': '1st', '2': '2nd'}}) -> {'1': '1st', '2': '2nd'}
"""
ret = {}

to_unpack = choices.items() if isinstance(choices, dict) else choices

for key, value in to_unpack:
if isinstance(value, (list, tuple)):
# grouped choices (category, sub choices)
for sub_key, sub_value in value:
ret[str(sub_key)] = sub_value
elif isinstance(value, (dict)):
# grouped choices using dict (category, sub choices)
for sub_key, sub_value in value.items():
ret[str(sub_key)] = sub_value
else:
# choice (key, display value)
ret[str(key)] = value
return ret

0 comments on commit 98a29a0

Please sign in to comment.