mirror of
https://github.com/wagtail/wagtail.git
synced 2024-11-29 09:33:54 +01:00
Moved SearchableListMixin to wagtailadmin (#3970)
* Moved SearchableListMixin to wagtailadmin * Add wagtailsearch support to SearchableListMixin * Cleanup in SearchableListLixin As per https://github.com/wagtail/wagtail/pull/3970#discussion_r146959286
This commit is contained in:
parent
d6df75e1d2
commit
c3022c23b5
42
wagtail/wagtailadmin/views/mixins.py
Normal file
42
wagtail/wagtailadmin/views/mixins.py
Normal file
@ -0,0 +1,42 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from wagtail.wagtailadmin.forms import SearchForm
|
||||
from wagtail.wagtailsearch.backends import get_search_backend
|
||||
from wagtail.wagtailsearch.index import class_is_indexed
|
||||
|
||||
|
||||
class SearchableListMixin(object):
|
||||
search_box_placeholder = _("Search")
|
||||
search_fields = None
|
||||
|
||||
def get_search_form(self):
|
||||
return SearchForm(self.request.GET if self.request.GET.get('q') else None, placeholder=self.search_box_placeholder)
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super(SearchableListMixin, self).get_queryset()
|
||||
search_form = self.get_search_form()
|
||||
|
||||
if search_form.is_valid():
|
||||
q = search_form.cleaned_data['q']
|
||||
|
||||
if class_is_indexed(queryset.model):
|
||||
search_backend = get_search_backend()
|
||||
queryset = search_backend.search(q, queryset, fields=self.search_fields)
|
||||
else:
|
||||
filters = {
|
||||
field + '__icontains': q
|
||||
for field in self.search_fields or []
|
||||
}
|
||||
|
||||
queryset = queryset.filter(**filters)
|
||||
|
||||
return queryset
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
if 'search_form' not in kwargs:
|
||||
kwargs['search_form'] = self.get_search_form()
|
||||
kwargs['is_searching'] = bool(self.request.GET.get('q'))
|
||||
|
||||
return super(SearchableListMixin, self).get_context_data(**kwargs)
|
@ -3,8 +3,7 @@ from __future__ import absolute_import, unicode_literals
|
||||
from django.contrib.auth.models import Group
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from wagtail.wagtailadmin.forms import SearchForm
|
||||
from wagtail.wagtailadmin.views import generic
|
||||
from wagtail.wagtailadmin.views import generic, mixins
|
||||
from wagtail.wagtailadmin.viewsets.model import ModelViewSet
|
||||
from wagtail.wagtailcore import hooks
|
||||
from wagtail.wagtailusers.forms import GroupForm, GroupPagePermissionFormSet
|
||||
@ -50,41 +49,7 @@ class PermissionPanelFormsMixin(object):
|
||||
return super(PermissionPanelFormsMixin, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class SearchableListMixin(object):
|
||||
search_box_placeholder = _("Search")
|
||||
search_fields = []
|
||||
|
||||
def get_search_form(self):
|
||||
if self.request.GET.get('q'):
|
||||
return SearchForm(self.request.GET, placeholder=self.search_box_placeholder)
|
||||
else:
|
||||
return SearchForm(placeholder=self.search_box_placeholder)
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super(SearchableListMixin, self).get_queryset()
|
||||
search_form = self.get_search_form()
|
||||
|
||||
if search_form.is_valid():
|
||||
q = search_form.cleaned_data['q']
|
||||
|
||||
filters = {
|
||||
field + '__icontains': q
|
||||
for field in self.search_fields
|
||||
}
|
||||
|
||||
queryset = queryset.filter(**filters)
|
||||
|
||||
return queryset
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
if 'search_form' not in kwargs:
|
||||
kwargs['search_form'] = self.get_search_form()
|
||||
kwargs['is_searching'] = bool(self.request.GET.get('q'))
|
||||
|
||||
return super(SearchableListMixin, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class IndexView(SearchableListMixin, generic.IndexView):
|
||||
class IndexView(mixins.SearchableListMixin, generic.IndexView):
|
||||
page_title = _("Groups")
|
||||
add_item_label = _("Add a group")
|
||||
search_box_placeholder = _("Search groups")
|
||||
|
Loading…
Reference in New Issue
Block a user