def test_intexing(self): obt = OrderByTuple(("a", "b", "c")) assert obt[0] == OrderBy("a") assert obt["b"] == OrderBy("b") with self.assertRaises(KeyError): obt["d"] with self.assertRaises(TypeError): obt[("tuple", )]
def test_intexing(self): obt = OrderByTuple(('a', 'b', 'c')) assert obt[0] == OrderBy('a') assert obt['b'] == OrderBy('b') with self.assertRaises(KeyError): obt['d'] with self.assertRaises(TypeError): obt[('tuple', )]
def orderbytuple(): obt = OrderByTuple('abc') Assert(obt) == (OrderBy('a'), OrderBy('b'), OrderBy('c')) Assert(obt[0]) == OrderBy('a') Assert(obt['b']) == OrderBy('b') with Assert.raises(IndexError) as error: obt['d'] with Assert.raises(TypeError) as error: obt[('tuple', )]
def order_by_alias(self): """ Returns an `OrderBy` describing the current state of ordering for this column. The following attempts to explain the difference between `order_by` and `.order_by_alias`. `.order_by_alias` returns and `.OrderBy` instance that's based on the *name* of the column, rather than the keys used to order the table data. Understanding the difference is essential. Having an alias *and* a keys version is necessary because an N-tuple (of data source keys) can be used by the column to order the data, and it is ambiguous when mapping from N-tuple to column (since multiple columns could use the same N-tuple). The solution is to use order by *aliases* (which are really just prefixed column names) that describe the ordering *state* of the column, rather than the specific keys in the data source should be ordered. e.g.:: >>> class SimpleTable(tables.Table): ... name = tables.Column(order_by=('firstname', 'last_name')) ... >>> table = SimpleTable([], order_by=('-name', )) >>> table.columns['name'].order_by_alias '-name' >>> table.columns['name'].order_by ('-first_name', '-last_name') The `OrderBy` returned has been patched to include an extra attribute ``next``, which returns a version of the alias that would be transitioned to if the user toggles sorting on this column, for example:: not sorted -> ascending ascending -> descending descending -> ascending This is useful otherwise in templates you'd need something like:: {% if column.is_ordered %} {% querystring table.prefixed_order_by_field=column.order_by_alias.opposite %} {% else %} {% querystring table.prefixed_order_by_field=column.order_by_alias %} {% endif %} """ order_by = OrderBy((self._table.order_by or {}).get(self.name, self.name)) order_by.next = order_by.opposite if self.is_ordered else order_by if self.column.initial_sort_descending and not self.is_ordered: order_by.next = order_by.opposite return order_by
def order_by_alias(self): """ Returns an `OrderBy` describing the current state of ordering for this column. The following attempts to explain the difference between `order_by` and `.order_by_alias`. `.order_by_alias` returns and `.OrderBy` instance that's based on the *name* of the column, rather than the keys used to order the table data. Understanding the difference is essential. Having an alias *and* a keys version is necessary because an N-tuple (of data source keys) can be used by the column to order the data, and it is ambiguous when mapping from N-tuple to column (since multiple columns could use the same N-tuple). The solution is to use order by *aliases* (which are really just prefixed column names) that describe the ordering *state* of the column, rather than the specific keys in the data source should be ordered. e.g.:: >>> class SimpleTable(tables.Table): ... name = tables.Column(order_by=("firstname", "last_name")) ... >>> table = SimpleTable([], order_by=('-name', )) >>> table.columns["name"].order_by_alias "-name" >>> table.columns["name"].order_by ("-first_name", "-last_name") The `OrderBy` returned has been patched to include an extra attribute ``next``, which returns a version of the alias that would be transitioned to if the user toggles sorting on this column, for example:: not sorted -> ascending ascending -> descending descending -> ascending This is useful otherwise in templates you'd need something like:: {% if column.is_ordered %} {% querystring table.prefixed_order_by_field=column.order_by_alias.opposite %} {% else %} {% querystring table.prefixed_order_by_field=column.order_by_alias %} {% endif %} """ order_by = OrderBy((self._table.order_by or {}).get(self.name, self.name)) order_by.next = order_by.opposite if self.is_ordered else order_by if self.column.initial_sort_descending and not self.is_ordered: order_by.next = order_by.opposite return order_by
def orderby(): a = OrderBy('a') Assert('a') == a Assert('a') == a.bare Assert('-a') == a.opposite Assert(True) == a.is_ascending Assert(False) == a.is_descending b = OrderBy('-b') Assert('-b') == b Assert('b') == b.bare Assert('b') == b.opposite Assert(True) == b.is_descending Assert(False) == b.is_ascending
def test_orderby(): a = OrderBy('a') assert 'a' == a assert 'a' == a.bare assert '-a' == a.opposite assert True is a.is_ascending assert False is a.is_descending b = OrderBy('-b') assert '-b' == b assert 'b' == b.bare assert 'b' == b.opposite assert True is b.is_descending assert False is b.is_ascending
def test_orderby_descending(self): b = OrderBy('-b') assert '-b' == b assert 'b' == b.bare assert 'b' == b.opposite assert True is b.is_descending assert False is b.is_ascending
def test_orderby_ascending(self): a = OrderBy('a') assert 'a' == a assert 'a' == a.bare assert '-a' == a.opposite assert True is a.is_ascending assert False is a.is_descending
def test_orderby_descending(self): b = OrderBy("-b") self.assertEqual(b, "-b") self.assertEqual(b.bare, "b") self.assertEqual(b.opposite, "b") self.assertTrue(b.is_descending) self.assertFalse(b.is_ascending)
def test_orderby_ascending(self): a = OrderBy("a") self.assertEqual(a, "a") self.assertEqual(a.bare, "a") self.assertEqual(a.opposite, "-a") self.assertTrue(a.is_ascending) self.assertFalse(a.is_descending)
def test_orderby_descending(self): b = OrderBy("-b") assert "-b" == b assert "b" == b.bare assert "b" == b.opposite assert True is b.is_descending assert False is b.is_ascending
def test_orderby_ascending(self): a = OrderBy("a") assert "a" == a assert "a" == a.bare assert "-a" == a.opposite assert True is a.is_ascending assert False is a.is_descending
def test_orderbytuple(): obt = OrderByTuple(('a', 'b', 'c')) assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c')) # indexing assert obt[0] == OrderBy('a') assert obt['b'] == OrderBy('b') with pytest.raises(KeyError): obt['d'] with pytest.raises(TypeError): obt[('tuple', )] # .get sentinel = object() assert obt.get('b', sentinel) is obt['b'] # keying assert obt.get('-', sentinel) is sentinel assert obt.get(0, sentinel) is obt['a'] # indexing assert obt.get(3, sentinel) is sentinel # .opposite assert OrderByTuple(('a', '-b', 'c')).opposite == ('-a', 'b', '-c') # in assert 'a' in obt and '-a' in obt
def test_basic(self): obt = OrderByTuple(('a', 'b', 'c')) assert obt == (OrderBy('a'), OrderBy('b'), OrderBy('c'))
def test_for_queryset(self): ab = OrderBy("a.b") self.assertEqual(ab.for_queryset(), "a__b") ab = OrderBy("a__b") self.assertEqual(ab.for_queryset(), "a__b")
def test_basic(self): obt = OrderByTuple(("a", "b", "c")) assert obt == (OrderBy("a"), OrderBy("b"), OrderBy("c"))
def test_error_on_legacy_separator(self): message = "Use '__' to separate path components, not '.' in accessor 'a.b'" with self.assertWarnsRegex(DeprecationWarning, message): OrderBy("a.b")