Paginator class
Provide mechanisms to implement a custom paginating SQLAlchemy Query.
For usage example, see the following test modules:
./tests/test_11_paginator_postgresql.py
./tests/test_12_paginator_mysql.py
- class bh_database.paginator.Paginator(query: Query, page: int, per_page: int)
Bases:
object
Provides mechanisms to implement a custom paginating SQLAlchemy Query.
It receives three (3) main inputs: a fully loaded SQLAlchemy Query, a page number and a number of records per page, it calculates and prepares paginated data. And returns its own instance. Callers can then access instance properties to prepare final data.
- Parameters:
query – sqlalchemy.orm.Query, which is opened, and data are ready to be fetched.
page (int) – the page number to retrieve data for.
per_page (int) – how many records to retrieve for each page.
- execute()
Carry out the paginating operation.
Calculating values for properties, retrieve target data rows.
- property page: int
Read only property. The requested page number.
If the originally requested page number is greater than the calculated
total_pages
, then the value of this property is set tototal_pages
.
- property per_page: int
Read only property. The requested number of records per page.
- property total_records: int
Read only property. The total number of records retrieved.
- property total_pages: int
Read only property. The total number of pages.
Its value is calculated page on
total_records
andper_page
properties.
- property offset: int
Read only property. It is 0-based.
From which row number to retrieve data for the requested
page
.
- property limit: int
Read only property. The requested number of records per page.
Its value is basically
per_page
, but iftotal_pages
is 0, then it is set to 0 also.
- property items: list
Read only property.
This is essentially the paginated data for the requested
page
. It is a list of rows fromoffset
tolimit
, inclusively.Rows are copied or rather referenced from Query directly, there is no additional process performed on the rows when copied to this list.
- property has_next: bool
Read only property.
True
if there is a next page.False
otherwise.
- property has_prev: bool
Read only property.
True
if there is a previous page.False
otherwise.