Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

unicode: Audited contrib.contentypes for unicode problems.

git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5240 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 36c508d1ed64695f864df70cd5d4d37b0c9e7a01 1 parent 03b86ea
Malcolm Tredinnick malcolmt authored
4 django/contrib/contenttypes/generic.py
View
@@ -49,7 +49,7 @@ def get_content_type(self, obj):
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 @@ def __get__(self, instance, instance_type=None):
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
7 django/contrib/contenttypes/management.py
View
@@ -4,6 +4,8 @@
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)
12 django/contrib/contenttypes/models.py
View
@@ -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 @@ def get_for_model(self, model):
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):
5 django/utils/translation/__init__.py
View
@@ -8,7 +8,7 @@
'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()
+
2  django/utils/translation/trans_null.py
View
@@ -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)
13 django/utils/translation/trans_real.py
View
@@ -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])
Please sign in to comment.
Something went wrong with that request. Please try again.