From a2d99178dfd5fbb1579e7a5a7b1471d20ef409ee Mon Sep 17 00:00:00 2001 From: Jhonatan Lopes Date: Sat, 23 Sep 2023 17:59:31 -0300 Subject: [PATCH 1/5] Show locale filter selector based on installed languages --- wagtail_localize/views/report.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/wagtail_localize/views/report.py b/wagtail_localize/views/report.py index 2dce4594..f44dd55a 100644 --- a/wagtail_localize/views/report.py +++ b/wagtail_localize/views/report.py @@ -8,6 +8,7 @@ from modelcluster.fields import ParentalKey from wagtail.admin.filters import WagtailFilterSet from wagtail.admin.views.reports import ReportView +from wagtail.coreutils import get_content_languages from wagtail.models import get_translatable_models from wagtail_localize.models import Translation @@ -98,9 +99,17 @@ def filter(self, qs, value): ) -class LocaleFilter(django_filters.CharFilter): +def _get_locale_choices(): + choices = [ + (language_code, display_name) + for language_code, display_name in get_content_languages().items() + ] + return choices + + +class LocaleFilter(django_filters.ChoiceFilter): def filter(self, qs, value): - if value in EMPTY_VALUES: + if not value or value == self.null_value: return qs return qs.filter(**{self.field_name + "__language_code": value}) @@ -111,8 +120,19 @@ class TranslationsReportFilterSet(WagtailFilterSet): field_name="source__specific_content_type", label=gettext_lazy("Content type") ) source_title = SourceTitleFilter(label=gettext_lazy("Source title")) - source_locale = LocaleFilter(field_name="source__locale") - target_locale = LocaleFilter() + source_locale = LocaleFilter( + field_name="source__locale", + choices=_get_locale_choices, + empty_label=None, + null_label="All", + null_value="all", + ) + target_locale = LocaleFilter( + choices=_get_locale_choices, + empty_label=None, + null_label="All", + null_value="all", + ) class Meta: model = Translation From bcbb99a6d80e7d515c90e3126adfcc85f1e24517 Mon Sep 17 00:00:00 2001 From: Jhonatan Lopes Date: Sat, 23 Sep 2023 18:00:55 -0300 Subject: [PATCH 2/5] Update tests to handle 'all' option --- .../tests/test_translations_report.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/wagtail_localize/tests/test_translations_report.py b/wagtail_localize/tests/test_translations_report.py index edfd0c46..43050f72 100644 --- a/wagtail_localize/tests/test_translations_report.py +++ b/wagtail_localize/tests/test_translations_report.py @@ -135,6 +135,17 @@ def test_filter_by_source_locale(self): self.assertNotIn(self.blog_index_translation, response.context["object_list"]) self.assertNotIn(self.blog_post_translation, response.context["object_list"]) + def test_filter_by_source_locale_all_options(self): + response = self.client.get( + reverse("wagtail_localize:translations_report") + "?source_locale=all" + ) + + self.assertIn(self.snippet_translation, response.context["object_list"]) + self.assertIn(self.homepage_translation, response.context["object_list"]) + self.assertIn(self.de_homepage_translation, response.context["object_list"]) + self.assertIn(self.blog_index_translation, response.context["object_list"]) + self.assertIn(self.blog_post_translation, response.context["object_list"]) + def test_filter_by_target_locale(self): response = self.client.get( reverse("wagtail_localize:translations_report") + "?target_locale=de" @@ -145,3 +156,14 @@ def test_filter_by_target_locale(self): self.assertIn(self.de_homepage_translation, response.context["object_list"]) self.assertNotIn(self.blog_index_translation, response.context["object_list"]) self.assertNotIn(self.blog_post_translation, response.context["object_list"]) + + def test_filter_by_target_locale_all_options(self): + response = self.client.get( + reverse("wagtail_localize:translations_report") + "?target_locale=all" + ) + + self.assertIn(self.snippet_translation, response.context["object_list"]) + self.assertIn(self.homepage_translation, response.context["object_list"]) + self.assertIn(self.de_homepage_translation, response.context["object_list"]) + self.assertIn(self.blog_index_translation, response.context["object_list"]) + self.assertIn(self.blog_post_translation, response.context["object_list"]) From 303c7cb06a719deaf950294ae84093c8bf8d1988 Mon Sep 17 00:00:00 2001 From: Jhonatan Lopes Date: Sat, 23 Sep 2023 18:01:28 -0300 Subject: [PATCH 3/5] Fix docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bf891a8..3ec4db49 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ tox ``` or, you can run them for a specific environment `tox -e python3.8-django3.2-wagtail4.1` or specific test -`tox -e python3.9-django3.2-wagtail4.1-sqlite wagtail_localize.tests.test_edit_translation.TestGetEditTranslationView` +`tox -e python3.9-django3.2-wagtail4.1-sqlite -- wagtail_localize.tests.test_edit_translation.TestGetEditTranslationView` To run the test app interactively, use `tox -e interactive`, visit `http://127.0.0.1:8020/admin/` and log in with `admin`/`changeme`. From fb5c26084ca5d5f0dac35a7a371568ff4ff297e3 Mon Sep 17 00:00:00 2001 From: Jhonatan Lopes Date: Sun, 24 Sep 2023 10:57:23 -0300 Subject: [PATCH 4/5] Address review comments --- wagtail_localize/views/report.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/wagtail_localize/views/report.py b/wagtail_localize/views/report.py index f44dd55a..bcd7b508 100644 --- a/wagtail_localize/views/report.py +++ b/wagtail_localize/views/report.py @@ -100,11 +100,7 @@ def filter(self, qs, value): def _get_locale_choices(): - choices = [ - (language_code, display_name) - for language_code, display_name in get_content_languages().items() - ] - return choices + return list(get_content_languages().items()) class LocaleFilter(django_filters.ChoiceFilter): @@ -124,13 +120,13 @@ class TranslationsReportFilterSet(WagtailFilterSet): field_name="source__locale", choices=_get_locale_choices, empty_label=None, - null_label="All", + null_label=gettext_lazy("All"), null_value="all", ) target_locale = LocaleFilter( choices=_get_locale_choices, empty_label=None, - null_label="All", + null_label=gettext_lazy("All"), null_value="all", ) From dcf744ecead4d40cd7f5f53c4324169baedb8e3f Mon Sep 17 00:00:00 2001 From: Jhonatan Lopes Date: Sun, 24 Sep 2023 10:57:59 -0300 Subject: [PATCH 5/5] Test that choices are properly passed to filter fields --- .../tests/test_translations_report.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/wagtail_localize/tests/test_translations_report.py b/wagtail_localize/tests/test_translations_report.py index 43050f72..d423fa01 100644 --- a/wagtail_localize/tests/test_translations_report.py +++ b/wagtail_localize/tests/test_translations_report.py @@ -167,3 +167,28 @@ def test_filter_by_target_locale_all_options(self): self.assertIn(self.de_homepage_translation, response.context["object_list"]) self.assertIn(self.blog_index_translation, response.context["object_list"]) self.assertIn(self.blog_post_translation, response.context["object_list"]) + + def test_locale_filters_get_proper_choices(self): + response = self.client.get(reverse("wagtail_localize:translations_report")) + + self.assertEqual( + list(response.context["filters"].form.fields["source_locale"].choices), + [ + ("all", "All"), + ("en", "English"), + ("fr", "French"), + ("de", "German"), + ("es", "Spanish"), + ], + ) + + self.assertEqual( + list(response.context["filters"].form.fields["target_locale"].choices), + [ + ("all", "All"), + ("en", "English"), + ("fr", "French"), + ("de", "German"), + ("es", "Spanish"), + ], + )