Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@malcolmt malcolmt authored
View
2  django/contrib/localflavor/br/forms.py
@@ -5,7 +5,7 @@
from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
-from django.newforms.util import smart_unicode
+from django.utils.encoding import smart_unicode
from django.utils.translation import gettext
import re
View
2  django/contrib/localflavor/fr/forms.py
@@ -4,7 +4,7 @@
from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
-from django.newforms.util import smart_unicode
+from django.utils.encoding import smart_unicode
from django.utils.translation import gettext
import re
View
1  django/contrib/localflavor/it/forms.py
@@ -4,7 +4,6 @@
from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
-from django.newforms.util import smart_unicode
from django.utils.translation import gettext
import re
View
6 django/contrib/localflavor/usa/forms.py
@@ -4,7 +4,7 @@
from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
-from django.newforms.util import smart_unicode
+from django.utils.encoding import smart_unicode
from django.utils.translation import gettext
import re
@@ -32,9 +32,9 @@ def clean(self, value):
class USSocialSecurityNumberField(Field):
"""
A United States Social Security number.
-
+
Checks the following rules to determine whether the number is valid:
-
+
* Conforms to the XXX-XX-XXXX format.
* No group consists entirely of zeroes.
* The leading group is not "666" (block "666" will never be allocated).
View
3  django/newforms/fields.py
@@ -3,7 +3,8 @@
"""
from django.utils.translation import gettext
-from util import ErrorList, ValidationError, smart_unicode
+from django.utils.encoding import smart_unicode
+from util import ErrorList, ValidationError
from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
import datetime
import re
View
3  django/newforms/forms.py
@@ -4,9 +4,10 @@
from django.utils.datastructures import SortedDict, MultiValueDict
from django.utils.html import escape
+from django.utils.encoding import StrAndUnicode
from fields import Field
from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
-from util import flatatt, StrAndUnicode, ErrorDict, ErrorList, ValidationError
+from util import flatatt, ErrorDict, ErrorList, ValidationError
import copy
__all__ = ('BaseForm', 'Form')
View
31 django/newforms/util.py
@@ -1,41 +1,12 @@
from django.conf import settings
from django.utils.html import escape
from django.utils.functional import Promise, lazy
+from django.utils.encoding import smart_unicode
# Converts a dictionary to a single string with key="value", XML-style with
# a leading space. Assumes keys do not need to be XML-escaped.
flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
-def smart_unicode(s):
- if isinstance(s, Promise):
- # The input is something from gettext_lazy or similar. We don't want to
- # translate it until render time, so defer the conversion.
- return smart_unicode_lazy(s)
- else:
- return smart_unicode_immediate(s)
-
-def smart_unicode_immediate(s):
- if not isinstance(s, basestring):
- if hasattr(s, '__unicode__'):
- s = unicode(s)
- else:
- s = unicode(str(s), settings.DEFAULT_CHARSET)
- elif not isinstance(s, unicode):
- s = unicode(s, settings.DEFAULT_CHARSET)
- return s
-
-smart_unicode_lazy = lazy(smart_unicode_immediate, unicode)
-
-class StrAndUnicode(object):
- """
- A class whose __str__ returns its __unicode__ as a bytestring
- according to settings.DEFAULT_CHARSET.
-
- Useful as a mix-in.
- """
- def __str__(self):
- return self.__unicode__().encode(settings.DEFAULT_CHARSET)
-
class ErrorDict(dict):
"""
A collection of errors that knows how to display itself in various formats.
View
3  django/newforms/widgets.py
@@ -9,10 +9,11 @@
'MultiWidget', 'SplitDateTimeWidget',
)
-from util import flatatt, StrAndUnicode, smart_unicode
+from util import flatatt
from django.utils.datastructures import MultiValueDict
from django.utils.html import escape
from django.utils.translation import gettext
+from django.utils.encoding import StrAndUnicode, smart_unicode
from itertools import chain
try:
View
28 django/utils/encoding.py
@@ -0,0 +1,28 @@
+from django.conf import settings
+from django.utils.functional import Promise
+
+def smart_unicode(s):
+ if isinstance(s, Promise):
+ # The input is the result of a gettext_lazy() call, or similar. It will
+ # already be encoded in DEFAULT_CHARSET on evaluation and we don't want
+ # to evaluate it until render time.
+ return s
+ if not isinstance(s, basestring,):
+ if hasattr(s, '__unicode__'):
+ s = unicode(s)
+ else:
+ s = unicode(str(s), settings.DEFAULT_CHARSET)
+ elif not isinstance(s, unicode):
+ s = unicode(s, settings.DEFAULT_CHARSET)
+ return s
+
+class StrAndUnicode(object):
+ """
+ A class whose __str__ returns its __unicode__ as a bytestring
+ according to settings.DEFAULT_CHARSET.
+
+ Useful as a mix-in.
+ """
+ def __str__(self):
+ return self.__unicode__().encode(settings.DEFAULT_CHARSET)
+
View
2  tests/regressiontests/forms/tests.py
@@ -3276,7 +3276,7 @@
#################################
# smart_unicode tests
->>> from django.newforms.util import smart_unicode
+>>> from django.utils.encoding import smart_unicode
>>> class Test:
... def __str__(self):
... return 'ŠĐĆŽćžšđ'
Please sign in to comment.
Something went wrong with that request. Please try again.