From: Michael Elsdoerfer Date: Tue, 6 Apr 2010 13:18:00 +0000 (+0200) Subject: Added a __len__ method to the columns container. X-Git-Tag: 0.2~14 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5ee008025addae18d87f542b4fc25b32dc52ec28;p=django-tables2.git Added a __len__ method to the columns container. --- diff --git a/django_tables/base.py b/django_tables/base.py index bdb97bb..d005226 100644 --- a/django_tables/base.py +++ b/django_tables/base.py @@ -249,6 +249,10 @@ class Columns(object): else: return item in self.all() + def __len__(self): + self._spawn_columns() + return len([1 for c in self._columns.values() if c.visible]) + def __getitem__(self, name): """Return a column by name.""" self._spawn_columns() diff --git a/tests/test_basic.py b/tests/test_basic.py index d171bd0..fe94278 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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()