Commit 70748600 authored by Michal Čihař's avatar Michal Čihař

Use force_text instead of force_unicode and unicode

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 114e54e1
...@@ -26,7 +26,7 @@ from django import forms ...@@ -26,7 +26,7 @@ from django import forms
from django.utils.translation import ugettext_lazy as _, pgettext from django.utils.translation import ugettext_lazy as _, pgettext
from django.contrib.auth import authenticate from django.contrib.auth import authenticate
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from weblate.accounts.models import Profile, VerifiedEmail from weblate.accounts.models import Profile, VerifiedEmail
...@@ -46,7 +46,7 @@ def remove_accents(input_str): ...@@ -46,7 +46,7 @@ def remove_accents(input_str):
""" """
Removes accents from a string. Removes accents from a string.
""" """
nkfd_form = unicodedata.normalize('NFKD', force_unicode(input_str)) nkfd_form = unicodedata.normalize('NFKD', force_text(input_str))
only_ascii = nkfd_form.encode('ASCII', 'ignore') only_ascii = nkfd_form.encode('ASCII', 'ignore')
return only_ascii return only_ascii
...@@ -66,7 +66,7 @@ def sort_choices(choices): ...@@ -66,7 +66,7 @@ def sort_choices(choices):
collator = pyuca.Collator() collator = pyuca.Collator()
return sorted( return sorted(
choices, choices,
key=lambda tup: collator.sort_key(force_unicode(tup[1])) key=lambda tup: collator.sort_key(force_text(tup[1]))
) )
...@@ -127,7 +127,7 @@ class SortedSelectMixin(object): ...@@ -127,7 +127,7 @@ class SortedSelectMixin(object):
Renders sorted options. Renders sorted options.
''' '''
# Normalize to strings. # Normalize to strings.
selected_choices = set(force_unicode(v) for v in selected_choices) selected_choices = set(force_text(v) for v in selected_choices)
output = [] output = []
# Actually sort values # Actually sort values
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import models, transaction from django.db import models, transaction
from django.db.utils import OperationalError from django.db.utils import OperationalError
from django.utils.encoding import force_text
from django.utils.translation import ugettext as _, ugettext_lazy from django.utils.translation import ugettext as _, ugettext_lazy
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.dispatch import receiver from django.dispatch import receiver
...@@ -414,7 +415,7 @@ class Language(models.Model, PercentMixin): ...@@ -414,7 +415,7 @@ class Language(models.Model, PercentMixin):
Returns label for plural form. Returns label for plural form.
''' '''
try: try:
return unicode(data.PLURAL_NAMES[self.plural_type][idx]) return force_text(data.PLURAL_NAMES[self.plural_type][idx])
except (IndexError, KeyError): except (IndexError, KeyError):
if idx == 0: if idx == 0:
return _('Singular') return _('Singular')
......
...@@ -27,6 +27,7 @@ import gettext ...@@ -27,6 +27,7 @@ import gettext
from django.test import TestCase from django.test import TestCase
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.management import call_command from django.core.management import call_command
from django.utils.encoding import force_text
from weblate.lang.models import Language, get_plural_type from weblate.lang.models import Language, get_plural_type
from weblate.lang import data from weblate.lang import data
from weblate.trans.tests.test_views import ViewTestCase from weblate.trans.tests.test_views import ViewTestCase
...@@ -227,7 +228,7 @@ class LanguagesTest(TestCase): ...@@ -227,7 +228,7 @@ class LanguagesTest(TestCase):
self.assertIn(direction, lang.get_html()) self.assertIn(direction, lang.get_html())
self.assertIn(expected, lang.get_html()) self.assertIn(expected, lang.get_html())
# Check name # Check name
self.assertEqual(unicode(lang), name) self.assertEqual(force_text(lang), name)
def test_plurals(self): def test_plurals(self):
''' '''
......
...@@ -24,7 +24,7 @@ from django.utils.translation import ( ...@@ -24,7 +24,7 @@ from django.utils.translation import (
ugettext_lazy as _, ugettext, pgettext_lazy, pgettext ugettext_lazy as _, ugettext, pgettext_lazy, pgettext
) )
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_text
from django.forms import ValidationError from django.forms import ValidationError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db.models import Q from django.db.models import Q
...@@ -232,7 +232,7 @@ class PluralTextarea(forms.Textarea): ...@@ -232,7 +232,7 @@ class PluralTextarea(forms.Textarea):
if fieldname not in data: if fieldname not in data:
break break
ret.append(data.get(fieldname, '')) ret.append(data.get(fieldname, ''))
ret = [smart_unicode(r.replace('\r', '')) for r in ret] ret = [smart_text(r.replace('\r', '')) for r in ret]
return ret return ret
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from django.utils.encoding import force_text
from weblate.trans.machine.base import MachineTranslation from weblate.trans.machine.base import MachineTranslation
from weblate.trans.models.unit import Unit from weblate.trans.models.unit import Unit
...@@ -29,7 +30,7 @@ def format_unit_match(unit, quality): ...@@ -29,7 +30,7 @@ def format_unit_match(unit, quality):
return ( return (
unit.get_target_plurals()[0], unit.get_target_plurals()[0],
quality, quality,
'Weblate (%s)' % unicode(unit.translation.subproject), 'Weblate (%s)' % force_text(unit.translation.subproject),
unit.get_source_plurals()[0], unit.get_source_plurals()[0],
) )
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
# #
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
from django.db.models import Q from django.db.models import Q
from django.utils.text import slugify from django.utils.text import slugify
from weblate.trans.models import SubProject, Project from weblate.trans.models import SubProject, Project
...@@ -326,7 +326,7 @@ class Command(BaseCommand): ...@@ -326,7 +326,7 @@ class Command(BaseCommand):
raise CommandError( raise CommandError(
'Specified --main-component was not found in matches!' 'Specified --main-component was not found in matches!'
) )
match = force_unicode(self.main_component) match = force_text(self.main_component)
matches.remove(self.main_component) matches.remove(self.main_component)
else: else:
match = matches.pop() match = matches.pop()
......
...@@ -23,6 +23,7 @@ from django.contrib.auth.models import User ...@@ -23,6 +23,7 @@ from django.contrib.auth.models import User
from django.db.models import Count, Q from django.db.models import Count, Q
from django.utils.translation import ugettext as _, ugettext_lazy from django.utils.translation import ugettext as _, ugettext_lazy
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import force_text
from weblate.trans.models.project import Project from weblate.trans.models.project import Project
from weblate.accounts.avatar import get_user_display from weblate.accounts.avatar import get_user_display
...@@ -282,9 +283,9 @@ class Change(models.Model): ...@@ -282,9 +283,9 @@ class Change(models.Model):
Returns display name for translation. Returns display name for translation.
''' '''
if self.translation is not None: if self.translation is not None:
return unicode(self.translation) return force_text(self.translation)
elif self.subproject is not None: elif self.subproject is not None:
return unicode(self.subproject) return force_text(self.subproject)
elif self.dictionary is not None: elif self.dictionary is not None:
return '%s/%s' % ( return '%s/%s' % (
self.dictionary.project, self.dictionary.project,
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
import sys import sys
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
from weblate.lang.models import Language from weblate.lang.models import Language
from weblate.trans.formats import AutoFormat from weblate.trans.formats import AutoFormat
from weblate.trans.models.project import Project from weblate.trans.models.project import Project
...@@ -131,7 +131,7 @@ class DictionaryManager(models.Manager): ...@@ -131,7 +131,7 @@ class DictionaryManager(models.Manager):
# Some Whoosh analyzers break on unicode # Some Whoosh analyzers break on unicode
try: try:
words.update( words.update(
[token.text for token in analyzer(force_unicode(text))] [token.text for token in analyzer(force_text(text))]
) )
except (UnicodeDecodeError, IndexError) as error: except (UnicodeDecodeError, IndexError) as error:
report_error(error, sys.exc_info()) report_error(error, sys.exc_info())
......
...@@ -30,6 +30,7 @@ from whoosh import qparser ...@@ -30,6 +30,7 @@ from whoosh import qparser
from django.dispatch import receiver from django.dispatch import receiver
from django.db.models.signals import post_migrate from django.db.models.signals import post_migrate
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from django.utils.encoding import force_text
from django.db import transaction from django.db import transaction
from weblate import appsettings from weblate import appsettings
from weblate.lang.models import Language from weblate.lang.models import Language
...@@ -92,10 +93,10 @@ def update_source_unit_index(writer, unit): ...@@ -92,10 +93,10 @@ def update_source_unit_index(writer, unit):
Updates source index for given unit. Updates source index for given unit.
''' '''
writer.update_document( writer.update_document(
checksum=unicode(unit.checksum), checksum=force_text(unit.checksum),
source=unicode(unit.source), source=force_text(unit.source),
context=unicode(unit.context), context=force_text(unit.context),
location=unicode(unit.location), location=force_text(unit.location),
) )
...@@ -104,9 +105,9 @@ def update_target_unit_index(writer, unit): ...@@ -104,9 +105,9 @@ def update_target_unit_index(writer, unit):
Updates target index for given unit. Updates target index for given unit.
''' '''
writer.update_document( writer.update_document(
checksum=unicode(unit.checksum), checksum=force_text(unit.checksum),
target=unicode(unit.target), target=force_text(unit.target),
comment=unicode(unit.comment), comment=force_text(unit.comment),
) )
......
...@@ -23,7 +23,7 @@ from django.utils.html import escape, urlize ...@@ -23,7 +23,7 @@ from django.utils.html import escape, urlize
from django.contrib.admin.templatetags.admin_static import static from django.contrib.admin.templatetags.admin_static import static
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode from django.utils.encoding import force_text
from django.utils.translation import ugettext as _, ungettext, ugettext_lazy from django.utils.translation import ugettext as _, ungettext, ugettext_lazy
from django.utils import timezone from django.utils import timezone
from django import template from django import template
...@@ -111,11 +111,11 @@ def format_translation(value, language, diff=None, search_match=None, ...@@ -111,11 +111,11 @@ def format_translation(value, language, diff=None, search_match=None,
for idx, value in enumerate(plurals): for idx, value in enumerate(plurals):
# HTML escape # HTML escape
value = escape(force_unicode(value)) value = escape(force_text(value))
# Format diff if there is any # Format diff if there is any
if diff is not None: if diff is not None:
diffvalue = escape(force_unicode(diff[idx])) diffvalue = escape(force_text(diff[idx]))
value = html_diff(diffvalue, value) value = html_diff(diffvalue, value)
# Format search term # Format search term
......
...@@ -24,6 +24,7 @@ Tests for automatix fixups. ...@@ -24,6 +24,7 @@ Tests for automatix fixups.
from __future__ import unicode_literals from __future__ import unicode_literals
from django.test import TestCase from django.test import TestCase
from django.utils.encoding import force_text
from weblate.trans.tests.test_checks import MockUnit from weblate.trans.tests.test_checks import MockUnit
from weblate.trans.autofixes import fix_target from weblate.trans.autofixes import fix_target
from weblate.trans.autofixes.chars import ( from weblate.trans.autofixes.chars import (
...@@ -115,4 +116,4 @@ class AutoFixTest(TestCase): ...@@ -115,4 +116,4 @@ class AutoFixTest(TestCase):
fixed, fixups = fix_target(['Bar...'], unit) fixed, fixups = fix_target(['Bar...'], unit)
self.assertEqual(fixed, ['Bar…']) self.assertEqual(fixed, ['Bar…'])
self.assertEqual(len(fixups), 1) self.assertEqual(len(fixups), 1)
self.assertEqual(unicode(fixups[0]), 'Trailing ellipsis') self.assertEqual(force_text(fixups[0]), 'Trailing ellipsis')
...@@ -24,6 +24,7 @@ Tests for unitdata models. ...@@ -24,6 +24,7 @@ Tests for unitdata models.
from __future__ import unicode_literals from __future__ import unicode_literals
from django.test import TestCase from django.test import TestCase
from django.utils.encoding import force_text
from weblate.lang.models import Language from weblate.lang.models import Language
from weblate.trans.models import Check, Project from weblate.trans.models import Check, Project
...@@ -45,14 +46,14 @@ class UnitdataTestCase(TestCase): ...@@ -45,14 +46,14 @@ class UnitdataTestCase(TestCase):
def test_check(self): def test_check(self):
check = self.create_check('same') check = self.create_check('same')
self.assertEqual( self.assertEqual(
unicode(check.get_description()), force_text(check.get_description()),
'Source and translated strings are same' 'Source and translated strings are same'
) )
self.assertEqual(check.get_severity(), 'warning') self.assertEqual(check.get_severity(), 'warning')
self.assertTrue( self.assertTrue(
check.get_doc_url().endswith('user/checks.html#check-same') check.get_doc_url().endswith('user/checks.html#check-same')
) )
self.assertEqual(unicode(check), 'test/Acholi: same') self.assertEqual(force_text(check), 'test/Acholi: same')
def test_check_nonexisting(self): def test_check_nonexisting(self):
check = self.create_check('-invalid-') check = self.create_check('-invalid-')
......
...@@ -23,6 +23,7 @@ from django.core.cache import cache ...@@ -23,6 +23,7 @@ from django.core.cache import cache
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import resolve_url from django.shortcuts import resolve_url
from django.conf import settings from django.conf import settings
from django.utils.encoding import force_text
from importlib import import_module from importlib import import_module
import os import os
import sys import sys
...@@ -226,7 +227,7 @@ def report_error(error, exc_info, request=None, extra_data=None): ...@@ -226,7 +227,7 @@ def report_error(error, exc_info, request=None, extra_data=None):
LOGGER.error( LOGGER.error(
'Handled exception %s: %s', 'Handled exception %s: %s',
error.__class__.__name__, error.__class__.__name__,
unicode(error).encode('utf-8') force_text(error).encode('utf-8')
) )
# Print error when running testsuite # Print error when running testsuite
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
# #
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
from django.utils.encoding import force_text
from django.utils.translation import ugettext as _, ungettext from django.utils.translation import ugettext as _, ungettext
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages from django.contrib import messages
...@@ -169,7 +170,7 @@ def upload_dictionary(request, project, lang): ...@@ -169,7 +170,7 @@ def upload_dictionary(request, project, lang):
except Exception as error: except Exception as error:
report_error(error, sys.exc_info(), request) report_error(error, sys.exc_info(), request)
messages.error( messages.error(
request, _('File upload has failed: %s') % unicode(error) request, _('File upload has failed: %s') % force_text(error)
) )
else: else:
messages.error(request, _('Failed to process form!')) messages.error(request, _('Failed to process form!'))
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.encoding import force_text
from django.http import HttpResponseRedirect, HttpResponse from django.http import HttpResponseRedirect, HttpResponse
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
...@@ -161,7 +162,7 @@ def search(translation, request): ...@@ -161,7 +162,7 @@ def search(translation, request):
search_id = str(uuid.uuid1()) search_id = str(uuid.uuid1())
search_result = { search_result = {
'query': search_query, 'query': search_query,
'name': unicode(name) if name else None, 'name': force_text(name) if name else None,
'ids': unit_ids, 'ids': unit_ids,
'search_id': search_id, 'search_id': search_id,
'ttl': int(time.time()) + 86400, 'ttl': int(time.time()) + 86400,
...@@ -238,7 +239,7 @@ def perform_translation(unit, form, request): ...@@ -238,7 +239,7 @@ def perform_translation(unit, form, request):
messages.info( messages.info(
request, request,
_('Following fixups were applied to translation: %s') % _('Following fixups were applied to translation: %s') %
', '.join([unicode(f) for f in fixups]) ', '.join([force_text(f) for f in fixups])
) )
# Get new set of checks # Get new set of checks
...@@ -255,7 +256,7 @@ def perform_translation(unit, form, request): ...@@ -255,7 +256,7 @@ def perform_translation(unit, form, request):
'Some checks have failed on your translation: {0}' 'Some checks have failed on your translation: {0}'
).format( ).format(
', '.join( ', '.join(
[unicode(CHECKS[check].name) for check in newchecks] [force_text(CHECKS[check].name) for check in newchecks]
) )
) )
) )
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
# #
from django.utils.translation import ugettext as _, ungettext from django.utils.translation import ugettext as _, ungettext
from django.utils.encoding import force_text
from django.shortcuts import redirect from django.shortcuts import redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib import messages from django.contrib import messages
...@@ -141,7 +142,7 @@ def upload_translation(request, project, subproject, lang): ...@@ -141,7 +142,7 @@ def upload_translation(request, project, subproject, lang):
) )
except Exception as error: except Exception as error:
messages.error( messages.error(
request, _('File content merge failed: %s') % unicode(error) request, _('File content merge failed: %s') % force_text(error)
) )
report_error(error, sys.exc_info(), request) report_error(error, sys.exc_info(), request)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment