From af49cf1f156e68dcd8d34a3de59c40ee5bd12b66 Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Wed, 16 Jul 2014 17:50:03 +0200 Subject: [PATCH] Replace force_unicode with force_text Django changed the name to force_unicode and it's only available in python2 as an alias of force_text. The new name is available since Django 1.4.2 Closes-Bug: #1345642 Change-Id: Ida8c545ceec7c31999f2497d540a0dc5a653d286 --- HACKING.rst | 2 +- horizon/exceptions.py | 6 +++--- horizon/forms/fields.py | 10 +++++----- horizon/messages.py | 4 ++-- horizon/templatetags/horizon.py | 6 +++--- horizon/test/helpers.py | 4 ++-- horizon/test/tests/base.py | 2 +- horizon/test/tests/exceptions.py | 4 ++-- horizon/test/tests/messages.py | 6 +++--- horizon/utils/functions.py | 4 ++-- horizon/workflows/base.py | 10 +++++----- .../dashboards/project/containers/forms.py | 4 ++-- .../project/data_processing/job_binaries/forms.py | 4 ++-- openstack_dashboard/dashboards/settings/user/forms.py | 2 +- tox.ini | 2 +- 15 files changed, 35 insertions(+), 35 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index b413c8a23f..fdebbd93f2 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -12,7 +12,7 @@ Horizon Style Commandments django.core.urlresolvers.reverse_lazy, django.template.loader.render_to_string, django.utils.datastructures.SortedDict, - django.utils.encoding.force_unicode, + django.utils.encoding.force_text, django.utils.html.conditional_escape, django.utils.html.escape, django.utils.http.urlencode, diff --git a/horizon/exceptions.py b/horizon/exceptions.py index 673ef79ffb..a1a7151f4f 100644 --- a/horizon/exceptions.py +++ b/horizon/exceptions.py @@ -261,7 +261,7 @@ def handle(request, message=None, redirect=None, ignore=False, exc_type, exc_value, exc_traceback = exc_value.wrapped wrap = True - log_entry = encoding.force_unicode(exc_value) + log_entry = encoding.force_text(exc_value) # We trust messages from our own exceptions if issubclass(exc_type, HorizonException): @@ -271,9 +271,9 @@ def handle(request, message=None, redirect=None, ignore=False, message = exc_value._safe_message # If the message has a placeholder for the exception, fill it in elif message and "%(exc)s" in message: - message = encoding.force_unicode(message) % {"exc": log_entry} + message = encoding.force_text(message) % {"exc": log_entry} if message: - message = encoding.force_unicode(message) + message = encoding.force_text(message) if issubclass(exc_type, UNAUTHORIZED): if ignore: diff --git a/horizon/forms/fields.py b/horizon/forms/fields.py index 584b9d330e..58c06bf413 100644 --- a/horizon/forms/fields.py +++ b/horizon/forms/fields.py @@ -20,7 +20,7 @@ from django.core.exceptions import ValidationError # noqa from django.core import urlresolvers from django.forms import fields from django.forms import widgets -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.functional import Promise # noqa from django.utils import html from django.utils.translation import ugettext_lazy as _ @@ -144,21 +144,21 @@ class SelectWidget(widgets.Select): super(SelectWidget, self).__init__(attrs, choices) def render_option(self, selected_choices, option_value, option_label): - option_value = force_unicode(option_value) + option_value = force_text(option_value) other_html = (option_value in selected_choices) and \ u' selected="selected"' or '' if not isinstance(option_label, (basestring, Promise)): for data_attr in self.data_attrs: data_value = html.conditional_escape( - force_unicode(getattr(option_label, - data_attr, ""))) + force_text(getattr(option_label, + data_attr, ""))) other_html += ' data-%s="%s"' % (data_attr, data_value) if self.transform: option_label = self.transform(option_label) return u'' % ( html.escape(option_value), other_html, - html.conditional_escape(force_unicode(option_label))) + html.conditional_escape(force_text(option_label))) class DynamicSelectWidget(widgets.Select): diff --git a/horizon/messages.py b/horizon/messages.py index 20eb32d193..c1b36adbd0 100644 --- a/horizon/messages.py +++ b/horizon/messages.py @@ -19,7 +19,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 +from django.utils.encoding import force_text from django.utils.safestring import SafeData # noqa @@ -32,7 +32,7 @@ def add_message(request, level, message, extra_tags='', fail_silently=False): if isinstance(message, SafeData): extra_tags = extra_tags + ' safe' request.horizon['async_messages'].append([tag, - force_unicode(message), + force_text(message), extra_tags]) else: return _messages.add_message(request, level, message, diff --git a/horizon/templatetags/horizon.py b/horizon/templatetags/horizon.py index 29aa509bc8..d420a95177 100644 --- a/horizon/templatetags/horizon.py +++ b/horizon/templatetags/horizon.py @@ -16,7 +16,7 @@ from __future__ import absolute_import from django import template from django.utils.datastructures import SortedDict -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.translation import ugettext_lazy as _ from horizon.base import Horizon # noqa @@ -118,9 +118,9 @@ def quota(val, units=None): if val == float("inf"): return _("No Limit") elif units is not None: - return "%s %s %s" % (val, units, force_unicode(_("Available"))) + return "%s %s %s" % (val, units, force_text(_("Available"))) else: - return "%s %s" % (val, force_unicode(_("Available"))) + return "%s %s" % (val, force_text(_("Available"))) @register.filter diff --git a/horizon/test/helpers.py b/horizon/test/helpers.py index 205dadb184..e377c52592 100644 --- a/horizon/test/helpers.py +++ b/horizon/test/helpers.py @@ -30,7 +30,7 @@ from django.core.handlers import wsgi from django import http from django import test as django_test from django.test.client import RequestFactory # noqa -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils import unittest LOG = logging.getLogger(__name__) @@ -156,7 +156,7 @@ class TestCase(django_test.TestCase): # Otherwise, make sure we got the expected messages. for msg_type, count in kwargs.items(): - msgs = [force_unicode(m.message) + msgs = [force_text(m.message) for m in messages if msg_type in m.tags] assert len(msgs) == count, \ "%s messages not as expected: %s" % (msg_type.title(), diff --git a/horizon/test/tests/base.py b/horizon/test/tests/base.py index 85547d5f1b..40dd64ea37 100644 --- a/horizon/test/tests/base.py +++ b/horizon/test/tests/base.py @@ -332,7 +332,7 @@ class GetUserHomeTests(BaseHorizonTests): base.Horizon.get_user_home(self.test_user)) def test_using_module_function(self): - module_func = 'django.utils.encoding.force_unicode' + module_func = 'django.utils.encoding.force_text' settings.HORIZON_CONFIG['user_home'] = module_func conf.HORIZON_CONFIG._setup() diff --git a/horizon/test/tests/exceptions.py b/horizon/test/tests/exceptions.py index b358996fc7..795dde50b2 100644 --- a/horizon/test/tests/exceptions.py +++ b/horizon/test/tests/exceptions.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from horizon import exceptions from horizon.test import helpers as test @@ -25,7 +25,7 @@ class HandleTests(test.TestCase): # Japanese translation of: # 'Because the container is not empty, it can not be deleted.' - expected = ['error', force_unicode(translated_unicode), ''] + expected = ['error', force_text(translated_unicode), ''] req = self.request req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' diff --git a/horizon/test/tests/messages.py b/horizon/test/tests/messages.py index 5add2e9933..716d090328 100644 --- a/horizon/test/tests/messages.py +++ b/horizon/test/tests/messages.py @@ -15,7 +15,7 @@ import json from django import http -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.safestring import mark_safe from horizon import messages @@ -27,7 +27,7 @@ class MessageTests(test.TestCase): def test_middleware_header(self): req = self.request string = "Giant ants are attacking San Francisco!" - expected = ["error", force_unicode(string), ""] + expected = ["error", force_text(string), ""] self.assertTrue("async_messages" in req.horizon) self.assertItemsEqual(req.horizon['async_messages'], []) req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' @@ -41,7 +41,7 @@ class MessageTests(test.TestCase): def test_safe_message(self): req = self.request string = mark_safe("We are now safe from ants! Go here!") - expected = ["error", force_unicode(string), " safe"] + expected = ["error", force_text(string), " safe"] self.assertTrue("async_messages" in req.horizon) self.assertItemsEqual(req.horizon['async_messages'], []) req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' diff --git a/horizon/utils/functions.py b/horizon/utils/functions.py index d8a3da1adc..3d38366142 100644 --- a/horizon/utils/functions.py +++ b/horizon/utils/functions.py @@ -16,13 +16,13 @@ import re from django.conf import settings from django.contrib.auth import logout # noqa from django import http -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.functional import lazy # noqa from django.utils import translation def _lazy_join(separator, strings): - return separator.join([force_unicode(s) + return separator.join([force_text(s) for s in strings]) lazy_join = lazy(_lazy_join, unicode) diff --git a/horizon/workflows/base.py b/horizon/workflows/base.py index dfce78a6ef..dcab851202 100644 --- a/horizon/workflows/base.py +++ b/horizon/workflows/base.py @@ -23,7 +23,7 @@ from django import template from django.template.defaultfilters import linebreaks # noqa from django.template.defaultfilters import safe # noqa from django.template.defaultfilters import slugify # noqa -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.importlib import import_module # noqa from django.utils.translation import ugettext_lazy as _ import six @@ -139,7 +139,7 @@ class Action(forms.Form): self.required_css_class = 'required' def __unicode__(self): - return force_unicode(self.name) + return force_text(self.name) def __repr__(self): return "<%s: %s>" % (self.__class__.__name__, self.slug) @@ -159,7 +159,7 @@ class Action(forms.Form): context = template.RequestContext(self.request, extra_context) text += tmpl.render(context) else: - text += linebreaks(force_unicode(self.help_text)) + text += linebreaks(force_text(self.help_text)) return safe(text) def add_error(self, message): @@ -285,7 +285,7 @@ class Step(object): return "<%s: %s>" % (self.__class__.__name__, self.slug) def __unicode__(self): - return force_unicode(self.name) + return force_text(self.name) def __init__(self, workflow): super(Step, self).__init__() @@ -426,7 +426,7 @@ class Step(object): def get_help_text(self): """Returns the help text for this step.""" - text = linebreaks(force_unicode(self.help_text)) + text = linebreaks(force_text(self.help_text)) text += self.action.get_help_text() return safe(text) diff --git a/openstack_dashboard/dashboards/project/containers/forms.py b/openstack_dashboard/dashboards/project/containers/forms.py index 2a8fa09bd8..a3fbaf4172 100644 --- a/openstack_dashboard/dashboards/project/containers/forms.py +++ b/openstack_dashboard/dashboards/project/containers/forms.py @@ -18,7 +18,7 @@ from django.core.urlresolvers import reverse from django.core import validators -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.translation import ugettext_lazy as _ from horizon import exceptions @@ -116,7 +116,7 @@ class UploadObject(forms.SelfHandlingForm): data['container_name'], object_path, object_file) - msg = force_unicode(_("Object was successfully uploaded.")) + msg = force_text(_("Object was successfully uploaded.")) messages.success(request, msg) return obj except Exception: diff --git a/openstack_dashboard/dashboards/project/data_processing/job_binaries/forms.py b/openstack_dashboard/dashboards/project/data_processing/job_binaries/forms.py index 050744976d..8cda391182 100644 --- a/openstack_dashboard/dashboards/project/data_processing/job_binaries/forms.py +++ b/openstack_dashboard/dashboards/project/data_processing/job_binaries/forms.py @@ -18,7 +18,7 @@ from django.forms import util from django.forms import widgets from django import template from django.template import defaultfilters -from django.utils.encoding import force_unicode +from django.utils.encoding import force_text from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ @@ -136,7 +136,7 @@ class JobBinaryCreateForm(forms.SelfHandlingForm): context = template.RequestContext(self.request, extra_context) text += tmpl.render(context) else: - text += defaultfilters.linebreaks(force_unicode(self.help_text)) + text += defaultfilters.linebreaks(force_text(self.help_text)) return defaultfilters.safe(text) class Meta: diff --git a/openstack_dashboard/dashboards/settings/user/forms.py b/openstack_dashboard/dashboards/settings/user/forms.py index 505a564434..d38e95377f 100644 --- a/openstack_dashboard/dashboards/settings/user/forms.py +++ b/openstack_dashboard/dashboards/settings/user/forms.py @@ -100,6 +100,6 @@ class UserSettingsForm(forms.SelfHandlingForm): with translation.override(lang_code): messages.success(request, - encoding.force_unicode(_("Settings saved."))) + encoding.force_text(_("Settings saved."))) return response diff --git a/tox.ini b/tox.ini index a8f1460358..ff1f670081 100644 --- a/tox.ini +++ b/tox.ini @@ -74,7 +74,7 @@ import_exceptions = collections.defaultdict, django.template.loader.render_to_string, django.test.utils.override_settings, django.utils.datastructures.SortedDict, - django.utils.encoding.force_unicode, + django.utils.encoding.force_text, django.utils.html.conditional_escape, django.utils.html.escape, django.utils.http.urlencode,