Added a __len__ method to the columns container.
authorMichael Elsdoerfer <michael@elsdoerfer.com>
Tue, 6 Apr 2010 13:18:00 +0000 (15:18 +0200)
committerMichael Elsdoerfer <michael@elsdoerfer.com>
Tue, 6 Apr 2010 13:18:00 +0000 (15:18 +0200)
django_tables/base.py
tests/test_basic.py

index bdb97bb29ac6af6647139213b2f7cba325fd081b..d0052269c4e76e1e6d5d13c642760dc001c4c3da 100644 (file)
@@ -249,6 +249,10 @@ class Columns(object):
         else:\r
             return item in self.all()\r
 \r
+    def __len__(self):\r
+        self._spawn_columns()\r
+        return len([1 for c in self._columns.values() if c.visible])\r
+\r
     def __getitem__(self, name):\r
         """Return a column by name."""\r
         self._spawn_columns()\r
index d171bd0a315015b3483550a6e70dfd4322174f3c..fe942789175b62145c7c0ebdca2c4e7cbfbabf19 100644 (file)
@@ -53,6 +53,15 @@ def test_declaration():
     assert 'motto' in StateTable2.base_columns
 
 
+def test_column_count():
+    class MyTable(TestTable):
+        visbible = tables.Column(visible=True)
+        hidden = tables.Column(visible=False)
+
+    # The columns container supports the len() builtin
+    assert len(MyTable([]).columns) == 1
+
+
 def test_pagination():
     class BookTable(TestTable):
         name = tables.Column()