Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale filter selector based on installed languages #716

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
47 changes: 47 additions & 0 deletions wagtail_localize/tests/test_translations_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
zerolab marked this conversation as resolved.
Show resolved Hide resolved
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"
Expand All @@ -145,3 +156,39 @@ 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"])

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"),
],
)
24 changes: 20 additions & 4 deletions wagtail_localize/views/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -98,9 +99,13 @@ def filter(self, qs, value):
)


class LocaleFilter(django_filters.CharFilter):
def _get_locale_choices():
return list(get_content_languages().items())


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})
Expand All @@ -111,8 +116,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=gettext_lazy("All"),
null_value="all",
)
target_locale = LocaleFilter(
choices=_get_locale_choices,
empty_label=None,
null_label=gettext_lazy("All"),
null_value="all",
)

class Meta:
model = Translation
Expand Down