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
This commit is contained in:
Malcolm Tredinnick 2007-04-04 06:34:19 +00:00
parent f791a598a8
commit 1bddac37b6
10 changed files with 41 additions and 40 deletions

View File

@ -5,7 +5,7 @@ BR-specific Form helpers
from django.newforms import ValidationError from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES 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 from django.utils.translation import gettext
import re import re

View File

@ -4,7 +4,7 @@ FR-specific Form helpers
from django.newforms import ValidationError from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES 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 from django.utils.translation import gettext
import re import re

View File

@ -4,7 +4,6 @@ IT-specific Form helpers
from django.newforms import ValidationError from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
from django.newforms.util import smart_unicode
from django.utils.translation import gettext from django.utils.translation import gettext
import re import re

View File

@ -4,7 +4,7 @@ USA-specific Form helpers
from django.newforms import ValidationError from django.newforms import ValidationError
from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES 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 from django.utils.translation import gettext
import re import re

View File

@ -3,7 +3,8 @@ Field classes
""" """
from django.utils.translation import gettext 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 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
import datetime import datetime
import re import re

View File

@ -4,9 +4,10 @@ Form classes
from django.utils.datastructures import SortedDict, MultiValueDict from django.utils.datastructures import SortedDict, MultiValueDict
from django.utils.html import escape from django.utils.html import escape
from django.utils.encoding import StrAndUnicode
from fields import Field from fields import Field
from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
from util import flatatt, StrAndUnicode, ErrorDict, ErrorList, ValidationError from util import flatatt, ErrorDict, ErrorList, ValidationError
import copy import copy
__all__ = ('BaseForm', 'Form') __all__ = ('BaseForm', 'Form')

View File

@ -1,41 +1,12 @@
from django.conf import settings from django.conf import settings
from django.utils.html import escape from django.utils.html import escape
from django.utils.functional import Promise, lazy 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 # 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. # 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()]) 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): class ErrorDict(dict):
""" """
A collection of errors that knows how to display itself in various formats. A collection of errors that knows how to display itself in various formats.

View File

@ -9,10 +9,11 @@ __all__ = (
'MultiWidget', 'SplitDateTimeWidget', 'MultiWidget', 'SplitDateTimeWidget',
) )
from util import flatatt, StrAndUnicode, smart_unicode from util import flatatt
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.html import escape from django.utils.html import escape
from django.utils.translation import gettext from django.utils.translation import gettext
from django.utils.encoding import StrAndUnicode, smart_unicode
from itertools import chain from itertools import chain
try: try:

28
django/utils/encoding.py Normal file
View File

@ -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 File

@ -3276,7 +3276,7 @@ True
################################# #################################
# smart_unicode tests # smart_unicode tests
>>> from django.newforms.util import smart_unicode >>> from django.utils.encoding import smart_unicode
>>> class Test: >>> class Test:
... def __str__(self): ... def __str__(self):
... return 'ŠĐĆŽćžšđ' ... return 'ŠĐĆŽćžšđ'