Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate table class definition and instantiation in MultiTableMixin #876 #885

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions django_tables2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,32 @@ class MultiTableMixin(TableMixinBase):
# override context table name to make sense in a multiple table context
context_table_name = "tables"

def get_tables(self):
def get_tables_classes(self):
"""
Return an array of table instances containing data.
Return the list of classes to use for the tables.
"""
if self.tables is None:
view_name = type(self).__name__
raise ImproperlyConfigured(f"No tables were specified. Define {view_name}.tables")
klass = type(self).__name__
raise ImproperlyConfigured(
f"You must either specify {klass}.tables or override {klass}.get_tables_classes()"
)

return self.tables

def get_tables(self, **kwargs):
"""
Return an array of table instances containing data.
"""
tables = self.get_tables_classes()
data = self.get_tables_data()

if data is None:
return self.tables
return tables

if len(data) != len(self.tables):
view_name = type(self).__name__
raise ImproperlyConfigured(f"len({view_name}.tables_data) != len({view_name}.tables)")
return list(Table(data[i]) for i, Table in enumerate(self.tables))
if len(data) != len(tables):
klass = type(self).__name__
raise ImproperlyConfigured(f"len({klass}.tables_data) != len({klass}.tables)")
return list(Table(data[i], **kwargs) for i, Table in enumerate(tables))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would one specify the kwargs passed through here?


def get_tables_data(self):
"""
Expand Down
31 changes: 29 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,42 @@ def test_without_tables(self):
class View(tables.MultiTableMixin, TemplateView):
template_name = "multiple.html"

message = "No tables were specified. Define View.tables"
message = "You must either specify View.tables or override View.get_tables_classes()"
with self.assertRaisesMessage(ImproperlyConfigured, message):
View.as_view()(build_request("/"))

def test_get_tables_classes_list(self):
class View(tables.MultiTableMixin, TemplateView):
tables_data = (Person.objects.all(), Region.objects.all())
template_name = "multiple.html"

def get_tables_classes(self):
return [TableA, TableB]

response = View.as_view()(build_request("/"))
response.render()

html = response.rendered_content
self.assertEqual(html.count("<table >"), 2)

def test_with_empty_get_tables_list(self):
class View(tables.MultiTableMixin, TemplateView):
template_name = "multiple.html"

def get_tables(self):
def get_tables(self, **kwargs):
return []

response = View.as_view()(build_request("/"))
response.render()

html = response.rendered_content
self.assertIn("<h1>Multiple tables using MultiTableMixin</h1>", html)

def test_with_empty_get_tables_clases_list(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_with_empty_get_tables_clases_list(self):
def test_with_empty_get_tables_classes_list(self):

class View(tables.MultiTableMixin, TemplateView):
template_name = "multiple.html"

def get_tables_classes(self):
return []

response = View.as_view()(build_request("/"))
Expand Down