-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom search handler classes to ModelAdmin's IndexView
Author: Seb <seb@takeflight.com.au> Date: Sun Apr 7 12:34:00 2019 +1000
- Loading branch information
Showing
12 changed files
with
382 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .button import ButtonHelper, PageButtonHelper # NOQA | ||
from .permission import PagePermissionHelper, PermissionHelper # NOQA | ||
from .url import AdminURLHelper, PageAdminURLHelper # NOQA | ||
from .button import ButtonHelper, PageButtonHelper # NOQA | ||
from .permission import PagePermissionHelper, PermissionHelper # NOQA | ||
from .search import DjangoORMSearchHandler, WagtailBackendSearchHandler # NOQA | ||
from .url import AdminURLHelper, PageAdminURLHelper # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import operator | ||
from functools import reduce | ||
|
||
from django.contrib.admin.utils import lookup_needs_distinct | ||
from django.db.models import Q | ||
|
||
from wagtail.search.backends import get_search_backend | ||
|
||
|
||
class BaseSearchHandler: | ||
def __init__(self, search_fields): | ||
self.search_fields = search_fields | ||
|
||
def search_queryset(self, queryset, search_term, **kwargs): | ||
""" | ||
Returns an iterable of objects from ``queryset`` matching the | ||
provided ``search_term``. | ||
""" | ||
raise NotImplementedError() | ||
|
||
@property | ||
def show_search_form(self): | ||
""" | ||
Returns a boolean that determines whether a search form should be | ||
displayed in the IndexView UI. | ||
""" | ||
return True | ||
|
||
|
||
class DjangoORMSearchHandler(BaseSearchHandler): | ||
def search_queryset(self, queryset, search_term, **kwargs): | ||
if not search_term or not self.search_fields: | ||
return queryset | ||
|
||
orm_lookups = ['%s__icontains' % str(search_field) | ||
for search_field in self.search_fields] | ||
for bit in search_term.split(): | ||
or_queries = [Q(**{orm_lookup: bit}) | ||
for orm_lookup in orm_lookups] | ||
queryset = queryset.filter(reduce(operator.or_, or_queries)) | ||
opts = queryset.model._meta | ||
for search_spec in orm_lookups: | ||
if lookup_needs_distinct(opts, search_spec): | ||
return queryset.distinct() | ||
return queryset | ||
|
||
|
||
@property | ||
def show_search_form(self): | ||
return bool(self.search_fields) | ||
|
||
|
||
class WagtailBackendSearchHandler(BaseSearchHandler): | ||
|
||
default_search_backend = 'default' | ||
|
||
def search_queryset( | ||
self, queryset, search_term, preserve_order=False, operator=None, | ||
partial_match=True, backend=None, **kwargs | ||
): | ||
if not search_term: | ||
return queryset | ||
|
||
backend = get_search_backend(backend or self.default_search_backend) | ||
return backend.search( | ||
search_term, | ||
queryset, | ||
fields=self.search_fields or None, | ||
operator=operator, | ||
partial_match=partial_match, | ||
order_by_relevance=not preserve_order, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
wagtail/contrib/modeladmin/templates/modeladmin/includes/search_form.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.