From 6302d5a3519a84ae80f82cc022553bae56ac2447 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 6 Jul 2017 20:57:53 -0500 Subject: [PATCH] Refactor grids to use new 'paginate' library instead of the older `webhelpers.paginate` --- setup.py | 2 ++ tailbone/newgrids/alchemy.py | 41 ++++++++++++++++++++------- tailbone/templates/newgrids/grid.mako | 2 +- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index 6bd82ce2..fc3fb42e 100644 --- a/setup.py +++ b/setup.py @@ -84,6 +84,8 @@ requires = [ 'humanize', # 0.5.1 'Mako', # 0.6.2 'openpyxl', # 2.4.7 + 'paginate', # 0.5.6 + 'paginate_sqlalchemy', # 0.2.0 'pyramid_beaker>=0.6', # 0.6.1 'pyramid_debugtoolbar', # 1.0 'pyramid_exclog', # 0.6 diff --git a/tailbone/newgrids/alchemy.py b/tailbone/newgrids/alchemy.py index 99d0aec6..fa1a72e4 100644 --- a/tailbone/newgrids/alchemy.py +++ b/tailbone/newgrids/alchemy.py @@ -1,8 +1,8 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8; -*- ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2016 Lance Edgar +# Copyright © 2010-2017 Lance Edgar # # This file is part of Rattail. # @@ -26,6 +26,7 @@ FormAlchemy Grid Classes from __future__ import unicode_literals, absolute_import +import urllib import logging import sqlalchemy as sa @@ -34,8 +35,9 @@ from sqlalchemy import orm from rattail.db.types import GPCType from rattail.util import prettify -import formalchemy -from webhelpers import paginate +import formalchemy as fa +import paginate +from paginate_sqlalchemy import SqlalchemyOrmPage from tailbone.db import Session from tailbone.newgrids import Grid, GridColumn, filters @@ -44,6 +46,23 @@ from tailbone.newgrids import Grid, GridColumn, filters log = logging.getLogger(__name__) +class URLMaker(object): + """ + URL constructor for use with SQLAlchemy grid pagers. Logic for this was + basically copied from the old `webhelpers.paginate` module + """ + + def __init__(self, request): + self.request = request + + def __call__(self, page): + params = self.request.GET.copy() + params["page"] = page + params["partial"] = "1" + qs = urllib.urlencode(params, True) + return '{}?{}'.format(self.request.path, qs) + + class AlchemyGrid(Grid): """ Grid class for use with SQLAlchemy data models. @@ -64,9 +83,9 @@ class AlchemyGrid(Grid): def __init__(self, *args, **kwargs): super(AlchemyGrid, self).__init__(*args, **kwargs) - fa_grid = formalchemy.Grid(self.model_class, instances=self.data, - session=kwargs.get('session', Session()), - request=self.request) + fa_grid = fa.Grid(self.model_class, instances=self.data, + session=kwargs.get('session', Session()), + request=self.request) fa_grid.prettify = prettify self._fa_grid = fa_grid @@ -174,10 +193,10 @@ class AlchemyGrid(Grid): Paginate the given data set according to current settings, and return the result. """ - return paginate.Page( - query, item_count=query.count(), - items_per_page=self.pagesize, page=self.page, - url=paginate.PageURL_WebOb(self.request)) + return SqlalchemyOrmPage(query, + items_per_page=self.pagesize, + page=self.page, + url_maker=URLMaker(self.request)) def iter_visible_columns(self): """ diff --git a/tailbone/templates/newgrids/grid.mako b/tailbone/templates/newgrids/grid.mako index 61b02a47..6bc37820 100644 --- a/tailbone/templates/newgrids/grid.mako +++ b/tailbone/templates/newgrids/grid.mako @@ -43,7 +43,7 @@ % endif