diff --git a/product/ZSQLCatalog/SQLCatalog.py b/product/ZSQLCatalog/SQLCatalog.py index aabc00daf3d39046d62774915c20c05764fa6464..5b0200381475c5d019615fcdd00c6b51f9d49cd6 100644 --- a/product/ZSQLCatalog/SQLCatalog.py +++ b/product/ZSQLCatalog/SQLCatalog.py @@ -2346,7 +2346,13 @@ class Catalog(Folder, elif order_by_expression is not None: if not isinstance(order_by_expression, basestring): raise TypeError, 'order_by_expression must be a basestring instance. Got %r.' % (order_by_expression, ) - order_by_list = [[x.strip()] for x in order_by_expression.split(',')] + for x in order_by_expression.split(','): + x = x.strip() + item = x.rsplit(None, 1) + if len(item) > 1 and item[-1].upper() in ('ASC', 'DESC'): + append(item) + else: + append([x]) return order_by_list def buildEntireQuery(self, kw, query_table='catalog', ignore_empty_string=1, diff --git a/product/ZSQLCatalog/tests/testSQLCatalog.py b/product/ZSQLCatalog/tests/testSQLCatalog.py index ad886f5f2606a259500cfe701d51ea1fe1551ff8..8f637cbb3c6e13784a1a45280bda8faf992f971f 100644 --- a/product/ZSQLCatalog/tests/testSQLCatalog.py +++ b/product/ZSQLCatalog/tests/testSQLCatalog.py @@ -791,6 +791,36 @@ class TestSQLCatalog(ERP5TypeTestCase): self._searchTextInDictQuery('date') self._searchTextInDictQuery('related_date') + def test_buildOrderByList(self): + order_by_list = self._catalog.buildOrderByList( + sort_on='default', + ) + self.assertEqual(order_by_list, [['default']]) + order_by_list = self._catalog.buildOrderByList( + sort_on='default', + sort_order='DESC', + ) + self.assertEqual(order_by_list, [['default', 'DESC']]) + order_by_list = self._catalog.buildOrderByList( + sort_on=[['default', 'DESC', 'INT']] + ) + self.assertEqual(order_by_list, [['default', 'DESC', 'INT']]) + order_by_list = self._catalog.buildOrderByList( + order_by_expression='default' + ) + order_by_list = self._catalog.buildOrderByList( + order_by_expression='default DESC' + ) + self.assertEqual(order_by_list, [['default', 'DESC']]) + order_by_list = self._catalog.buildOrderByList( + order_by_expression='CAST(default AS INT) DESC' + ) + self.assertEqual(order_by_list, [['CAST(default AS INT)', 'DESC']]) + order_by_list = self._catalog.buildOrderByList( + order_by_expression='CAST(default AS INT)' + ) + self.assertEqual(order_by_list, [['CAST(default AS INT)']]) + ##return catalog(title=Query(title='a', operator='not')) #return catalog(title={'query': 'a', 'operator': 'not'}) #return catalog(title={'query': ['a', 'b'], 'operator': 'not'})