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): """