From f6f2a91e14f6bdd4e1a87e31a1d6923127afee1b Mon Sep 17 00:00:00 2001 From: Gabriel Hurley Date: Thu, 14 Jun 2012 16:19:22 -0700 Subject: [PATCH] Fix async messages w/ translated strings. Change-Id: I83d2a30f94fc382d382c504ac1a7851df0c97e17 --- horizon/messages.py | 4 +++- horizon/tests/message_tests.py | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/horizon/messages.py b/horizon/messages.py index 906d3d052..feac64dbe 100644 --- a/horizon/messages.py +++ b/horizon/messages.py @@ -21,6 +21,7 @@ messaging needs (e.g. AJAX communication, etc.). from django.contrib import messages as _messages from django.contrib.messages import constants +from django.utils.encoding import force_unicode def add_message(request, level, message, extra_tags='', fail_silently=False): @@ -29,7 +30,8 @@ def add_message(request, level, message, extra_tags='', fail_silently=False): """ if request.is_ajax(): tag = constants.DEFAULT_TAGS[level] - request.horizon['async_messages'].append([tag, message]) + request.horizon['async_messages'].append([tag, + force_unicode(message)]) else: return _messages.add_message(request, level, message, extra_tags, fail_silently) diff --git a/horizon/tests/message_tests.py b/horizon/tests/message_tests.py index a314813eb..87c73af5c 100644 --- a/horizon/tests/message_tests.py +++ b/horizon/tests/message_tests.py @@ -15,6 +15,8 @@ # under the License. from django import http +from django.utils.encoding import force_unicode +from django.utils.translation import ugettext_lazy as _ from horizon import messages from horizon import middleware @@ -25,11 +27,12 @@ from horizon.openstack.common import jsonutils class MessageTests(test.TestCase): def test_middleware_header(self): req = self.request - expected = ["error", "Giant ants are attacking San Francisco!"] + string = _("Giant ants are attacking San Francisco!") + expected = ["error", force_unicode(string)] self.assertTrue("async_messages" in req.horizon) self.assertItemsEqual(req.horizon['async_messages'], []) req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' - messages.error(req, expected[1]) + messages.error(req, string) self.assertItemsEqual(req.horizon['async_messages'], [expected]) res = http.HttpResponse() res = middleware.HorizonMiddleware().process_response(req, res)