Beispiel #1
0
 def test_defaults(self):
     # Ensure the pages are returned correctly using the default values.
     pages = utils.get_page_numbers(10, 20)
     expected = [
         'previous', 1, 2, 3, None, 8, 9, 10, 11, 12,
         None, 18, 19, 20, 'next']
     self.assertSequenceEqual(expected, pages)
Beispiel #2
0
 def test_arrows(self):
     # Ensure the pages are returned correctly adding first / last arrows.
     pages = utils.get_page_numbers(5, 10, arrows=True)
     expected = [
         'first', 'previous', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'next', 'last'
     ]
     self.assertSequenceEqual(expected, pages)
Beispiel #3
0
def describe_page_numbers(current_page,
                          total_count,
                          per_page,
                          page_numbers_at_ends=3,
                          pages_numbers_around_current=3):
    """
    Produces a description of how to display a paginated list's page numbers. Rather than just
    spitting out a list of every page available, the page numbers returned will be trimmed
    to display only the immediate numbers around the start, end, and the current page.

    :param current_page: the current page number (page numbers should start at 1)
    :param total_count: the total number of items that are being paginated
    :param per_page: the number of items that are displayed per page
    :param page_numbers_at_ends: the amount of page numbers to display at the beginning and end of the list
    :param pages_numbers_around_current: the amount of page numbers to display around the currently selected page

    :return: a dictionary describing the page numbers, relative to the current page
    """
    if total_count:
        page_count = int(math.ceil(1.0 * total_count / per_page))
        if page_count < current_page:
            raise PageNumberOutOfBounds
        page_numbers = get_page_numbers(
            current_page=current_page,
            num_pages=page_count,
            extremes=page_numbers_at_ends,
            arounds=pages_numbers_around_current,
        )
    else:
        page_count = 0
        page_numbers = []

    return {
        'numbers':
        [num for num in page_numbers if not isinstance(num, six.string_types)],
        'has_previous':
        'previous' in page_numbers,
        'has_next':
        'next' in page_numbers,
        'current_page':
        current_page,
        'previous_page':
        current_page - 1,
        'next_page':
        current_page + 1,
        'total_count':
        total_count,
        'page_count':
        page_count,
        'per_page':
        per_page,
    }
def page_list_callable_arrows(number, num_pages):
    """Wrap ``el_pagination.utils.get_page_numbers``.

    Set first / last page arrows to True.
    """
    return utils.get_page_numbers(number, num_pages, arrows=True)
Beispiel #5
0
def page_list_callable_arrows(number, num_pages):
    """Wrap ``el_pagination.utils.get_page_numbers``.

    Set first / last page arrows to True.
    """
    return utils.get_page_numbers(number, num_pages, arrows=True)
Beispiel #6
0
 def test_no_extremes_arounds(self):
     # Ensure the correct pages are returned with no extremes and arounds.
     pages = utils.get_page_numbers(10, 20, extremes=0, arounds=0)
     expected = ['previous', 10, 'next']
     self.assertSequenceEqual(expected, pages)
Beispiel #7
0
 def test_no_arounds(self):
     # Ensure the correct pages are returned with no arounds.
     pages = utils.get_page_numbers(10, 20, arounds=0)
     expected = ['previous', 1, 2, 3, None, 10, None, 18, 19, 20, 'next']
     self.assertSequenceEqual(expected, pages)
Beispiel #8
0
 def test_last_page(self):
     # Ensure the correct pages are returned if the last page is requested.
     pages = utils.get_page_numbers(10, 10)
     expected = ['previous', 1, 2, 3, None, 8, 9, 10]
     self.assertSequenceEqual(expected, pages)
Beispiel #9
0
 def test_first_page(self):
     # Ensure the correct pages are returned if the first page is requested.
     pages = utils.get_page_numbers(1, 10)
     expected = [1, 2, 3, None, 8, 9, 10, 'next']
     self.assertSequenceEqual(expected, pages)
Beispiel #10
0
 def test_arrows_last_page(self):
     # Ensure the correct pages are returned if the last page is requested
     # adding first / last arrows.
     pages = utils.get_page_numbers(5, 5, arrows=True)
     expected = ['first', 'previous', 1, 2, 3, 4, 5]
     self.assertSequenceEqual(expected, pages)
