mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Implement support for ordering and searching at the same time over the API (#2732)
When the API was originally created, this was not possible to do with Wagtail search. So I added a check to prevent people from doing it. Custom ordering was implemented in Wagtailsearch in #1815 so this can now be switched on.
This commit is contained in:
parent
e3483a8bd3
commit
e592cbf65c
@ -49,10 +49,6 @@ class OrderingFilter(BaseFilterBackend):
|
|||||||
Eg: ?order=random
|
Eg: ?order=random
|
||||||
"""
|
"""
|
||||||
if 'order' in request.GET:
|
if 'order' in request.GET:
|
||||||
# Prevent ordering while searching
|
|
||||||
if 'search' in request.GET:
|
|
||||||
raise BadRequestError("ordering with a search query is not supported")
|
|
||||||
|
|
||||||
order_by = request.GET['order']
|
order_by = request.GET['order']
|
||||||
|
|
||||||
# Random ordering
|
# Random ordering
|
||||||
@ -102,9 +98,10 @@ class SearchFilter(BaseFilterBackend):
|
|||||||
|
|
||||||
search_query = request.GET['search']
|
search_query = request.GET['search']
|
||||||
search_operator = request.GET.get('search_operator', None)
|
search_operator = request.GET.get('search_operator', None)
|
||||||
|
order_by_relevance = 'order' not in request.GET
|
||||||
|
|
||||||
sb = get_search_backend()
|
sb = get_search_backend()
|
||||||
queryset = sb.search(search_query, queryset, operator=search_operator)
|
queryset = sb.search(search_query, queryset, operator=search_operator, order_by_relevance=order_by_relevance)
|
||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
@ -269,12 +269,13 @@ class TestDocumentListing(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(set(document_id_list), set([2]))
|
self.assertEqual(set(document_id_list), set([2]))
|
||||||
|
|
||||||
def test_search_when_ordering_gives_error(self):
|
def test_search_with_order(self):
|
||||||
response = self.get_response(search='james', order='title')
|
response = self.get_response(search='james', order='title')
|
||||||
content = json.loads(response.content.decode('UTF-8'))
|
content = json.loads(response.content.decode('UTF-8'))
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 400)
|
document_id_list = self.get_document_id_list(content)
|
||||||
self.assertEqual(content, {'message': "ordering with a search query is not supported"})
|
|
||||||
|
self.assertEqual(document_id_list, [2])
|
||||||
|
|
||||||
@override_settings(WAGTAILAPI_SEARCH_ENABLED=False)
|
@override_settings(WAGTAILAPI_SEARCH_ENABLED=False)
|
||||||
def test_search_when_disabled_gives_error(self):
|
def test_search_when_disabled_gives_error(self):
|
||||||
|
@ -268,12 +268,13 @@ class TestImageListing(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(set(image_id_list), set([5]))
|
self.assertEqual(set(image_id_list), set([5]))
|
||||||
|
|
||||||
def test_search_when_ordering_gives_error(self):
|
def test_search_with_order(self):
|
||||||
response = self.get_response(search='james', order='title')
|
response = self.get_response(search='james', order='title')
|
||||||
content = json.loads(response.content.decode('UTF-8'))
|
content = json.loads(response.content.decode('UTF-8'))
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 400)
|
image_id_list = self.get_image_id_list(content)
|
||||||
self.assertEqual(content, {'message': "ordering with a search query is not supported"})
|
|
||||||
|
self.assertEqual(image_id_list, [5])
|
||||||
|
|
||||||
@override_settings(WAGTAILAPI_SEARCH_ENABLED=False)
|
@override_settings(WAGTAILAPI_SEARCH_ENABLED=False)
|
||||||
def test_search_when_disabled_gives_error(self):
|
def test_search_when_disabled_gives_error(self):
|
||||||
|
@ -552,12 +552,13 @@ class TestPageListing(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(set(page_id_list), set([16, 18, 19]))
|
self.assertEqual(set(page_id_list), set([16, 18, 19]))
|
||||||
|
|
||||||
def test_search_when_ordering_gives_error(self):
|
def test_search_with_order(self):
|
||||||
response = self.get_response(search='blog', order='title')
|
response = self.get_response(search='blog', order='title')
|
||||||
content = json.loads(response.content.decode('UTF-8'))
|
content = json.loads(response.content.decode('UTF-8'))
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 400)
|
page_id_list = self.get_page_id_list(content)
|
||||||
self.assertEqual(content, {'message': "ordering with a search query is not supported"})
|
|
||||||
|
self.assertEqual(page_id_list, [19, 5, 16, 18])
|
||||||
|
|
||||||
@override_settings(WAGTAILAPI_SEARCH_ENABLED=False)
|
@override_settings(WAGTAILAPI_SEARCH_ENABLED=False)
|
||||||
def test_search_when_disabled_gives_error(self):
|
def test_search_when_disabled_gives_error(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user