Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored May 14, 2007
4  django/contrib/contenttypes/generic.py
@@ -49,7 +49,7 @@ def get_content_type(self, obj):
49 49
         
50 50
     def __get__(self, instance, instance_type=None):
51 51
         if instance is None:
52  
-            raise AttributeError, "%s must be accessed via instance" % self.name
  52
+            raise AttributeError, u"%s must be accessed via instance" % self.name
53 53
 
54 54
         try:
55 55
             return getattr(instance, self.cache_attr)
@@ -66,7 +66,7 @@ def __get__(self, instance, instance_type=None):
66 66
 
67 67
     def __set__(self, instance, value):
68 68
         if instance is None:
69  
-            raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
  69
+            raise AttributeError, u"%s must be accessed via instance" % self.related.opts.object_name
70 70
 
71 71
         ct = None
72 72
         fk = None
7  django/contrib/contenttypes/management.py
@@ -4,6 +4,8 @@
4 4
 
5 5
 from django.dispatch import dispatcher
6 6
 from django.db.models import get_apps, get_models, signals
  7
+from django.utils.translation import activate, no_trans, get_language
  8
+from django.utils.encoding import smart_unicode
7 9
 
8 10
 def create_contenttypes(app, created_models, verbosity=2):
9 11
     from django.contrib.contenttypes.models import ContentType
@@ -17,9 +19,12 @@ def create_contenttypes(app, created_models, verbosity=2):
17 19
             ContentType.objects.get(app_label=opts.app_label,
18 20
                 model=opts.object_name.lower())
19 21
         except ContentType.DoesNotExist:
20  
-            ct = ContentType(name=str(opts.verbose_name),
  22
+            lang = get_language()
  23
+            no_trans()
  24
+            ct = ContentType(name=smart_unicode(opts.verbose_name),
21 25
                 app_label=opts.app_label, model=opts.object_name.lower())
22 26
             ct.save()
  27
+            activate(lang)
23 28
             if verbosity >= 2:
24 29
                 print "Adding content type '%s | %s'" % (ct.app_label, ct.model)
25 30
 
12  django/contrib/contenttypes/models.py
... ...
@@ -1,5 +1,6 @@
1 1
 from django.db import models
2  
-from django.utils.translation import ugettext_lazy as _
  2
+from django.utils.translation import ugettext_lazy as _, no_trans, activate, get_language
  3
+from django.utils.encoding import smart_unicode
3 4
 
4 5
 CONTENT_TYPE_CACHE = {}
5 6
 class ContentTypeManager(models.Manager):
@@ -13,11 +14,14 @@ def get_for_model(self, model):
13 14
         try:
14 15
             ct = CONTENT_TYPE_CACHE[key]
15 16
         except KeyError:
16  
-            # The str() is needed around opts.verbose_name because it's a
17  
-            # django.utils.functional.__proxy__ object.
  17
+            # The unicode() is needed around opts.verbose_name because it might
  18
+            # be a django.utils.functional.__proxy__ object.
  19
+            lang = get_language()
  20
+            no_trans()
18 21
             ct, created = self.model._default_manager.get_or_create(app_label=key[0],
19  
-                model=key[1], defaults={'name': str(opts.verbose_name)})
  22
+                model=key[1], defaults={'name': smart_unicode(opts.verbose_name)})
20 23
             CONTENT_TYPE_CACHE[key] = ct
  24
+            activate(lang)
21 25
         return ct
22 26
 
23 27
     def clear_cache(self):
5  django/utils/translation/__init__.py
@@ -8,7 +8,7 @@
8 8
         'get_language', 'get_language_bidi', 'get_date_formats',
9 9
         'get_partial_date_formats', 'check_for_language', 'to_locale',
10 10
         'get_language_from_request', 'install', 'templatize', 'ugettext',
11  
-        'ungettext']
  11
+        'ungettext', 'no_trans']
12 12
 
13 13
 # Here be dragons, so a short explanation of the logic won't hurt:
14 14
 # We are trying to solve two problems: (1) access settings, in particular
@@ -105,3 +105,6 @@ def install():
105 105
 def templatize(src):
106 106
     return real_templatize(src)
107 107
 
  108
+def no_trans():
  109
+    return real_no_trans()
  110
+
2  django/utils/translation/trans_null.py
@@ -15,7 +15,7 @@ def ungettext(singular, plural, number):
15 15
 
16 16
 string_concat = lambda *strings: ''.join([str(el) for el in strings])
17 17
 activate = lambda x: None
18  
-deactivate = install = lambda: None
  18
+deactivate = no_trans = install = lambda: None
19 19
 get_language = lambda: settings.LANGUAGE_CODE
20 20
 get_language_bidi = lambda: settings.LANGUAGE_CODE in settings.LANGUAGES_BIDI
21 21
 get_date_formats = lambda: (settings.DATE_FORMAT, settings.DATETIME_FORMAT, settings.TIME_FORMAT)
13  django/utils/translation/trans_real.py
@@ -203,6 +203,14 @@ def deactivate():
203 203
     if currentThread() in _active:
204 204
         del _active[currentThread()]
205 205
 
  206
+def no_trans():
  207
+    """
  208
+    Makes the active translation object a NullTranslations() instance. This is
  209
+    useful when we want delayed translations to appear as the original string
  210
+    for some reason.
  211
+    """
  212
+    _active[currentThread()] = gettext_module.NullTranslations()
  213
+
206 214
 def get_language():
207 215
     "Returns the currently selected language."
208 216
     t = _active.get(currentThread(), None)
@@ -510,7 +518,6 @@ def string_concat(*strings):
510 518
     """"
511 519
     lazy variant of string concatenation, needed for translations that are
512 520
     constructed from multiple parts. Handles lazy strings and non-strings by
513  
-    first turning all arguments to strings, before joining them.
  521
+    first turning all arguments to unicode, before joining them.
514 522
     """
515  
-    return ''.join([str(el) for el in strings])
516  
-
  523
+    return u''.join([smart_unicode(el) for el in strings])

0 notes on commit 36c508d

Please sign in to comment.
Something went wrong with that request. Please try again.