Fix async messages w/ translated strings.

Change-Id: I83d2a30f94fc382d382c504ac1a7851df0c97e17
This commit is contained in:
Gabriel Hurley 2012-06-14 16:19:22 -07:00
parent c17b06d08a
commit f6f2a91e14
2 changed files with 8 additions and 3 deletions

View File

@ -21,6 +21,7 @@ messaging needs (e.g. AJAX communication, etc.).
from django.contrib import messages as _messages from django.contrib import messages as _messages
from django.contrib.messages import constants from django.contrib.messages import constants
from django.utils.encoding import force_unicode
def add_message(request, level, message, extra_tags='', fail_silently=False): 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(): if request.is_ajax():
tag = constants.DEFAULT_TAGS[level] tag = constants.DEFAULT_TAGS[level]
request.horizon['async_messages'].append([tag, message]) request.horizon['async_messages'].append([tag,
force_unicode(message)])
else: else:
return _messages.add_message(request, level, message, return _messages.add_message(request, level, message,
extra_tags, fail_silently) extra_tags, fail_silently)

View File

@ -15,6 +15,8 @@
# under the License. # under the License.
from django import http 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 messages
from horizon import middleware from horizon import middleware
@ -25,11 +27,12 @@ from horizon.openstack.common import jsonutils
class MessageTests(test.TestCase): class MessageTests(test.TestCase):
def test_middleware_header(self): def test_middleware_header(self):
req = self.request 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.assertTrue("async_messages" in req.horizon)
self.assertItemsEqual(req.horizon['async_messages'], []) self.assertItemsEqual(req.horizon['async_messages'], [])
req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
messages.error(req, expected[1]) messages.error(req, string)
self.assertItemsEqual(req.horizon['async_messages'], [expected]) self.assertItemsEqual(req.horizon['async_messages'], [expected])
res = http.HttpResponse() res = http.HttpResponse()
res = middleware.HorizonMiddleware().process_response(req, res) res = middleware.HorizonMiddleware().process_response(req, res)