0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Build up the filter form with mixins, so that we can reuse the collection filter code

This commit is contained in:
Matt Westcott 2022-07-05 12:54:09 +01:00 committed by Matt Westcott
parent 3caa98abbe
commit ea4e7be178
4 changed files with 40 additions and 51 deletions

View File

@ -44,3 +44,35 @@ class EmailLinkChooserForm(forms.Form):
class PhoneLinkChooserForm(forms.Form):
phone_number = forms.CharField(required=True)
link_text = forms.CharField(required=False)
class SearchFilterMixin(forms.Form):
"""
Mixin for a chooser listing filter form, to provide a search field
"""
q = forms.CharField(
label=_("Search term"),
widget=forms.TextInput(attrs={"placeholder": _("Search")}),
required=False,
)
class CollectionFilterMixin(forms.Form):
"""
Mixin for a chooser listing filter form, to provide a collection filter field.
The view must pass a `collections` keyword argument when constructing the form
"""
def __init__(self, *args, collections=None, **kwargs):
super().__init__(*args, **kwargs)
if collections:
collection_choices = [
("", _("All collections"))
] + collections.get_indented_choices()
self.fields["collection_id"] = forms.ChoiceField(
label=_("Collection"),
choices=collection_choices,
required=False,
)

View File

@ -15,8 +15,10 @@ from django.utils.translation import gettext_lazy as _
from django.views.generic.base import ContextMixin, View
from wagtail.admin.admin_url_finder import AdminURLFinder
from wagtail.admin.forms.choosers import CollectionFilterMixin, SearchFilterMixin
from wagtail.admin.modal_workflow import render_modal_workflow
from wagtail.admin.ui.tables import Table, TitleColumn
from wagtail.models import CollectionMember
from wagtail.permission_policies import BlanketPermissionPolicy, ModelPermissionPolicy
from wagtail.search.backends import get_search_backend
from wagtail.search.index import class_is_indexed
@ -73,16 +75,16 @@ class BaseChooseView(ModalPageFurnitureMixin, ContextMixin, View):
if self.filter_form_class:
return self.filter_form_class
else:
fields = {}
bases = [forms.Form]
if class_is_indexed(self.model):
fields["q"] = forms.CharField(
label=_("Search term"), widget=forms.TextInput(), required=False
)
bases.insert(0, SearchFilterMixin)
if issubclass(self.model, CollectionMember):
bases.insert(0, CollectionFilterMixin)
return type(
"FilterForm",
(forms.Form,),
fields,
tuple(bases),
{},
)
def get_filter_form(self):

View File

@ -66,30 +66,8 @@ class DownloadColumn(Column):
return context
class DocumentFilterForm(forms.Form):
q = forms.CharField(
label=_("Search term"),
widget=forms.TextInput(attrs={"placeholder": _("Search")}),
required=False,
)
def __init__(self, *args, collections, **kwargs):
super().__init__(*args, **kwargs)
if collections:
collection_choices = [
("", _("All collections"))
] + collections.get_indented_choices()
self.fields["collection_id"] = forms.ChoiceField(
label=_("Collection"),
choices=collection_choices,
required=False,
)
class BaseDocumentChooseView(BaseChooseView):
results_template_name = "wagtaildocs/chooser/results.html"
filter_form_class = DocumentFilterForm
per_page = 10
def get_object_list(self):

View File

@ -1,4 +1,3 @@
from django import forms
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
@ -45,27 +44,6 @@ class ImageChosenResponseMixin(ChosenResponseMixin):
return response_data
class ImageFilterForm(forms.Form):
q = forms.CharField(
label=_("Search term"),
widget=forms.TextInput(attrs={"placeholder": _("Search")}),
required=False,
)
def __init__(self, *args, collections, **kwargs):
super().__init__(*args, **kwargs)
if collections:
collection_choices = [
("", _("All collections"))
] + collections.get_indented_choices()
self.fields["collection_id"] = forms.ChoiceField(
label=_("Collection"),
choices=collection_choices,
required=False,
)
class ImageCreationFormMixin(CreationFormMixin):
creation_tab_id = "upload"
create_url_name = "wagtailimages:chooser_upload"
@ -102,7 +80,6 @@ class BaseImageChooseView(BaseChooseView):
results_url_name = "wagtailimages:chooser_results"
template_name = "wagtailimages/chooser/chooser.html"
results_template_name = "wagtailimages/chooser/results.html"
filter_form_class = ImageFilterForm
per_page = getattr(settings, "WAGTAILIMAGES_CHOOSER_PAGE_SIZE", 12)
def get_object_list(self):