Fix references to LogEntry model used and add support for a maximum page of results on the logs API

This commit is contained in:
Joseph Schorr 2019-01-02 15:57:55 -05:00
parent 204eb74c4f
commit b6db002729
9 changed files with 48 additions and 31 deletions

View file

@ -1,6 +1,7 @@
from peewee import SQL
def paginate(query, model, descending=False, page_token=None, limit=50, id_alias=None):
def paginate(query, model, descending=False, page_token=None, limit=50, id_alias=None,
max_page=None):
""" Paginates the given query using an ID range, starting at the optional page_token.
Returns a *list* of matching results along with an unencrypted page_token for the
next page, if any. If descending is set to True, orders by the ID descending rather
@ -27,7 +28,12 @@ def paginate(query, model, descending=False, page_token=None, limit=50, id_alias
query = query.where(model.id >= start_id)
query = query.limit(limit + 1)
return paginate_query(query, limit=limit, id_alias=id_alias)
page_number = (page_token.get('page_number') or None) if page_token else None
if page_number is not None and max_page is not None and page_number > max_page:
return [], None
return paginate_query(query, limit=limit, id_alias=id_alias, page_number=page_number)
def pagination_start(page_token=None):
@ -38,7 +44,7 @@ def pagination_start(page_token=None):
return None
def paginate_query(query, limit=50, id_alias=None):
def paginate_query(query, limit=50, id_alias=None, page_number=None):
""" Executes the given query and returns a page's worth of results, as well as the page token
for the next page (if any).
"""
@ -47,7 +53,8 @@ def paginate_query(query, limit=50, id_alias=None):
if len(results) > limit:
start_id = getattr(results[limit], id_alias or 'id')
page_token = {
'start_id': start_id
'start_id': start_id,
'page_number': page_number + 1 if page_number else 1,
}
return results[0:limit], page_token