Refactor grids to use new 'paginate' library

instead of the older `webhelpers.paginate`
This commit is contained in:
Lance Edgar 2017-07-06 20:57:53 -05:00
parent 8014e60d14
commit 6302d5a351
3 changed files with 33 additions and 12 deletions

View file

@ -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

View file

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

View file

@ -43,7 +43,7 @@
<p class="page-links">
${h.select('pagesize', grid.pager.items_per_page, grid.get_pagesize_options())}
per page&nbsp;
${grid.pager.pager('$link_first $link_previous ~1~ $link_next $link_last', symbol_next='next', symbol_previous='prev', partial=1)}
${grid.pager.pager('$link_first $link_previous ~1~ $link_next $link_last', symbol_next='next', symbol_previous='prev')|n}
</p>
</div>
% endif