watson.db.utils

class watson.db.utils.Page(id)[source]

A single page object that is returned from the paginator.

Provides the ability to automatically generate a query string.

__init__(id)[source]
class watson.db.utils.Pagination(query, page=1, limit=20)[source]

Provides simple pagination for query results.

query

Query – The SQLAlchemy query to be paginated

page

int – The page to be displayed

limit

int – The maximum number of results to be displayed on a page

total

int – The total number of results

items

list – The items returned from the query

Example:

# within controller
query = session.query(Model)
paginator = Pagination(query, limit=50)

# within view
{% for item in paginator %}
{% endfor %}
<div class="pagination">
{% for page in paginator.iter_pages() %}
    {% if page == paginator.page %}
    <a href="{{ page }}" class="current">{{ page.id }}</a>
    {% else %}
    <a href="{{ page }}">{{ page.id }}</a>
    {% endif %}
{% endfor %}
</div>
__init__(query, page=1, limit=20)[source]

Initialize the paginator and set some default values.

has_next

Return whether or not there are more pages from the currently displayed page.

Returns:boolean
has_previous

Return whether or not there are previous pages from the currently displayed page.

Returns:boolean
iter_pages()[source]

An iterable containing the number of pages to be displayed.

Example:

{% for page in paginator.iter_pages() %}{% endfor %}
next

Return the next page object if another page exists.

Returns:Page
pages

The total amount of pages to be displayed based on the number of results and the limit being displayed.

Returns:int
previous

Return the previous page object if the page exists.

Returns:Page