Add grid workarounds when data is list instead of query

ugh, this is not very intuitive.  pretty sure all that needs an
overhaul someday
This commit is contained in:
Lance Edgar 2022-05-10 20:06:21 -05:00
parent 18c3c57930
commit 75319c0d6a

View file

@ -596,7 +596,16 @@ class Grid(object):
""" """
class_ = getattr(model_property, 'class_', self.model_class) class_ = getattr(model_property, 'class_', self.model_class)
column = getattr(class_, model_property.key) column = getattr(class_, model_property.key)
return lambda q, d: q.order_by(getattr(column, d)())
def sorter(query, direction):
# TODO: this seems hacky..normally we expect a true query
# of course, but in some cases it may be a list instead.
# if so then we can't actually sort
if isinstance(query, list):
return query
return query.order_by(getattr(column, direction)())
return sorter
def make_simple_sorter(self, key, foldcase=False): def make_simple_sorter(self, key, foldcase=False):
""" """
@ -984,6 +993,16 @@ class Grid(object):
return pager return pager
def make_pager(self, data): def make_pager(self, data):
# TODO: this seems hacky..normally we expect `data` to be a
# query of course, but in some cases it may be a list instead.
# if so then we can't use ORM pager
if isinstance(data, list):
import paginate
return paginate.Page(data,
items_per_page=self.pagesize,
page=self.page)
return SqlalchemyOrmPage(data, return SqlalchemyOrmPage(data,
items_per_page=self.pagesize, items_per_page=self.pagesize,
page=self.page, page=self.page,