Skip to content

Commit

Permalink
Add preference towards single id for action handling.
Browse files Browse the repository at this point in the history
When the action handler received both a single id and multiple
object ids (such as when a user checks multiple boxes, then selects
a single row's action), it would previously favor the mutiple
ids, which goes against the intention of the user. This patch
causes it to favor the single id now.

Fixes bug 960866.

Change-Id: I53034589ada5792c22c2fc5144af2aee3b551eb0
  • Loading branch information
gabrielhurley authored and jeblair committed Apr 3, 2012
1 parent 300591a commit 8fadde1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion horizon/tables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,9 @@ def take_action(self, action_name, obj_id=None, obj_ids=None):
response = action.single(self, self._meta.request, obj_id)
# Otherwise figure out what to pass along
else:
if obj_id and not obj_ids:
# Preference given to a specific id, since that implies
# the user selected an action for just one row.
if obj_id:
obj_ids = [obj_id]
response = action.multiple(self, self._meta.request, obj_ids)
return response
Expand Down
19 changes: 16 additions & 3 deletions horizon/tests/table_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def allowed(self, request, obj=None):
return getattr(obj, 'status', None) != 'down'

def handle(self, data_table, request, object_ids):
return shortcuts.redirect('http://example.com/%s' % len(object_ids))
return shortcuts.redirect('http://example.com/?ids=%s'
% ",".join(object_ids))


class MyColumn(tables.Column):
Expand Down Expand Up @@ -416,7 +417,7 @@ def test_table_actions(self):
('my_table', 'delete', '1'))
handled = self.table.maybe_handle()
self.assertEqual(handled.status_code, 302)
self.assertEqual(handled["location"], "http://example.com/1")
self.assertEqual(handled["location"], "http://example.com/?ids=1")

# Batch action (without toggle) conjugation behavior
req = self.factory.get('/my_url/')
Expand Down Expand Up @@ -474,7 +475,7 @@ def test_table_actions(self):
('my_table', 'delete', None))
handled = self.table.maybe_handle()
self.assertEqual(handled.status_code, 302)
self.assertEqual(handled["location"], "http://example.com/2")
self.assertEqual(handled["location"], "http://example.com/?ids=1,2")

# Action with nothing selected
req = self.factory.post('/my_url/', {'action': action_string})
Expand All @@ -486,6 +487,18 @@ def test_table_actions(self):
self.assertEqual(list(req._messages)[0].message,
"Please select a row before taking that action.")

# Action with specific id and multiple ids favors single id
action_string = "my_table__delete__3"
req = self.factory.post('/my_url/', {'action': action_string,
'object_ids': [1, 2]})
self.table = MyTable(req, TEST_DATA)
self.assertEqual(self.table.parse_action(action_string),
('my_table', 'delete', '3'))
handled = self.table.maybe_handle()
self.assertEqual(handled.status_code, 302)
self.assertEqual(handled["location"],
"http://example.com/?ids=3")

# At least one object in table
# BatchAction is available
req = self.factory.get('/my_url/')
Expand Down

0 comments on commit 8fadde1

Please sign in to comment.