mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
unicode: Audited contrib.contentypes for unicode problems.
git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5240 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
03b86ea12d
commit
36c508d1ed
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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):
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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])
|
||||
|
Loading…
x
Reference in New Issue
Block a user