Smarter table form rendering and controls.

Fixes bug 1022466.

Change-Id: I7958db7751b01f558efcb2b0451df2c0728d4b1e
This commit is contained in:
Gabriel Hurley 2012-07-12 14:52:03 -07:00
parent 459ed0c156
commit 4fb923d27e
4 changed files with 58 additions and 4 deletions

View File

@ -27,6 +27,6 @@
{% endblock %}
{% block modal-footer %}
<button class="btn btn-primary pull-right" type="submit">{% trans "Download RC File" %}"</button>
<button class="btn btn-primary pull-right" type="submit">{% trans "Download RC File" %}</button>
{% if hide %}<a href="{% url horizon:settings:project:index %}" class="btn secondary cancel close">{% trans "Cancel" %}</a>{% endif %}
{% endblock %}

View File

@ -789,10 +789,11 @@ class DataTable(object):
"""
__metaclass__ = DataTableMetaclass
def __init__(self, request, data=None, **kwargs):
def __init__(self, request, data=None, needs_form_wrapper=None, **kwargs):
self._meta.request = request
self._meta.data = data
self.kwargs = kwargs
self._needs_form_wrapper = needs_form_wrapper
# Create a new set
columns = []
@ -909,6 +910,28 @@ class DataTable(object):
% lookup)
return matches[0]
@property
def has_actions(self):
"""
Boolean. Indicates whether there are any available actions on this
table.
"""
if not self.base_actions:
return False
return any(self.get_table_actions()) or any(self._meta.row_actions)
@property
def needs_form_wrapper(self):
"""
Boolean. Indicates whather this table should be rendered wrapped in
a ``<form>`` tag or not.
"""
# If needs_form_wrapper is explicitly set, defer to that.
if self._needs_form_wrapper is not None:
return self._needs_form_wrapper
# Otherwise calculate whether or not we need a form element.
return self.has_actions
def get_table_actions(self):
""" Returns a list of the action instances for this table. """
bound_actions = [self.base_actions[action.name] for

View File

@ -1,6 +1,7 @@
{% load i18n %}
{% with table.needs_form_wrapper as needs_form_wrapper %}
<div class="table_wrapper">
<form action="{{ table.get_absolute_url }}" method="POST">{% csrf_token %}
{% if needs_form_wrapper %}<form action="{{ table.get_absolute_url }}" method="POST">{% csrf_token %}{% endif %}
{% with columns=table.get_columns rows=table.get_rows %}
<table id="{{ table.name }}" class="table table-bordered table-striped">
<thead>
@ -49,5 +50,6 @@
</tfoot>
</table>
{% endwith %}
</form>
{% if needs_form_wrapper %}</form>{% endif %}
</div>
{% endwith %}

View File

@ -175,6 +175,16 @@ class MyTable(tables.DataTable):
row_actions = (MyAction, MyLinkAction, MyBatchAction, MyToggleAction)
class NoActionsTable(tables.DataTable):
id = tables.Column('id')
class Meta:
name = "no_actions_table"
verbose_name = _("No Actions Table")
table_actions = ()
row_actions = ()
class DataTableTests(test.TestCase):
def test_table_instantiation(self):
""" Tests everything that happens when the table is instantiated. """
@ -625,3 +635,22 @@ class DataTableTests(test.TestCase):
self.assertNotContains(res, '<tr class="summation"')
self.assertNotContains(res, '<td>3.0</td>')
self.assertNotContains(res, '<td>6</td>')
def test_table_action_attributes(self):
table = MyTable(self.request, TEST_DATA)
self.assertTrue(table.has_actions)
self.assertTrue(table.needs_form_wrapper)
res = http.HttpResponse(table.render())
self.assertContains(res, "<form")
table = MyTable(self.request, TEST_DATA, needs_form_wrapper=False)
self.assertTrue(table.has_actions)
self.assertFalse(table.needs_form_wrapper)
res = http.HttpResponse(table.render())
self.assertNotContains(res, "<form")
table = NoActionsTable(self.request, TEST_DATA)
self.assertFalse(table.has_actions)
self.assertFalse(table.needs_form_wrapper)
res = http.HttpResponse(table.render())
self.assertNotContains(res, "<form")