order_by was not normalized when passed to __init__, and generally failed with None...
authorMichael Elsdoerfer <michael@elsdoerfer.info>
Thu, 26 Jun 2008 22:32:12 +0000 (00:32 +0200)
committerMichael Elsdoerfer <michael@elsdoerfer.info>
Thu, 26 Jun 2008 22:32:12 +0000 (00:32 +0200)
django_tables/tables.py
tests/test_basic.py

index 3a1f532ffcd7c341eaaeac724660f10fa39d130d..9b814fe3585e685d468a7b87ad5bfc1754d74623 100644 (file)
@@ -122,7 +122,8 @@ class BaseTable(object):
         self._snapshot = None      # will store output dataset (ordered...)\r
         self._rows = Rows(self)\r
         self._columns = Columns(self)\r
-        self._order_by = order_by\r
+\r
+        self.order_by = order_by\r
 \r
         # Make a copy so that modifying this will not touch the class\r
         # definition. Note that this is different from forms, where the\r
@@ -233,14 +234,17 @@ class BaseTable(object):
         order_by = (isinstance(value, basestring) \\r
             and [value.split(',')] \\r
             or [value])[0]\r
-        # validate, remove all invalid order instructions\r
-        validated_order_by = []\r
-        for o in order_by:\r
-            if self._validate_column_name((o[:1]=='-' and [o[1:]] or [o])[0], "order_by"):\r
-                validated_order_by.append(o)\r
-            elif not options.IGNORE_INVALID_OPTIONS:\r
-                raise ValueError('Column name %s is invalid.' % o)\r
-        self._order_by = OrderByTuple(validated_order_by)\r
+        if order_by:\r
+            # validate, remove all invalid order instructions\r
+            validated_order_by = []\r
+            for o in order_by:\r
+                if self._validate_column_name((o[:1]=='-' and [o[1:]] or [o])[0], "order_by"):\r
+                    validated_order_by.append(o)\r
+                elif not options.IGNORE_INVALID_OPTIONS:\r
+                    raise ValueError('Column name %s is invalid.' % o)\r
+            self._order_by = OrderByTuple(validated_order_by)\r
+        else:\r
+            self._order_by = ()\r
     order_by = property(lambda s: s._order_by, _set_order_by)\r
 \r
     def __unicode__(self):\r
index d090df09206b58d27619f1cbe1fe9d0821e49d24..a41daf2a5600c77d3c9f05761da75d64eb179615 100644 (file)
@@ -141,6 +141,14 @@ def test_sort():
         books.order_by = order\r
         assert [b['id'] for b in books.rows] == result\r
 \r
+    # None is normalized to an empty tuple, ensuring iterability; it's\r
+    # also the default value\r
+    assert books.order_by == ()\r
+    iter(books.order_by)\r
+    books.order_by = None\r
+    assert books.order_by == ()\r
+    iter(books.order_by)\r
+\r
     # test various orderings\r
     test_order(('num_pages',), [1,3,2,4])\r
     test_order(('-num_pages',), [4,2,3,1])\r
@@ -165,6 +173,10 @@ def test_sort():
     assert not books.order_by\r
     test_order(('language', 'num_pages'), [1,3,2,4])  # as if: 'num_pages'\r
 \r
+    # [bug] order_by did not run through setter when passed to init\r
+    books = BookTable([], order_by='name')\r
+    assert books.order_by == ('name',)\r
+\r
 def test_callable():\r
     """Data fields, ``default`` and ``data`` options can be callables.\r
     """\r