core.pagination
¶
Module Contents¶
Classes¶
This module provides efficient pagination for a small specified set of queries. |
- class core.pagination.Pagination(page_size=10, max_pages=100)¶
This module provides efficient pagination for a small specified set of queries. The datastore does not provide an efficient method for skipping N number of entities. This prevents the usual navigation over multiple pages (in most cases - like a google search - the user expects a list of pages (e.g. 1-10) on the bottom of each page with direct access to these pages). With the datastore and it’s cursors, the most we can provide is a next-page & previous-page link using cursors. This module provides an efficient method to provide these direct-access page links under the condition that only a few, known-in-advance queries will be run. This is typically the case for forums, where there is only one query per thread (it’s posts ordered by creation date) and one for the threadlist (it’s threads, ordered by changedate).
To use this module, create an instance of this index-manager on class-level (setting page_size & max_pages). Then call :meth:get_pages with the query you want to retrieve the cursors for the individual pages for. This will return one start-cursor per available page that can then be used to create urls that point to the specific page. When the entities returned by the query change (eg a new post is added), call :meth:refresh_index for each affected query.
Note
The refreshAll Method is missing - intentionally. Whenever data changes you have to call refresh_index for each affected Index. As long as you can name them, their number is limited and this module can be efficiently used.
- Parameters:
page_size (int) – How many entities shall fit on one page
max_pages (int) – How many pages are build. Items become unreachable if the amount of items exceeds page_size*max_pages (i.e. if a forum-thread has more than page_size*max_pages Posts, Posts after that barrier won’t show up).
- _db_type = 'viur_pagination'¶
- key_from_query(query)¶
Derives a unique Database-Key from a given query. This Key is stable regardless in which order the filter have been applied
- Parameters:
query (viur.core.db.Query) – Query to derive key from
- Returns:
The unique key derived
- Return type:
str
- get_or_build_index(orig_query)¶
Builds a specific index based on origQuery AND local variables (self.page_size and self.max_pages) Returns a list of starting-cursors for each page. You probably shouldn’t call this directly. Use cursor_for_query.
- Parameters:
orig_query (viur.core.db.Query) – Query to build the index for
- Return type:
list[str]
- cursor_for_query(query, page)¶
Returns the starting-cursor for the given query and page using an index.
- Parameters:
query (viur.core.db.Query) – Query to get the cursor for
page (int) – Page the user wants to retrieve
- Returns:
Cursor or None if no cursor is applicable
- Return type:
Optional[str]
- get_pages(query)¶
Returns a list of all starting-cursors for this query. The first element is always None as the first page doesn’t have any start-cursor
- Parameters:
query (viur.core.db.Query) –
- Return type:
list[str]
- refresh_index(query)¶
Refreshes the Index for the given query (Actually it removes it from the db, so it gets rebuild on next use)
- Parameters:
query (viur.core.db.Query) – Query for which the index should be refreshed
- Return type:
None