From cb6b093a2a7fbe57b7f9f41a88bbbc8ca848488d Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 18 Jul 2017 13:26:17 -0500 Subject: [PATCH] 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! --- tailbone/grids/core.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tailbone/grids/core.py b/tailbone/grids/core.py index 3d8aaa24..d5c3ef62 100644 --- a/tailbone/grids/core.py +++ b/tailbone/grids/core.py @@ -698,11 +698,11 @@ class Grid(object): page = self.request.GET.get('page') if page is not None: if page.isdigit(): - settings['page'] = page + settings['page'] = int(page) else: page = self.request.session.get('grid.{}.page'.format(self.key)) if page is not None: - settings['page'] = page + settings['page'] = int(page) 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 the result. """ - if self.model_class: - return SqlalchemyOrmPage(data, - items_per_page=self.pagesize, - page=self.page, - url_maker=URLMaker(self.request)) - return data + if not self.model_class: + return data + + # we of course assume our current page is correct, at first + pager = self.make_pager(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): """