Fix grid bug if "current page" becomes invalid

sometimes it was possible to fall outside the valid page range, in which
case grid would stop showing results!
This commit is contained in:
Lance Edgar 2017-07-18 13:26:17 -05:00
parent 69778a4682
commit cb6b093a2a

View file

@ -698,11 +698,11 @@ class Grid(object):
page = self.request.GET.get('page') page = self.request.GET.get('page')
if page is not None: if page is not None:
if page.isdigit(): if page.isdigit():
settings['page'] = page settings['page'] = int(page)
else: else:
page = self.request.session.get('grid.{}.page'.format(self.key)) page = self.request.session.get('grid.{}.page'.format(self.key))
if page is not None: if page is not None:
settings['page'] = page settings['page'] = int(page)
def persist_settings(self, settings, to='session'): def persist_settings(self, settings, to='session'):
""" """
@ -775,12 +775,26 @@ class Grid(object):
Paginate the given data set according to current settings, and return Paginate the given data set according to current settings, and return
the result. the result.
""" """
if self.model_class: if not self.model_class:
return SqlalchemyOrmPage(data, return data
items_per_page=self.pagesize,
page=self.page, # we of course assume our current page is correct, at first
url_maker=URLMaker(self.request)) pager = self.make_pager(data)
return data
# if pager has detected that our current page is outside the valid
# range, we must re-orient ourself around the "new" (valid) page
if pager.page != self.page:
self.page = pager.page
self.request.session['grid.{}.page'.format(self.key)] = self.page
pager = self.make_pager(data)
return pager
def make_pager(self, data):
return SqlalchemyOrmPage(data,
items_per_page=self.pagesize,
page=self.page,
url_maker=URLMaker(self.request))
def make_visible_data(self): def make_visible_data(self):
""" """