Преглед на файлове

Add test for flatten_choices and support dictionaries

Sævar Öfjörð Magnússon преди 9 месеца
родител
ревизия
49ef72725a
променени са 4 файла, в които са добавени 39 реда и са изтрити 2 реда
  1. 1 0
      CHANGELOG.txt
  2. 1 0
      docs/releases/6.2.md
  3. 28 1
      wagtail/tests/test_utils.py
  4. 9 1
      wagtail/utils/utils.py

+ 1 - 0
CHANGELOG.txt

@@ -31,6 +31,7 @@ Changelog
  * Maintenance: Exclude the `client/scss` directory in Tailwind content config to speed up CSS compilation (Sage Abdullah)
  * Maintenance: Split `contrib.frontend_cache.backends` into dedicated sub-modules (Andy Babic)
  * Maintenance: Remove unused `docs/autobuild.sh` script (Sævar Öfjörð Magnússon)
+ * Fix: Handle choice groups as dictionaries in active filters (Sébastien Corbin)
 
 
 6.1.2 (30.05.2024)

+ 1 - 0
docs/releases/6.2.md

@@ -35,6 +35,7 @@ depth: 1
  * Do not show delete button on model edit views if per-instance permissions prevent deletion (Matt Westcott)
  * Remove duplicate header in privacy dialog when a privacy setting is set on a parent page or collection (Matthias Brück)
  * Allow renditions of `.ico` images (Julie Rymer)
+ * Fix the rendering of grouped choices when using ChoiceFilter in combination with choices (Sébastien Corbin)
 
 
 ### Documentation

+ 28 - 1
wagtail/tests/test_utils.py

@@ -28,7 +28,7 @@ from wagtail.coreutils import (
 )
 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
 from wagtail.utils.version import get_main_version
 
 
@@ -589,3 +589,30 @@ class TestVersion(SimpleTestCase):
         for version, include_patch, expected in cases:
             with self.subTest(version=version, include_patch=include_patch):
                 self.assertEqual(get_main_version(version, include_patch), expected)
+
+
+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",
+            },
+        )

+ 9 - 1
wagtail/utils/utils.py

@@ -21,13 +21,21 @@ def flatten_choices(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 = {}
-    for key, value in choices:
+
+    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