Merge pull request #165 from ionelmc/master
Fix "silent" error handling in the TableData.__init__
This commit is contained in:
commit
006cc878ce
|
@ -6,6 +6,7 @@ from .rows import BoundRows
|
||||||
from .utils import (Accessor, AttributeDict, build_request, cached_property,
|
from .utils import (Accessor, AttributeDict, build_request, cached_property,
|
||||||
computed_values, OrderBy, OrderByTuple, segment, Sequence)
|
computed_values, OrderBy, OrderByTuple, segment, Sequence)
|
||||||
import copy
|
import copy
|
||||||
|
import sys
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
from django.db.models.fields import FieldDoesNotExist
|
from django.db.models.fields import FieldDoesNotExist
|
||||||
from django.utils.datastructures import SortedDict
|
from django.utils.datastructures import SortedDict
|
||||||
|
@ -34,12 +35,15 @@ class TableData(object):
|
||||||
self.queryset = data
|
self.queryset = data
|
||||||
# otherwise it must be convertable to a list
|
# otherwise it must be convertable to a list
|
||||||
else:
|
else:
|
||||||
try:
|
# do some light validation
|
||||||
|
if hasattr(data, '__iter__') or (hasattr(data, '__len__') and hasattr(data, '__getitem__')):
|
||||||
self.list = list(data)
|
self.list = list(data)
|
||||||
except:
|
else:
|
||||||
raise ValueError('data must be QuerySet-like (have count and '
|
raise ValueError(
|
||||||
'order_by) or support list(data) -- %s has '
|
'data must be QuerySet-like (have count and '
|
||||||
'neither' % type(data).__name__)
|
'order_by) or support list(data) -- %s has '
|
||||||
|
'neither' % type(data).__name__
|
||||||
|
)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
if not hasattr(self, "_length"):
|
if not hasattr(self, "_length"):
|
||||||
|
|
|
@ -178,7 +178,30 @@ def should_support_haystack_data_source():
|
||||||
|
|
||||||
table = PersonTable(SearchQuerySet().all())
|
table = PersonTable(SearchQuerySet().all())
|
||||||
table.as_html()
|
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
|
@core.test
|
||||||
def ordering():
|
def ordering():
|
||||||
|
|
Loading…
Reference in New Issue