Added a check for None value in get_object_display

When get_object_display returns None,
the concatenation of the list of display values fails.

Fixes bug 1030641

PATCH SET 2: Added function for joining of
lazy(proxy) strings(similar to string_concat)

Change-Id: I76398e85f93bc508aaae056aca77c8103998c7b6
This commit is contained in:
Tihomir Trifonov 2012-08-06 19:28:16 +03:00
parent 2f0678db45
commit 68e612d37f
3 changed files with 33 additions and 5 deletions

View File

@ -25,7 +25,7 @@ from django.utils.translation import string_concat, ugettext_lazy as _
from horizon import exceptions
from horizon import messages
from horizon.utils import html
from horizon.utils import html, functions
LOG = logging.getLogger(__name__)
@ -497,7 +497,7 @@ class BatchAction(Action):
action_not_allowed = []
for datum_id in obj_ids:
datum = table.get_object_by_id(datum_id)
datum_display = table.get_object_display(datum)
datum_display = table.get_object_display(datum) or _("N/A")
if not table._filter_action(self, request, datum):
action_not_allowed.append(datum_display)
LOG.info('Permission denied to %s: "%s"' %
@ -522,19 +522,19 @@ class BatchAction(Action):
if action_not_allowed:
msg = _('You do not have permission to %(action)s: %(objs)s')
params = {"action": self._conjugate(action_not_allowed).lower(),
"objs": ", ".join(action_not_allowed)}
"objs": functions.lazy_join(", ", action_not_allowed)}
messages.error(request, msg % params)
success_message_level = messages.info
if action_failure:
msg = _('Unable to %(action)s: %(objs)s')
params = {"action": self._conjugate(action_failure).lower(),
"objs": ", ".join(action_failure)}
"objs": functions.lazy_join(", ", action_failure)}
messages.error(request, msg % params)
success_message_level = messages.info
if action_success:
msg = _('%(action)s: %(objs)s')
params = {"action": self._conjugate(action_success, True),
"objs": ", ".join(action_success)}
"objs": functions.lazy_join(", ", action_success)}
success_message_level(request, msg % params)
return shortcuts.redirect(self.get_success_url(request))

View File

@ -19,6 +19,8 @@ from django import shortcuts
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from mox import IsA
from horizon import tables
from horizon import test
@ -654,3 +656,20 @@ class DataTableTests(test.TestCase):
self.assertFalse(table.needs_form_wrapper)
res = http.HttpResponse(table.render())
self.assertNotContains(res, "<form")
def test_table_action_object_display_is_none(self):
action_string = "my_table__toggle__1"
req = self.factory.post('/my_url/', {'action': action_string})
self.table = MyTable(req, TEST_DATA)
self.mox.StubOutWithMock(self.table, 'get_object_display')
self.table.get_object_display(IsA(FakeObject)).AndReturn(None)
self.mox.ReplayAll()
self.assertEqual(self.table.parse_action(action_string),
('my_table', 'toggle', '1'))
handled = self.table.maybe_handle()
self.assertEqual(handled.status_code, 302)
self.assertEqual(handled["location"], "/my_url/")
self.assertEqual(list(req._messages)[0].message,
u"Downed Item: N/A")

View File

@ -0,0 +1,9 @@
from django.utils.encoding import force_unicode
from django.utils.functional import lazy
def _lazy_join(separator, strings):
return separator.join([force_unicode(s)
for s in strings])
lazy_join = lazy(_lazy_join, unicode)