mirror of
https://github.com/wagtail/wagtail.git
synced 2024-11-29 17:36:49 +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:
parent
fe700b9292
commit
4f511f0fd4
@ -3,15 +3,19 @@
|
||||
{% include "wagtailadmin/shared/header.html" with title=choose_str subtitle=model_opts.verbose_name icon="snippet" %}
|
||||
|
||||
<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>
|
||||
{% 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 is_searchable %}
|
||||
{% if filter_form.visible_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 %}
|
||||
{% endfor %}
|
||||
<li class="submit"><input type="submit" value="{% trans 'Search' %}" class="button" /></li>
|
||||
|
@ -1,3 +1,4 @@
|
||||
from django import forms
|
||||
from django.contrib.admin.utils import quote, unquote
|
||||
from django.core.paginator import Paginator
|
||||
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.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.ui.tables import Table, TitleColumn
|
||||
from wagtail.models import Locale, TranslatableMixin
|
||||
@ -35,6 +35,8 @@ class SnippetTitleColumn(TitleColumn):
|
||||
|
||||
|
||||
class BaseChooseView(ContextMixin, View):
|
||||
filter_form_class = None
|
||||
|
||||
def get_object_list(self):
|
||||
objects = self.model.objects.all()
|
||||
|
||||
@ -45,6 +47,40 @@ class BaseChooseView(ContextMixin, View):
|
||||
|
||||
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):
|
||||
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_searching = False
|
||||
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.search_query = self.search_form.cleaned_data["q"]
|
||||
|
||||
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}
|
||||
)
|
||||
self.filter_form = self.get_filter_form()
|
||||
if self.filter_form.is_valid():
|
||||
objects = self.filter_object_list(objects, self.filter_form)
|
||||
|
||||
# Pagination
|
||||
paginator = Paginator(objects, per_page=25)
|
||||
@ -142,7 +163,7 @@ class ChooseView(BaseChooseView):
|
||||
context.update(
|
||||
{
|
||||
"is_searchable": self.is_searchable,
|
||||
"search_form": self.search_form,
|
||||
"filter_form": self.filter_form,
|
||||
"locale": self.locale,
|
||||
"locale_filter": self.locale_filter,
|
||||
"selected_locale": self.selected_locale,
|
||||
|
Loading…
Reference in New Issue
Block a user