Utils Pagination

inyoka.utils.pagination

This file helps creating a pagination. It is able to select the right database entries and return a subset of the query. The object has methods and properties to retrieve the links to other pages, which can be used in templates.

Usage:

>>> from django.test.client import RequestFactory
>>> from inyoka.forum.models import Topic
>>> request = RequestFactory().get('/')
>>> pagination = Pagination(request,
...     Topic.objects.all(), page=5,
...     per_page=25, total=500)
>>> # the database entries on this page
>>> objects = pagination.get_queryset()

If the page is out of range, it throws a Http404 exception, when the object is created. You can pass the optional argument total if you already know how many entries match your query. If you don’t, Pagination will use a database query to find it out. The number of pages can be limited with the optional argument max_pages. For tables that are quite big it’s sometimes useful to use an indexed column determinating the position instead of using an offset / limit statement. In this case you can use the rownum_column argument. To get all items on one page, set one_page=True or per_page=0.

URL to the first and last page will be accessible through pagination.first and pagination.last.

Similarly pagination.prev and pagination.next will return the URL for the previous or next page if there is such a page, otherwise return value will be False.

pagination.list(threshold) is a generator that yields all URLs to be displayed in order as well as necessary “spacers” if pages are omitted. The parameter threshold determines how many pages are to be displayed before/after current page and at beginning and end. Possible yields can take three forms:

  • { ‘type’: ‘link’, ‘url’: ‘http://localhost/’, ‘page’: 1 }

  • { ‘type’: ‘current’, ‘url’: ‘http://localhost/2/’, ‘page’: 2 }

  • { ‘type’: ‘spacer’ }

Caveat: paginations with link functions generated in a closure are not pickleable.

copyright:
  1. 2007-2024 by the Inyoka Team, see AUTHORS for more details.

license:

BSD, see LICENSE for more details.

class inyoka.utils.pagination.Pagination(request, query, page=1, per_page=10, link=None, total=None, rownum_column=None, max_pages=None, one_page=False)

Handle pagination

_get_total(total)
property first

Return the url to the first page

Get link for page number

Parameters:

page – page number

Returns:

A URL string

get_queryset()

Get objects for current page

property last

Return the url to the last page

list(threshold=2)

Generator, lists all links for a pagination.

Parameters:

threshold – Number of pages displayed at beginning, end and before/after current page. Other pages are omitted.

Yields:

A small dict containing at least a key type which can be ‘link’, ‘current’ or ‘spacer’. All except the ‘spacer’ will also have keys ‘url’ and ‘page’.

property next

Return the url to the last page, or False if already on the last page

property prev

Return the url to the previous page, or False if already on first page