def build_page(self): """For some reason occasionally the default pagination contains Nones (probably a bug in django-haystack. We don't want that.""" # Copied from super() try: page_no = int(self.request.GET.get('page', 1)) except (TypeError, ValueError): raise Http404("Not a valid number for page.") if page_no < 1: raise Http404("Pages should be 1 or greater.") start_offset = (page_no - 1) * self.results_per_page self.results = self.results.load_all() # Force haystack to pull all 'Product' objects at the same time self.results[start_offset:start_offset + self.results_per_page] paginator = Paginator(self.results, self.results_per_page) # My stuff try: page = paginator.page(page_no) except EmptyPage: page = Page([], page_no, paginator) page.object_list = [o for o in page.object_list if o is not None] return paginator, page
def page(self, number): """ Returns a page object. This class overrides the default behavior and ignores "orphans" and assigns the count from the ES result to the Paginator. """ number = self.validate_number(number) bottom = (number - 1) * self.per_page top = bottom + self.per_page page = Page(self.object_list[bottom:top], number, self) # Force the search to evaluate and then attach the count. We want to # avoid an extra useless query even if there are no results, so we # directly fetch the count from hits. # Overwrite `object_list` with the list of ES results. page.object_list = page.object_list.execute().hits # Update the `_count`. self._count = page.object_list.total # Now that we have the count validate that the page number isn't higher # than the possible number of pages and adjust accordingly. if number > self.num_pages: if number == 1 and self.allow_empty_first_page: pass else: raise EmptyPage('That page contains no results') return page
def _get_page(self, *args, **kwargs): page = Page(*args, **kwargs) if page.has_next(): page.next_page_first_item = page[-1] page.object_list = page.object_list[:-1] else: page.next_page_first_item = None return page
def page(self, number): """ Returns a page object. This class overrides the default behavior and ignores "orphans" and assigns the count from the ES result to the Paginator. """ number = self.validate_number(number) bottom = (number - 1) * self.per_page top = bottom + self.per_page page = Page(self.object_list[bottom:top], number, self) # Force the search to evaluate and then attach the count. We want to # avoid an extra useless query even if there are no results, so we # directly fetch the count from hits. # Overwrite `object_list` with the list of ES results. page.object_list = page.object_list.execute().hits # Update the `_count`. self._count = page.object_list.total return page
def check_if_liked_page(user: User, page: Page): page.object_list = [ check_if_liked_post(user, post) for post in page.object_list ] return page