Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8924603

Browse files
committed
Flatten logic in TableData constructor
1 parent df517da commit 8924603

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

django_tables2/tables.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ def __init__(self, data, table):
3434
if (hasattr(data, 'count') and callable(data.count) and
3535
hasattr(data, 'order_by') and callable(data.order_by)):
3636
self.queryset = data
37-
# otherwise it must be convertable to a list
38-
else:
39-
# do some light validation
40-
if hasattr(data, '__iter__') or (hasattr(data, '__len__') and hasattr(data, '__getitem__')):
41-
self.list = list(data)
42-
else:
43-
raise ValueError(
44-
'data must be QuerySet-like (have count and '
45-
'order_by) or support list(data) -- %s has '
46-
'neither' % type(data).__name__
47-
)
37+
return
38+
39+
# do some light validation
40+
if hasattr(data, '__iter__') or (hasattr(data, '__len__') and hasattr(data, '__getitem__')):
41+
self.list = list(data)
42+
return
43+
44+
raise ValueError(
45+
'data must be QuerySet-like (have count() and order_by()) or support'
46+
' list(data) -- {} has neither'.format(type(data).__name__)
47+
)
4848

4949
def __len__(self):
50-
if not hasattr(self, "_length"):
50+
if not hasattr(self, '_length'):
5151
# Use the queryset count() method to get the length, instead of
5252
# loading all results into memory. This allows, for example,
5353
# smart paginators that use len() to perform better.
@@ -72,7 +72,7 @@ def ordering(self):
7272
This works by inspecting the actual underlying data. As such it's only
7373
supported for querysets.
7474
"""
75-
if hasattr(self, "queryset"):
75+
if hasattr(self, 'queryset'):
7676
aliases = {}
7777
for bound_column in self.table.columns:
7878
aliases[bound_column.order_by_alias] = bound_column.order_by
@@ -101,7 +101,7 @@ def order_by(self, aliases):
101101
accessors += bound_column.order_by.opposite
102102
else:
103103
accessors += bound_column.order_by
104-
if hasattr(self, "queryset"):
104+
if hasattr(self, 'queryset'):
105105
translate = lambda accessor: accessor.replace(Accessor.SEPARATOR, QUERYSET_ACCESSOR_SEPARATOR)
106106
if accessors:
107107
self.queryset = self.queryset.order_by(*(translate(a) for a in accessors))
@@ -132,9 +132,9 @@ def verbose_name(self):
132132
honored. List data is checked for a ``verbose_name`` attribute, and
133133
falls back to using ``"item"``.
134134
"""
135-
if hasattr(self, "queryset"):
135+
if hasattr(self, 'queryset'):
136136
return self.queryset.model._meta.verbose_name
137-
return getattr(self.list, "verbose_name", "item")
137+
return getattr(self.list, 'verbose_name', 'item')
138138

139139
@cached_property
140140
def verbose_name_plural(self):
@@ -143,9 +143,9 @@ def verbose_name_plural(self):
143143
144144
This uses the same approach as `TableData.verbose_name`.
145145
"""
146-
if hasattr(self, "queryset"):
146+
if hasattr(self, 'queryset'):
147147
return self.queryset.model._meta.verbose_name_plural
148-
return getattr(self.list, "verbose_name_plural", "items")
148+
return getattr(self.list, 'verbose_name_plural', 'items')
149149

150150

151151
class DeclarativeColumnsMetaclass(type):

0 commit comments

Comments
 (0)