From: Michael Elsdoerfer Date: Thu, 26 Jun 2008 22:32:12 +0000 (+0200) Subject: order_by was not normalized when passed to __init__, and generally failed with None... X-Git-Tag: 0.2~55 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=97fd71e8a0eb2d724e083ab84a55c08ebac29a8f;p=django-tables2.git order_by was not normalized when passed to __init__, and generally failed with None values --- diff --git a/django_tables/tables.py b/django_tables/tables.py index 3a1f532..9b814fe 100644 --- a/django_tables/tables.py +++ b/django_tables/tables.py @@ -122,7 +122,8 @@ class BaseTable(object): self._snapshot = None # will store output dataset (ordered...) self._rows = Rows(self) self._columns = Columns(self) - self._order_by = order_by + + self.order_by = order_by # Make a copy so that modifying this will not touch the class # definition. Note that this is different from forms, where the @@ -233,14 +234,17 @@ class BaseTable(object): order_by = (isinstance(value, basestring) \ and [value.split(',')] \ or [value])[0] - # validate, remove all invalid order instructions - validated_order_by = [] - for o in order_by: - if self._validate_column_name((o[:1]=='-' and [o[1:]] or [o])[0], "order_by"): - validated_order_by.append(o) - elif not options.IGNORE_INVALID_OPTIONS: - raise ValueError('Column name %s is invalid.' % o) - self._order_by = OrderByTuple(validated_order_by) + if order_by: + # validate, remove all invalid order instructions + validated_order_by = [] + for o in order_by: + if self._validate_column_name((o[:1]=='-' and [o[1:]] or [o])[0], "order_by"): + validated_order_by.append(o) + elif not options.IGNORE_INVALID_OPTIONS: + raise ValueError('Column name %s is invalid.' % o) + self._order_by = OrderByTuple(validated_order_by) + else: + self._order_by = () order_by = property(lambda s: s._order_by, _set_order_by) def __unicode__(self): diff --git a/tests/test_basic.py b/tests/test_basic.py index d090df0..a41daf2 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -141,6 +141,14 @@ def test_sort(): books.order_by = order assert [b['id'] for b in books.rows] == result + # None is normalized to an empty tuple, ensuring iterability; it's + # also the default value + assert books.order_by == () + iter(books.order_by) + books.order_by = None + assert books.order_by == () + iter(books.order_by) + # test various orderings test_order(('num_pages',), [1,3,2,4]) test_order(('-num_pages',), [4,2,3,1]) @@ -165,6 +173,10 @@ def test_sort(): assert not books.order_by test_order(('language', 'num_pages'), [1,3,2,4]) # as if: 'num_pages' + # [bug] order_by did not run through setter when passed to init + books = BookTable([], order_by='name') + assert books.order_by == ('name',) + def test_callable(): """Data fields, ``default`` and ``data`` options can be callables. """