Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved smart_unicode and StrAndUnicode to django.utils.encoding. They …

…are useful

outside of newforms. This is backwards compatible as far as smart_unicode goes
(since newforms.util still imports it). All imports of smart_unicode and
StrAndUnicode have also been updated.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4918 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1bddac37b69152d919a3af29f2844ae3e34c7237 1 parent f791a59
Malcolm Tredinnick authored April 04, 2007
2  django/contrib/localflavor/br/forms.py
@@ -5,7 +5,7 @@
5 5
 
6 6
 from django.newforms import ValidationError
7 7
 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
8  
-from django.newforms.util import smart_unicode
  8
+from django.utils.encoding import smart_unicode
9 9
 from django.utils.translation import gettext
10 10
 import re
11 11
 
2  django/contrib/localflavor/fr/forms.py
@@ -4,7 +4,7 @@
4 4
 
5 5
 from django.newforms import ValidationError
6 6
 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
7  
-from django.newforms.util import smart_unicode
  7
+from django.utils.encoding import smart_unicode
8 8
 from django.utils.translation import gettext
9 9
 import re
10 10
 
1  django/contrib/localflavor/it/forms.py
@@ -4,7 +4,6 @@
4 4
 
5 5
 from django.newforms import ValidationError
6 6
 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
7  
-from django.newforms.util import smart_unicode
8 7
 from django.utils.translation import gettext
9 8
 import re
10 9
 
6  django/contrib/localflavor/usa/forms.py
@@ -4,7 +4,7 @@
4 4
 
5 5
 from django.newforms import ValidationError
6 6
 from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
7  
-from django.newforms.util import smart_unicode
  7
+from django.utils.encoding import smart_unicode
8 8
 from django.utils.translation import gettext
9 9
 import re
10 10
 
@@ -32,9 +32,9 @@ def clean(self, value):
32 32
 class USSocialSecurityNumberField(Field):
33 33
     """
34 34
     A United States Social Security number.
35  
-    
  35
+
36 36
     Checks the following rules to determine whether the number is valid:
37  
-    
  37
+
38 38
         * Conforms to the XXX-XX-XXXX format.
39 39
         * No group consists entirely of zeroes.
40 40
         * The leading group is not "666" (block "666" will never be allocated).
3  django/newforms/fields.py
@@ -3,7 +3,8 @@
3 3
 """
4 4
 
5 5
 from django.utils.translation import gettext
6  
-from util import ErrorList, ValidationError, smart_unicode
  6
+from django.utils.encoding import smart_unicode
  7
+from util import ErrorList, ValidationError
7 8
 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
8 9
 import datetime
9 10
 import re
3  django/newforms/forms.py
@@ -4,9 +4,10 @@
4 4
 
5 5
 from django.utils.datastructures import SortedDict, MultiValueDict
6 6
 from django.utils.html import escape
  7
+from django.utils.encoding import StrAndUnicode
7 8
 from fields import Field
8 9
 from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
9  
-from util import flatatt, StrAndUnicode, ErrorDict, ErrorList, ValidationError
  10
+from util import flatatt, ErrorDict, ErrorList, ValidationError
10 11
 import copy
11 12
 
12 13
 __all__ = ('BaseForm', 'Form')
31  django/newforms/util.py
... ...
@@ -1,41 +1,12 @@
1 1
 from django.conf import settings
2 2
 from django.utils.html import escape
3 3
 from django.utils.functional import Promise, lazy
  4
+from django.utils.encoding import smart_unicode
4 5
 
5 6
 # Converts a dictionary to a single string with key="value", XML-style with
6 7
 # a leading space. Assumes keys do not need to be XML-escaped.
7 8
 flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
8 9
 
9  
-def smart_unicode(s):
10  
-    if isinstance(s, Promise):
11  
-        # The input is something from gettext_lazy or similar. We don't want to
12  
-        # translate it until render time, so defer the conversion.
13  
-        return smart_unicode_lazy(s)
14  
-    else:
15  
-        return smart_unicode_immediate(s)
16  
-
17  
-def smart_unicode_immediate(s):
18  
-    if not isinstance(s, basestring):
19  
-        if hasattr(s, '__unicode__'):
20  
-            s = unicode(s)
21  
-        else:
22  
-            s = unicode(str(s), settings.DEFAULT_CHARSET)
23  
-    elif not isinstance(s, unicode):
24  
-        s = unicode(s, settings.DEFAULT_CHARSET)
25  
-    return s
26  
-
27  
-smart_unicode_lazy = lazy(smart_unicode_immediate, unicode)
28  
-
29  
-class StrAndUnicode(object):
30  
-    """
31  
-    A class whose __str__ returns its __unicode__ as a bytestring
32  
-    according to settings.DEFAULT_CHARSET.
33  
-
34  
-    Useful as a mix-in.
35  
-    """
36  
-    def __str__(self):
37  
-        return self.__unicode__().encode(settings.DEFAULT_CHARSET)
38  
-
39 10
 class ErrorDict(dict):
40 11
     """
41 12
     A collection of errors that knows how to display itself in various formats.
3  django/newforms/widgets.py
@@ -9,10 +9,11 @@
9 9
     'MultiWidget', 'SplitDateTimeWidget',
10 10
 )
11 11
 
12  
-from util import flatatt, StrAndUnicode, smart_unicode
  12
+from util import flatatt
13 13
 from django.utils.datastructures import MultiValueDict
14 14
 from django.utils.html import escape
15 15
 from django.utils.translation import gettext
  16
+from django.utils.encoding import StrAndUnicode, smart_unicode
16 17
 from itertools import chain
17 18
 
18 19
 try:
28  django/utils/encoding.py
... ...
@@ -0,0 +1,28 @@
  1
+from django.conf import settings
  2
+from django.utils.functional import Promise
  3
+
  4
+def smart_unicode(s):
  5
+    if isinstance(s, Promise):
  6
+        # The input is the result of a gettext_lazy() call, or similar. It will
  7
+        # already be encoded in DEFAULT_CHARSET on evaluation and we don't want
  8
+        # to evaluate it until render time.
  9
+        return s
  10
+    if not isinstance(s, basestring,):
  11
+        if hasattr(s, '__unicode__'):
  12
+            s = unicode(s)
  13
+        else:
  14
+            s = unicode(str(s), settings.DEFAULT_CHARSET)
  15
+    elif not isinstance(s, unicode):
  16
+        s = unicode(s, settings.DEFAULT_CHARSET)
  17
+    return s
  18
+
  19
+class StrAndUnicode(object):
  20
+    """
  21
+    A class whose __str__ returns its __unicode__ as a bytestring
  22
+    according to settings.DEFAULT_CHARSET.
  23
+
  24
+    Useful as a mix-in.
  25
+    """
  26
+    def __str__(self):
  27
+        return self.__unicode__().encode(settings.DEFAULT_CHARSET)
  28
+
2  tests/regressiontests/forms/tests.py
@@ -3276,7 +3276,7 @@
3276 3276
 #################################
3277 3277
 
3278 3278
 # smart_unicode tests
3279  
->>> from django.newforms.util import smart_unicode
  3279
+>>> from django.utils.encoding import smart_unicode
3280 3280
 >>> class Test:
3281 3281
 ...     def __str__(self):
3282 3282
 ...        return 'ŠĐĆŽćžšđ'

0 notes on commit 1bddac3

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