From 36c508d1ed64695f864df70cd5d4d37b0c9e7a01 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 14 May 2007 19:05:16 +0000 Subject: [PATCH] unicode: Audited contrib.contentypes for unicode problems. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5240 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/contenttypes/generic.py | 4 ++-- django/contrib/contenttypes/management.py | 7 ++++++- django/contrib/contenttypes/models.py | 12 ++++++++---- django/utils/translation/__init__.py | 5 ++++- django/utils/translation/trans_null.py | 2 +- django/utils/translation/trans_real.py | 13 ++++++++++--- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index f995ab2044..efa49edf73 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -49,7 +49,7 @@ class GenericForeignKey(object): def __get__(self, instance, instance_type=None): if instance is None: - raise AttributeError, "%s must be accessed via instance" % self.name + raise AttributeError, u"%s must be accessed via instance" % self.name try: return getattr(instance, self.cache_attr) @@ -66,7 +66,7 @@ class GenericForeignKey(object): def __set__(self, instance, value): if instance is None: - raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name + raise AttributeError, u"%s must be accessed via instance" % self.related.opts.object_name ct = None fk = None diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py index 3572d93049..9314ff7393 100644 --- a/django/contrib/contenttypes/management.py +++ b/django/contrib/contenttypes/management.py @@ -4,6 +4,8 @@ Creates content types for all installed models. from django.dispatch import dispatcher from django.db.models import get_apps, get_models, signals +from django.utils.translation import activate, no_trans, get_language +from django.utils.encoding import smart_unicode def create_contenttypes(app, created_models, verbosity=2): from django.contrib.contenttypes.models import ContentType @@ -17,9 +19,12 @@ def create_contenttypes(app, created_models, verbosity=2): ContentType.objects.get(app_label=opts.app_label, model=opts.object_name.lower()) except ContentType.DoesNotExist: - ct = ContentType(name=str(opts.verbose_name), + lang = get_language() + no_trans() + ct = ContentType(name=smart_unicode(opts.verbose_name), app_label=opts.app_label, model=opts.object_name.lower()) ct.save() + activate(lang) if verbosity >= 2: print "Adding content type '%s | %s'" % (ct.app_label, ct.model) diff --git a/django/contrib/contenttypes/models.py b/django/contrib/contenttypes/models.py index 314dff2cb2..4b23a2e008 100644 --- a/django/contrib/contenttypes/models.py +++ b/django/contrib/contenttypes/models.py @@ -1,5 +1,6 @@ from django.db import models -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext_lazy as _, no_trans, activate, get_language +from django.utils.encoding import smart_unicode CONTENT_TYPE_CACHE = {} class ContentTypeManager(models.Manager): @@ -13,11 +14,14 @@ class ContentTypeManager(models.Manager): try: ct = CONTENT_TYPE_CACHE[key] except KeyError: - # The str() is needed around opts.verbose_name because it's a - # django.utils.functional.__proxy__ object. + # The unicode() is needed around opts.verbose_name because it might + # be a django.utils.functional.__proxy__ object. + lang = get_language() + no_trans() ct, created = self.model._default_manager.get_or_create(app_label=key[0], - model=key[1], defaults={'name': str(opts.verbose_name)}) + model=key[1], defaults={'name': smart_unicode(opts.verbose_name)}) CONTENT_TYPE_CACHE[key] = ct + activate(lang) return ct def clear_cache(self): diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 8205a4f957..563fa92f50 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -8,7 +8,7 @@ __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', 'get_language', 'get_language_bidi', 'get_date_formats', 'get_partial_date_formats', 'check_for_language', 'to_locale', 'get_language_from_request', 'install', 'templatize', 'ugettext', - 'ungettext'] + 'ungettext', 'no_trans'] # Here be dragons, so a short explanation of the logic won't hurt: # We are trying to solve two problems: (1) access settings, in particular @@ -105,3 +105,6 @@ def install(): def templatize(src): return real_templatize(src) +def no_trans(): + return real_no_trans() + diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py index a4b30737ca..433af3897c 100644 --- a/django/utils/translation/trans_null.py +++ b/django/utils/translation/trans_null.py @@ -15,7 +15,7 @@ def ungettext(singular, plural, number): string_concat = lambda *strings: ''.join([str(el) for el in strings]) activate = lambda x: None -deactivate = install = lambda: None +deactivate = no_trans = install = lambda: None get_language = lambda: settings.LANGUAGE_CODE get_language_bidi = lambda: settings.LANGUAGE_CODE in settings.LANGUAGES_BIDI get_date_formats = lambda: (settings.DATE_FORMAT, settings.DATETIME_FORMAT, settings.TIME_FORMAT) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index a78a0f9af9..e57408b0fe 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -203,6 +203,14 @@ def deactivate(): if currentThread() in _active: del _active[currentThread()] +def no_trans(): + """ + Makes the active translation object a NullTranslations() instance. This is + useful when we want delayed translations to appear as the original string + for some reason. + """ + _active[currentThread()] = gettext_module.NullTranslations() + def get_language(): "Returns the currently selected language." t = _active.get(currentThread(), None) @@ -510,7 +518,6 @@ def string_concat(*strings): """" lazy variant of string concatenation, needed for translations that are constructed from multiple parts. Handles lazy strings and non-strings by - first turning all arguments to strings, before joining them. + first turning all arguments to unicode, before joining them. """ - return ''.join([str(el) for el in strings]) - + return u''.join([smart_unicode(el) for el in strings])