Fix pagination of repositories

Fixes #1725
This commit is contained in:
Joseph Schorr 2016-08-15 16:11:45 -04:00
parent 1c4d3326c2
commit 7f5b536ddb
5 changed files with 70 additions and 39 deletions

View file

@ -19,16 +19,30 @@ def paginate(query, model, descending=False, page_token=None, limit=50, id_alias
else:
query = query.order_by(id_field)
if page_token is not None:
start_id = page_token.get('start_id')
if start_id is not None:
if descending:
query = query.where(model.id <= start_id)
else:
query = query.where(model.id >= start_id)
start_id = pagination_start(page_token)
if start_id is not None:
if descending:
query = query.where(model.id <= start_id)
else:
query = query.where(model.id >= start_id)
else:
query = query.limit(limit + 1)
return paginate_query(query, limit=limit, id_alias=id_alias)
def pagination_start(page_token=None):
""" Returns the start ID for pagination for the given page token. Will return None if None. """
if page_token is not None:
return page_token.get('start_id')
return None
def paginate_query(query, limit=50, id_alias=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).
"""
results = list(query)
page_token = None
if len(results) > limit: