From 5ee008025addae18d87f542b4fc25b32dc52ec28 Mon Sep 17 00:00:00 2001 From: Michael Elsdoerfer Date: Tue, 6 Apr 2010 15:18:00 +0200 Subject: [PATCH] Added a __len__ method to the columns container. --- django_tables/base.py | 4 ++++ tests/test_basic.py | 9 +++++++++ 2 files changed, 13 insertions(+) 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() -- 2.26.2