kopia lustrzana https://github.com/wagtail/wagtail
Add an empty choice for the Locale filter for choosers
it is optional after allpull/13043/head
rodzic
c772c02e71
commit
cee29236d8
|
@ -133,13 +133,13 @@ class LocaleFilterMixin(forms.Form):
|
|||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
locales = Locale.objects.all()
|
||||
if locales:
|
||||
self.fields["locale"] = forms.ChoiceField(
|
||||
choices=[
|
||||
choices = [
|
||||
(locale.language_code, locale.get_display_name())
|
||||
for locale in locales
|
||||
],
|
||||
for locale in Locale.objects.all()
|
||||
]
|
||||
if choices:
|
||||
self.fields["locale"] = forms.ChoiceField(
|
||||
choices=[("", _("All")), *choices],
|
||||
required=False,
|
||||
widget=forms.Select(attrs={"data-chooser-modal-search-filter": True}),
|
||||
)
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
import json
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
from wagtail.admin import widgets
|
||||
from wagtail.models import Locale
|
||||
from wagtail.test.snippets.models import TranslatableSnippet
|
||||
from wagtail.test.testapp.models import Advert
|
||||
from wagtail.test.testapp.views import AdvertChooserWidget
|
||||
from wagtail.test.utils.wagtail_tests import WagtailTestUtils
|
||||
|
||||
|
||||
class TestChooserViewSetWithFilteredObjects(WagtailTestUtils, TestCase):
|
||||
def setUp(self):
|
||||
self.user = self.login()
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
Advert.objects.create(text="Head On, apply directly to the forehead")
|
||||
|
||||
advert2 = Advert.objects.create(
|
||||
|
@ -19,6 +21,9 @@ class TestChooserViewSetWithFilteredObjects(WagtailTestUtils, TestCase):
|
|||
)
|
||||
advert2.tags.add("animated")
|
||||
|
||||
def setUp(self):
|
||||
self.user = self.login()
|
||||
|
||||
def test_get(self):
|
||||
response = self.client.get("/admin/animated_advert_chooser/")
|
||||
response_html = json.loads(response.content)["html"]
|
||||
|
@ -54,3 +59,32 @@ class TestChooserViewSetWithFilteredObjects(WagtailTestUtils, TestCase):
|
|||
"linkedFields": {"url": "#id_cool_url"},
|
||||
},
|
||||
)
|
||||
|
||||
@override_settings(WAGTAIL_I18N_ENABLED=True)
|
||||
def test_filter_by_locale(self):
|
||||
fr_locale = Locale.objects.create(language_code="fr")
|
||||
|
||||
TranslatableSnippet.objects.create(text="English snippet")
|
||||
TranslatableSnippet.objects.create(text="French snippet", locale=fr_locale)
|
||||
|
||||
chooser_url = reverse(
|
||||
"wagtailsnippetchoosers_snippetstests_translatablesnippet:choose"
|
||||
)
|
||||
response = self.client.get(chooser_url)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
soup = self.get_soup(json.loads(response.content)["html"])
|
||||
|
||||
options = soup.select("select[name=locale] option")
|
||||
self.assertEqual(len(options), 3)
|
||||
self.assertListEqual(
|
||||
[(option["value"], option.text) for option in options],
|
||||
[("", "All"), ("en", "English"), ("fr", "French")],
|
||||
)
|
||||
|
||||
self.assertEqual(len(response.context["results"]), 2)
|
||||
self.assertEqual(response.context["results"][0].text, "English snippet")
|
||||
self.assertEqual(response.context["results"][1].text, "French snippet")
|
||||
|
||||
response = self.client.get(f"{chooser_url}?locale=en")
|
||||
self.assertEqual(len(response.context["results"]), 1)
|
||||
self.assertEqual(response.context["results"][0].text, "English snippet")
|
||||
|
|
Ładowanie…
Reference in New Issue