Added sort by aliased and joined entity

This commit is contained in:
Konsta Vesterinen
2013-04-08 13:24:18 +03:00
parent cc4c27a755
commit f786ca040c
5 changed files with 38 additions and 11 deletions

View File

@@ -4,6 +4,12 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release. Here you can see the full list of changes between each SQLAlchemy-Utils release.
0.8.4 (2013-04-08)
^^^^^^^^^^^^^^^^^^
- Added sort by aliased and joined entity
0.8.3 (2013-04-03) 0.8.3 (2013-04-03)
^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^

View File

@@ -24,7 +24,7 @@ class PyTest(Command):
setup( setup(
name='SQLAlchemy-Utils', name='SQLAlchemy-Utils',
version='0.8.3', version='0.8.4',
url='https://github.com/kvesteri/sqlalchemy-utils', url='https://github.com/kvesteri/sqlalchemy-utils',
license='BSD', license='BSD',
author='Konsta Vesterinen', author='Konsta Vesterinen',

View File

@@ -2,6 +2,7 @@ from sqlalchemy.orm import defer
from sqlalchemy.orm.mapper import Mapper from sqlalchemy.orm.mapper import Mapper
from sqlalchemy.orm.query import _ColumnEntity from sqlalchemy.orm.query import _ColumnEntity
from sqlalchemy.orm.properties import ColumnProperty from sqlalchemy.orm.properties import ColumnProperty
from sqlalchemy.orm.util import AliasedInsp
from sqlalchemy.sql.expression import desc, asc from sqlalchemy.sql.expression import desc, asc
@@ -107,6 +108,16 @@ def sort_query(query, sort):
return query.order_by(func(sort)) return query.order_by(func(sort))
for entity in entities: for entity in entities:
if isinstance(entity, AliasedInsp):
if component and entity.name != component:
continue
selectable = entity.selectable
if sort in selectable.c:
attr = selectable.c[sort]
query = query.order_by(func(attr))
else:
table = entity.__table__ table = entity.__table__
if component and table.name != component: if component and table.name != component:
continue continue

View File

@@ -130,7 +130,7 @@ class NumberRangeType(types.TypeDecorator):
impl = NumberRangeRawType impl = NumberRangeRawType
def process_bind_param(self, value, dialect): def process_bind_param(self, value, dialect):
if value: if value is not None:
return value.normalized return value.normalized
return value return value

View File

@@ -64,6 +64,16 @@ class TestSortQuery(TestCase):
query = sort_query(query, '-articles') query = sort_query(query, '-articles')
assert 'ORDER BY articles DESC' in str(query) assert 'ORDER BY articles DESC' in str(query)
def test_sort_by_aliased_joined_entity(self):
alias = sa.orm.aliased(self.Category, name='categories')
query = self.session.query(
self.Article
).join(
alias, self.Article.category
)
query = sort_query(query, '-categories-name')
assert 'ORDER BY categories.name DESC' in str(query)
def test_sort_by_joined_table_column(self): def test_sort_by_joined_table_column(self):
query = self.session.query(self.Article).join(self.Article.category) query = self.session.query(self.Article).join(self.Article.category)
sorted_query = sort_query(query, 'category-name') sorted_query = sort_query(query, 'category-name')