Beispiel #11
0
 def test_arrows_first_page(self):
     # Ensure the correct pages are returned if the first page is requested
     # adding first / last arrows.
     pages = utils.get_page_numbers(1, 5, arrows=True)
     expected = [1, 2, 3, 4, 5, 'next', 'last']
     self.assertSequenceEqual(expected, pages)
 def test_arrows(self):
     # Ensure the pages are returned correctly adding first / last arrows.
     pages = utils.get_page_numbers(5, 10, arrows=True)
     expected = [
         'first', 'previous', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'next', 'last']
     self.assertSequenceEqual(expected, pages)
Beispiel #13
0
 def test_one_page(self):
     # Ensure the correct pages are returned if there is only one page.
     pages = utils.get_page_numbers(1, 1)
     expected = [1]
     self.assertSequenceEqual(expected, pages)
def describe_page_numbers(current_page,
                          total_count,
                          per_page,
                          page_numbers_at_ends=3,
                          pages_numbers_around_current=3):
    """
    Produces a description of how to display a paginated list's page numbers. Rather than just
    spitting out a list of every page available, the page numbers returned will be trimmed
    to display only the immediate numbers around the start, end, and the current page.

    ```
    >>> pprint(describe_page_numbers(1, 500, 10))
    {'current_page': 1,
     'has_next': True,
     'has_previous': False,
     'next_page': 2,
     'numbers': [1, 2, 3, 4, None, 48, 49, 50],
     'per_page': 10,
     'previous_page': 0,
     'total_count': 500}

    >>> pprint(describe_page_numbers(10, 500, 10))
    {'current_page': 10,
     'has_next': True,
     'has_previous': True,
     'next_page': 11,
     'numbers': [1, 2, 3, None, 7, 8, 9, 10, 11, 12, 13, None, 48, 49, 50],
     'per_page': 10,
     'previous_page': 9,
     'total_count': 500}

    >>> pprint(describe_page_numbers(50, 500, 10))
    {'current_page': 50,
     'has_next': False,
     'has_previous': True,
     'next_page': 51,
     'numbers': [1, 2, 3, None, 47, 48, 49, 50],
     'per_page': 10,
     'previous_page': 49,
     'total_count': 500}

    >>> pprint(describe_page_numbers(2, 40, 10))
    {'current_page': 2,
     'has_next': True,
     'has_previous': True,
     'next_page': 3,
     'numbers': [1, 2, 3, 4],
     'per_page': 10,
     'previous_page': 1,
     'total_count': 40}

    # total_count / per_page == 0 - need float math
    >>> pprint(describe_page_numbers(1, 1, 20))
    {'current_page': 1,
     'has_next': False,
     'has_previous': False,
     'next_page': 2,
     'numbers': [1],
     'per_page': 20,
     'previous_page': 0,
     'total_count': 1}

    # empty results - total_count==0
    >>> pprint(describe_page_numbers(1, 0, 20))
    {'current_page': 1,
     'has_next': False,
     'has_previous': False,
     'next_page': 2,
     'numbers': [],
     'per_page': 20,
     'previous_page': 0,
     'total_count': 0}

    ```

    :param current_page: the current page number (page numbers should start at 1)
    :param total_count: the total number of items that are being paginated
    :param per_page: the number of items that are displayed per page
    :param page_numbers_at_ends: the amount of page numbers to display at the beginning and end of the list
    :param pages_numbers_around_current: the amount of page numbers to display around the currently selected page

    :return: a dictionary describing the page numbers, relative to the current page
    """
    if total_count:
        page_numbers = get_page_numbers(
            current_page=current_page,
            num_pages=int(math.ceil(1.0 * total_count / per_page)),
            extremes=page_numbers_at_ends,
            arounds=pages_numbers_around_current,
        )
    else:
        page_numbers = []

    return {
        'numbers':
        [num for num in page_numbers if not isinstance(num, six.string_types)],
        'has_previous':
        'previous' in page_numbers,
        'has_next':
        'next' in page_numbers,
        'current_page':
        current_page,
        'previous_page':
        current_page - 1,
        'next_page':
        current_page + 1,
        'total_count':
        total_count,
        'per_page':
        per_page,
    }