Exemplo n.º 1
0
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
Exemplo n.º 2
0
    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()
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
    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()
Exemplo n.º 6
0
    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')
Exemplo n.º 7
0
    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()
Exemplo n.º 8
0
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_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')
Exemplo n.º 10
0
    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')
Exemplo n.º 11
0
    def test_single_hit(self):
        """Test the ESPaginator only queries ES one time."""
        mocked_qs = MagicMock()
        mocked_qs.count.return_value = 42
        paginator = Paginator(mocked_qs, 5)
        # With the base paginator, requesting any page forces a count.
        paginator.page(1)
        assert paginator.count == 42
        assert mocked_qs.count.call_count == 1

        mocked_qs = MagicMock()
        mocked_qs.__getitem__().execute().hits.total = 666
        paginator = ESPaginator(mocked_qs, 5)
        # With the ES paginator, the count is fetched from the 'total' key
        # in the results.
        paginator.page(1)
        assert paginator.count == 666
        assert mocked_qs.count.call_count == 0