Merge pull request #3370 from charhate/ui_changes

Return empty result instead of 500 if page number exceeds total
This commit is contained in:
charhate 2021-01-20 12:12:37 -08:00 committed by GitHub
commit f0fbc81370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 5 deletions

View File

@ -9,6 +9,7 @@
.. moduleauthor:: Kevin Glisson <kglisson@netflix.com> .. moduleauthor:: Kevin Glisson <kglisson@netflix.com>
""" """
import math
from inflection import underscore from inflection import underscore
from sqlalchemy import exc, func, distinct from sqlalchemy import exc, func, distinct
from sqlalchemy.orm import make_transient, lazyload from sqlalchemy.orm import make_transient, lazyload
@ -219,15 +220,20 @@ def sort(query, model, field, direction):
def paginate(query, page, count): def paginate(query, page, count):
""" """
Returns the items given the count and page specified Returns the items given the count and page specified. The items would be an empty list
if page number exceeds max page number based on count per page and total number of records.
:param query: :param query: search query
:param page: :param page: current page number
:param count: :param count: results per page
""" """
total = get_count(query) total = get_count(query)
# Check if input page is higher than total number of pages based on count per page and total
# In such a case Flask-SQLAlchemy pagination call results in 404
if math.ceil(total / count) < page:
return dict(items=[], total=total)
items = query.paginate(page, count).items items = query.paginate(page, count).items
return dict(items=items, total=total, current=len(items)) return dict(items=items, total=total)
def update_list(model, model_attr, item_model, items): def update_list(model, model_attr, item_model, items):