|
|
@@ -1,5 +1,4 @@
|
|
1
|
1
|
from django.core.paginator import InvalidPage, Paginator
|
|
2
|
|
-from django.utils.translation import gettext_lazy as _
|
|
3
|
2
|
from haystack import connections
|
|
4
|
3
|
|
|
5
|
4
|
from oscar.core.loading import get_class
|
|
|
@@ -9,7 +8,39 @@ from . import facets
|
|
9
|
8
|
FacetMunger = get_class('search.facets', 'FacetMunger')
|
|
10
|
9
|
|
|
11
|
10
|
|
|
12
|
|
-class SearchHandler(object):
|
|
|
11
|
+class SearchResultsPaginationMixin:
|
|
|
12
|
+ paginate_by = None
|
|
|
13
|
+ paginator_class = Paginator
|
|
|
14
|
+ page_kwarg = 'page'
|
|
|
15
|
+
|
|
|
16
|
+ def paginate_queryset(self, queryset, page_size):
|
|
|
17
|
+ """
|
|
|
18
|
+ Paginate the search results. This is a simplified version of
|
|
|
19
|
+ Django's MultipleObjectMixin.paginate_queryset
|
|
|
20
|
+ """
|
|
|
21
|
+ paginator = self.get_paginator(queryset, page_size)
|
|
|
22
|
+ page_kwarg = self.page_kwarg
|
|
|
23
|
+ page_number = self.request_data.get(page_kwarg, 1)
|
|
|
24
|
+ try:
|
|
|
25
|
+ page_number = int(page_number)
|
|
|
26
|
+ except ValueError:
|
|
|
27
|
+ if page_number == 'last':
|
|
|
28
|
+ page_number = paginator.num_pages
|
|
|
29
|
+ else:
|
|
|
30
|
+ raise InvalidPage
|
|
|
31
|
+ # This can also raise an InvalidPage exception.
|
|
|
32
|
+ page = paginator.page(page_number)
|
|
|
33
|
+ return paginator, page, page.object_list, page.has_other_pages()
|
|
|
34
|
+
|
|
|
35
|
+ def get_paginator(self, queryset, per_page=None):
|
|
|
36
|
+ """
|
|
|
37
|
+ Return a paginator. Override this to set settings like orphans,
|
|
|
38
|
+ allow_empty, etc.
|
|
|
39
|
+ """
|
|
|
40
|
+ return self.paginator_class(queryset, per_page)
|
|
|
41
|
+
|
|
|
42
|
+
|
|
|
43
|
+class SearchHandler(SearchResultsPaginationMixin):
|
|
13
|
44
|
"""
|
|
14
|
45
|
A class that is concerned with performing a search and paginating the
|
|
15
|
46
|
results. The search is triggered upon initialisation (mainly to have a
|
|
|
@@ -36,9 +67,6 @@ class SearchHandler(object):
|
|
36
|
67
|
|
|
37
|
68
|
form_class = None
|
|
38
|
69
|
model_whitelist = None
|
|
39
|
|
- paginate_by = None
|
|
40
|
|
- paginator_class = Paginator
|
|
41
|
|
- page_kwarg = 'page'
|
|
42
|
70
|
|
|
43
|
71
|
def __init__(self, request_data, full_path):
|
|
44
|
72
|
self.full_path = full_path
|
|
|
@@ -51,8 +79,7 @@ class SearchHandler(object):
|
|
51
|
79
|
self.results = self.get_search_results(self.search_form)
|
|
52
|
80
|
# If below raises an UnicodeDecodeError, you're running pysolr < 3.2
|
|
53
|
81
|
# with Solr 4.
|
|
54
|
|
- self.paginator, self.page = self.paginate_queryset(
|
|
55
|
|
- self.results, request_data)
|
|
|
82
|
+ self.paginator, self.page = self.paginate_queryset(self.results, self.paginate_by)[0:2]
|
|
56
|
83
|
|
|
57
|
84
|
# Search related methods
|
|
58
|
85
|
|
|
|
@@ -85,34 +112,6 @@ class SearchHandler(object):
|
|
85
|
112
|
sqs = sqs.models(*self.model_whitelist)
|
|
86
|
113
|
return sqs
|
|
87
|
114
|
|
|
88
|
|
- # Pagination related methods
|
|
89
|
|
-
|
|
90
|
|
- def paginate_queryset(self, queryset, request_data):
|
|
91
|
|
- """
|
|
92
|
|
- Paginate the search results. This is a simplified version of
|
|
93
|
|
- Django's MultipleObjectMixin.paginate_queryset
|
|
94
|
|
- """
|
|
95
|
|
- paginator = self.get_paginator(queryset)
|
|
96
|
|
- page_kwarg = self.page_kwarg
|
|
97
|
|
- page = request_data.get(page_kwarg, 1)
|
|
98
|
|
- try:
|
|
99
|
|
- page_number = int(page)
|
|
100
|
|
- except ValueError:
|
|
101
|
|
- if page == 'last':
|
|
102
|
|
- page_number = paginator.num_pages
|
|
103
|
|
- else:
|
|
104
|
|
- raise InvalidPage(_(
|
|
105
|
|
- "Page is not 'last', nor can it be converted to an int."))
|
|
106
|
|
- # This can also raise an InvalidPage exception.
|
|
107
|
|
- return paginator, paginator.page(page_number)
|
|
108
|
|
-
|
|
109
|
|
- def get_paginator(self, queryset):
|
|
110
|
|
- """
|
|
111
|
|
- Return a paginator. Override this to set settings like orphans,
|
|
112
|
|
- allow_empty, etc.
|
|
113
|
|
- """
|
|
114
|
|
- return self.paginator_class(queryset, self.paginate_by)
|
|
115
|
|
-
|
|
116
|
115
|
# Accessing the search results and meta data
|
|
117
|
116
|
|
|
118
|
117
|
def bulk_fetch_results(self, paginated_results):
|