Skip to content

Commit

Permalink
Merge pull request #165 from ionelmc/master
Browse files Browse the repository at this point in the history
Fix "silent" error handling in the TableData.__init__
  • Loading branch information
bradleyayers committed Mar 7, 2014
2 parents bc9da04 + a4eecf6 commit 006cc87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
14 changes: 9 additions & 5 deletions django_tables2/tables.py
Expand Up @@ -6,6 +6,7 @@
from .utils import (Accessor, AttributeDict, build_request, cached_property,
computed_values, OrderBy, OrderByTuple, segment, Sequence)
import copy
import sys
from django.core.paginator import Paginator
from django.db.models.fields import FieldDoesNotExist
from django.utils.datastructures import SortedDict
Expand Down Expand Up @@ -34,12 +35,15 @@ def __init__(self, data, table):
self.queryset = data
# otherwise it must be convertable to a list
else:
try:
# do some light validation
if hasattr(data, '__iter__') or (hasattr(data, '__len__') and hasattr(data, '__getitem__')):
self.list = list(data)
except:
raise ValueError('data must be QuerySet-like (have count and '
'order_by) or support list(data) -- %s has '
'neither' % type(data).__name__)
else:
raise ValueError(
'data must be QuerySet-like (have count and '
'order_by) or support list(data) -- %s has '
'neither' % type(data).__name__
)

def __len__(self):
if not hasattr(self, "_length"):
Expand Down
25 changes: 24 additions & 1 deletion tests/core.py
Expand Up @@ -178,7 +178,30 @@ class PersonTable(tables.Table):

table = PersonTable(SearchQuerySet().all())
table.as_html()



@core.test
def data_validation():
with raises(ValueError):
table = OrderedTable(None)

class Bad:
def __len__(self):
pass

with raises(ValueError):
table = OrderedTable(Bad())

class Ok:
def __len__(self):
return 1
def __getitem__(self, pos):
if pos != 0:
raise IndexError()
return {'a': 1}

table = OrderedTable(Ok())
assert len(table.rows) == 1

@core.test
def ordering():
Expand Down

0 comments on commit 006cc87

Please sign in to comment.