Move locale chooser into filter_form

Remove the hidden 'locale' field, as it's not clear why it's needed - there's no UI mechanism for reverting back to the locale we initially opened the modal on, so there's no reason to persist it between modal view loads.
pull/8711/head
Matt Westcott 2022-06-20 23:31:12 +01:00 zatwierdzone przez LB (Ben Johnston)
rodzic 4f511f0fd4
commit 8735472ace
3 zmienionych plików z 30 dodań i 60 usunięć

Wyświetl plik

@ -3,16 +3,12 @@
{% include "wagtailadmin/shared/header.html" with title=choose_str subtitle=model_opts.verbose_name icon="snippet" %}
<div class="nice-padding">
{% if filter_form.fields or locale or locale_options %}
{% if filter_form.fields %}
<form data-chooser-modal-search class="search-bar" action="{% url 'wagtailsnippets:choose_results' model_opts.app_label model_opts.model_name %}" method="GET" novalidate>
{% if filter_form.hidden_fields %}
{% for field in form.hidden_fields %}{{ field }}{% endfor %}
{% endif %}
{% if locale %}
<input type="hidden" name="locale" value="{{ locale.language_code }}">
{% endif %}
{% if filter_form.visible_fields %}
<ul class="fields">
{% for field in filter_form.visible_fields %}
@ -21,20 +17,6 @@
<li class="submit"><input type="submit" value="{% trans 'Search' %}" class="button" /></li>
</ul>
{% endif %}
{% if locale_options %}
<ul class="fields">
<li class="col">
<div class="field field-small">
<select data-chooser-modal-search-filter name="locale_filter">
{% for locale_option in locale_options %}
<option value="{{ locale_option.language_code }}"{% if locale_option == selected_locale %} selected{% endif %}>{{ locale_option.get_display_name }}</option>
{% endfor %}
</select>
</div>
</li>
</ul>
{% endif %}
</form>
{% endif %}

Wyświetl plik

@ -1763,7 +1763,8 @@ class TestSnippetChoose(TestCase, WagtailTestUtils):
self.assertTemplateUsed(response, "wagtailsnippets/chooser/choose.html")
def test_not_searchable(self):
self.assertFalse(self.get().context["is_searchable"])
# filter_form should not have a search field
self.assertFalse(self.get().context["filter_form"].fields.get("q"))
@override_settings(WAGTAIL_I18N_ENABLED=True)
def test_filter_by_locale(self):
@ -1776,10 +1777,9 @@ class TestSnippetChoose(TestCase, WagtailTestUtils):
response = self.get()
# Check the filter is added
self.assertIn(
'<select data-chooser-modal-search-filter name="locale_filter">',
response.json()["html"],
)
response_html = response.json()["html"]
self.assertIn("data-chooser-modal-search-filter", response_html)
self.assertIn('name="locale"', response_html)
# Check both snippets are shown
self.assertEqual(len(response.context["items"]), 2)
@ -1847,7 +1847,8 @@ class TestSnippetChooseWithSearchableSnippet(TestCase, WagtailTestUtils):
self.assertIn(self.snippet_c, items)
def test_is_searchable(self):
self.assertTrue(self.get().context["is_searchable"])
# filter_form should have a search field
self.assertTrue(self.get().context["filter_form"].fields.get("q"))
def test_search_hello(self):
response = self.get({"q": "Hello"})

Wyświetl plik

@ -62,6 +62,20 @@ class BaseChooseView(ContextMixin, View):
required=False,
)
if issubclass(self.model, TranslatableMixin):
locales = Locale.objects.all()
if locales:
fields["locale"] = forms.ChoiceField(
choices=[
(locale.language_code, locale.get_display_name())
for locale in locales
],
required=False,
widget=forms.Select(
attrs={"data-chooser-modal-search-filter": True}
),
)
return type(
"FilterForm",
(forms.Form,),
@ -73,6 +87,13 @@ class BaseChooseView(ContextMixin, View):
return FilterForm(self.request.GET)
def filter_object_list(self, objects, form):
selected_locale_code = form.cleaned_data.get("locale")
if selected_locale_code:
selected_locale = get_object_or_404(
Locale, language_code=selected_locale_code
)
objects = objects.filter(locale=selected_locale)
self.search_query = form.cleaned_data.get("q")
if self.search_query:
search_backend = get_search_backend()
@ -86,28 +107,6 @@ class BaseChooseView(ContextMixin, View):
objects = self.get_object_list()
# Filter by locale
self.locale = None
self.locale_filter = None
self.selected_locale = None
if issubclass(self.model, TranslatableMixin):
# 'locale' is the Locale of the object that this snippet is being chosen for
if request.GET.get("locale"):
self.locale = get_object_or_404(
Locale, language_code=request.GET["locale"]
)
# 'locale_filter' is the current value of the "Locale" selector in the UI
if request.GET.get("locale_filter"):
self.locale_filter = get_object_or_404(
Locale, language_code=request.GET["locale_filter"]
)
self.selected_locale = self.locale_filter or self.locale
if self.selected_locale:
objects = objects.filter(locale=self.selected_locale)
# Search
self.is_searchable = class_is_indexed(self.model)
self.is_searching = False
@ -159,19 +158,7 @@ class BaseChooseView(ContextMixin, View):
class ChooseView(BaseChooseView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update(
{
"is_searchable": self.is_searchable,
"filter_form": self.filter_form,
"locale": self.locale,
"locale_filter": self.locale_filter,
"selected_locale": self.selected_locale,
"locale_options": Locale.objects.all()
if issubclass(self.model, TranslatableMixin)
else [],
}
)
context["filter_form"] = self.filter_form
return context
# Return the choose view as a ModalWorkflow response