Stop using sa-filters for basic grid sorting
this just breaks if we need to use "aliased" models e.g. when sorting and/or filtering by Product "regular price" column and similar. so now sorting more like we always used to, except for multi-column. nb. this still assumes callers use `Grid.make_sorter()` when declaring the sorters. if caller must specify more custom/explicit sort logic then it likely will not work and we'll have to add a workaround to allow avoiding the common logic..but that's another day
This commit is contained in:
parent
421266e70c
commit
6d79766b24
2 changed files with 37 additions and 36 deletions
tailbone/grids
|
@ -30,7 +30,6 @@ import logging
|
|||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import orm
|
||||
from sa_filters import apply_sort
|
||||
|
||||
from rattail.db.types import GPCType
|
||||
from rattail.util import prettify, pretty_boolean, pretty_quantity
|
||||
|
@ -1235,29 +1234,29 @@ class Grid(object):
|
|||
# TODO: is there a better way to check for SA sorting?
|
||||
if self.model_class:
|
||||
|
||||
# convert sort settings into a 'sortspec' for use with sa-filters
|
||||
full_spec = []
|
||||
# collect actual column sorters for order_by clause
|
||||
sorters = []
|
||||
for sorter in self.active_sorters:
|
||||
sortkey = sorter['field']
|
||||
sortdir = sorter['order']
|
||||
sortfunc = self.sorters.get(sortkey)
|
||||
if sortfunc:
|
||||
spec = {
|
||||
'sortkey': sortkey,
|
||||
'model': sortfunc._class.__name__,
|
||||
'field': sortfunc._column.key,
|
||||
'direction': sortdir or 'asc',
|
||||
}
|
||||
full_spec.append(spec)
|
||||
if not sortfunc:
|
||||
log.warning("unknown sorter: %s", sorter)
|
||||
continue
|
||||
|
||||
# apply joins needed for this sort spec
|
||||
for spec in full_spec:
|
||||
sortkey = spec['sortkey']
|
||||
# join appropriate model if needed
|
||||
if sortkey in self.joiners and sortkey not in self.joined:
|
||||
data = self.joiners[sortkey](data)
|
||||
self.joined.add(sortkey)
|
||||
|
||||
return apply_sort(data, full_spec)
|
||||
# add column/dir to collection
|
||||
sortdir = sorter['order']
|
||||
sorters.append(getattr(sortfunc._column, sortdir)())
|
||||
|
||||
# apply sorting to query
|
||||
if sorters:
|
||||
data = data.order_by(*sorters)
|
||||
|
||||
return data
|
||||
|
||||
else:
|
||||
# not a SQLAlchemy grid, custom sorter
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue