def paginate(request, queryset, per_page=20, count=None): """ Get a Paginator, abstracting some common paging actions. If you pass ``count``, that value will be used instead of calling ``.count()`` on the queryset. This can be good if the queryset would produce an expensive count query. """ if isinstance(queryset, search.ES): paginator = ESPaginator( queryset, per_page, use_elasticsearch_dsl=False) else: paginator = DjangoPaginator(queryset, per_page) if count is not None: paginator.count = count # Get the page from the request, make sure it's an int. try: page = int(request.GET.get('page', 1)) except ValueError: page = 1 # Get a page of results, or the first page if there's a problem. try: paginated = paginator.page(page) except (EmptyPage, InvalidPage): paginated = paginator.page(1) paginated.url = u'%s?%s' % (request.path, request.GET.urlencode()) return paginated
def test_count_non_dsl_mode(self): p = ESPaginator(Addon.search(), 20, use_elasticsearch_dsl=False) assert p._count is None p.page(1) assert p.count == Addon.search().count()
def test_no_pages_beyond_max_window_result(self): mocked_qs = MagicMock() mocked_qs.__getitem__().execute().hits.total = 30000 paginator = ESPaginator(mocked_qs, 5) assert ESPaginator.max_result_window == 25000 page = paginator.page(4999) assert page.has_next() is True page = paginator.page(5000) assert page.has_next() is False
def test_count_non_dsl_mode(self): addon_factory() addon_factory() addon_factory() self.refresh() p = ESPaginator(Addon.search(), 20, use_elasticsearch_dsl=False) assert p.count == 3 p.page(1) assert p.count == 3 assert p.count == Addon.search().count()
def test_invalid_page(self): mocked_qs = MagicMock() mocked_qs.__getitem__().execute().hits.total = 30000 paginator = ESPaginator(mocked_qs, 5) assert ESPaginator.max_result_window == 25000 with pytest.raises(InvalidPage) as exc: # We're fetching 5 items per page, so requesting page 5001 should # fail, since the max result window should is set to 25000. paginator.page(5000 + 1) # Make sure we raise exactly `InvalidPage`, this is needed # unfortunately since `pytest.raises` won't check the exact # instance but also accepts parent exceptions inherited. assert ( exc.value.message == 'That page number is too high for the current page size') assert isinstance(exc.value, InvalidPage) with self.assertRaises(EmptyPage): paginator.page(0) with self.assertRaises(PageNotAnInteger): paginator.page('lol')
def test_invalid_page(self): mocked_qs = MagicMock() paginator = ESPaginator(mocked_qs, 5) assert ESPaginator.max_result_window == 25000 with self.assertRaises(InvalidPage): # We're fetching 5 items per page, so requesting page 5001 should # fail, since the max result window should is set to 25000. paginator.page(5000 + 1) with self.assertRaises(EmptyPage): paginator.page(0) with self.assertRaises(PageNotAnInteger): paginator.page('lol')