Skip to content

Commit

Permalink
Hide column on multi_select tables if no actions available.
Browse files Browse the repository at this point in the history
This patch will add a runtime check to hide/show checkbox columns
on multi_select tables.
Right know the behaviour is calculated once but does not take in account
the dynamic allowed call of each Table Action available.

The change will add 'hidden' class to the first column if table actions
are present but all of them returned False on allowed() call.

Change-Id: Ia4512abe3dbb5de4581673717eb518b2d3a50291
Closes-Bug: #1271194
  • Loading branch information
Leandro I. Costantino committed Mar 11, 2014
1 parent e5e7b5b commit 0bf864e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions horizon/tables/base.py
Expand Up @@ -1226,6 +1226,19 @@ def get_row_actions(self, datum):
bound_actions.append(bound_action)
return bound_actions

def set_multiselect_column_visibility(self, visible=True):
"""hide checkbox column if no current table action is allowed."""
if not self.multi_select:
return
select_column = self.columns.values()[0]
#Try to find if the hidden class need to be
#removed or added based on visible flag.
hidden_found = 'hidden' in select_column.classes
if hidden_found and visible:
select_column.classes.remove('hidden')
elif not hidden_found and not visible:
select_column.classes.append('hidden')

def render_table_actions(self):
"""Renders the actions specified in ``Meta.table_actions``."""
template_path = self._meta.table_actions_template
Expand All @@ -1236,6 +1249,7 @@ def render_table_actions(self):
self._filter_action(self._meta._filter_action, self.request):
extra_context["filter"] = self._meta._filter_action
context = template.RequestContext(self.request, extra_context)
self.set_multiselect_column_visibility(len(bound_actions) > 0)
return table_actions_template.render(context)

def render_row_actions(self, datum):
Expand Down Expand Up @@ -1559,4 +1573,5 @@ def get_rows(self):
LOG.exception("Error while rendering table rows.")
exc_info = sys.exc_info()
raise template.TemplateSyntaxError, exc_info[1], exc_info[2]

return rows
23 changes: 23 additions & 0 deletions horizon/test/tests/tables.py
Expand Up @@ -135,6 +135,11 @@ def action(self, request, object_ids):
self.current_past_action = 1


class MyDisabledAction(MyToggleAction):
def allowed(self, request, obj=None):
return False


class MyFilterAction(tables.FilterAction):
def filter(self, table, objs, filter_string):
q = filter_string.lower()
Expand Down Expand Up @@ -222,6 +227,17 @@ class Meta:
row_actions = ()


class DisabledActionsTable(tables.DataTable):
id = tables.Column('id')

class Meta:
name = "disabled_actions_table"
verbose_name = "Disabled Actions Table"
table_actions = (MyDisabledAction,)
row_actions = ()
multi_select = True


class DataTableTests(test.TestCase):
def test_table_instantiation(self):
"""Tests everything that happens when the table is instantiated."""
Expand Down Expand Up @@ -996,6 +1012,13 @@ def test_table_action_attributes(self):
res = http.HttpResponse(table.render())
self.assertNotContains(res, "<form")

def test_table_actions_not_allowed_hide_multiselect(self):
table = DisabledActionsTable(self.request, TEST_DATA)
self.assertFalse(table.has_actions)
self.assertFalse(table.needs_form_wrapper)
res = http.HttpResponse(table.render())
self.assertContains(res, "multi_select_column hidden")

def test_table_action_object_display_is_none(self):
action_string = "my_table__toggle__1"
req = self.factory.post('/my_url/', {'action': action_string})
Expand Down

0 comments on commit 0bf864e

Please sign in to comment.