0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Migrate snippet chooser search form to get_filter_form_class

to match generic implementation in wagtail.admin.views.generic.chooser
This commit is contained in:
Matt Westcott 2022-06-20 20:09:35 +01:00 committed by LB (Ben Johnston)
parent fe700b9292
commit 4f511f0fd4
2 changed files with 48 additions and 23 deletions

View File

@ -3,15 +3,19 @@
{% include "wagtailadmin/shared/header.html" with title=choose_str subtitle=model_opts.verbose_name icon="snippet" %} {% include "wagtailadmin/shared/header.html" with title=choose_str subtitle=model_opts.verbose_name icon="snippet" %}
<div class="nice-padding"> <div class="nice-padding">
{% if is_searchable or locale or locale_options %} {% if filter_form.fields or locale or locale_options %}
<form data-chooser-modal-search class="search-bar" action="{% url 'wagtailsnippets:choose_results' model_opts.app_label model_opts.model_name %}" method="GET" novalidate> <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 %} {% if locale %}
<input type="hidden" name="locale" value="{{ locale.language_code }}"> <input type="hidden" name="locale" value="{{ locale.language_code }}">
{% endif %} {% endif %}
{% if is_searchable %} {% if filter_form.visible_fields %}
<ul class="fields"> <ul class="fields">
{% for field in search_form %} {% for field in filter_form.visible_fields %}
{% include "wagtailadmin/shared/field_as_li.html" with field=field %} {% include "wagtailadmin/shared/field_as_li.html" with field=field %}
{% endfor %} {% endfor %}
<li class="submit"><input type="submit" value="{% trans 'Search' %}" class="button" /></li> <li class="submit"><input type="submit" value="{% trans 'Search' %}" class="button" /></li>

View File

@ -1,3 +1,4 @@
from django import forms
from django.contrib.admin.utils import quote, unquote from django.contrib.admin.utils import quote, unquote
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@ -6,7 +7,6 @@ from django.urls import reverse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.views.generic.base import ContextMixin, View from django.views.generic.base import ContextMixin, View
from wagtail.admin.forms.search import SearchForm
from wagtail.admin.modal_workflow import render_modal_workflow from wagtail.admin.modal_workflow import render_modal_workflow
from wagtail.admin.ui.tables import Table, TitleColumn from wagtail.admin.ui.tables import Table, TitleColumn
from wagtail.models import Locale, TranslatableMixin from wagtail.models import Locale, TranslatableMixin
@ -35,6 +35,8 @@ class SnippetTitleColumn(TitleColumn):
class BaseChooseView(ContextMixin, View): class BaseChooseView(ContextMixin, View):
filter_form_class = None
def get_object_list(self): def get_object_list(self):
objects = self.model.objects.all() objects = self.model.objects.all()
@ -45,6 +47,40 @@ class BaseChooseView(ContextMixin, View):
return objects return objects
def get_filter_form_class(self):
if self.filter_form_class:
return self.filter_form_class
else:
fields = {}
if class_is_indexed(self.model):
placeholder = _("Search %(snippet_type_name)s") % {
"snippet_type_name": self.model._meta.verbose_name
}
fields["q"] = forms.CharField(
label=_("Search term"),
widget=forms.TextInput(attrs={"placeholder": placeholder}),
required=False,
)
return type(
"FilterForm",
(forms.Form,),
fields,
)
def get_filter_form(self):
FilterForm = self.get_filter_form_class()
return FilterForm(self.request.GET)
def filter_object_list(self, objects, form):
self.search_query = form.cleaned_data.get("q")
if self.search_query:
search_backend = get_search_backend()
objects = search_backend.search(self.search_query, objects)
self.is_searching = True
return objects
def get(self, request, app_label, model_name): def get(self, request, app_label, model_name):
self.model = get_snippet_model_from_url_params(app_label, model_name) self.model = get_snippet_model_from_url_params(app_label, model_name)
@ -76,25 +112,10 @@ class BaseChooseView(ContextMixin, View):
self.is_searchable = class_is_indexed(self.model) self.is_searchable = class_is_indexed(self.model)
self.is_searching = False self.is_searching = False
self.search_query = None self.search_query = None
if self.is_searchable and "q" in request.GET:
self.search_form = SearchForm(
request.GET,
placeholder=_("Search %(snippet_type_name)s")
% {"snippet_type_name": self.model._meta.verbose_name},
)
if self.search_form.is_valid(): self.filter_form = self.get_filter_form()
self.search_query = self.search_form.cleaned_data["q"] if self.filter_form.is_valid():
objects = self.filter_object_list(objects, self.filter_form)
search_backend = get_search_backend()
objects = search_backend.search(self.search_query, objects)
self.is_searching = True
else:
self.search_form = SearchForm(
placeholder=_("Search %(snippet_type_name)s")
% {"snippet_type_name": self.model._meta.verbose_name}
)
# Pagination # Pagination
paginator = Paginator(objects, per_page=25) paginator = Paginator(objects, per_page=25)
@ -142,7 +163,7 @@ class ChooseView(BaseChooseView):
context.update( context.update(
{ {
"is_searchable": self.is_searchable, "is_searchable": self.is_searchable,
"search_form": self.search_form, "filter_form": self.filter_form,
"locale": self.locale, "locale": self.locale,
"locale_filter": self.locale_filter, "locale_filter": self.locale_filter,
"selected_locale": self.selected_locale, "selected_locale": self.selected_locale,