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:
parent
f791a598a8
commit
1bddac37b6
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
@ -32,9 +32,9 @@ class USPhoneNumberField(Field):
|
||||||
class USSocialSecurityNumberField(Field):
|
class USSocialSecurityNumberField(Field):
|
||||||
"""
|
"""
|
||||||
A United States Social Security number.
|
A United States Social Security number.
|
||||||
|
|
||||||
Checks the following rules to determine whether the number is valid:
|
Checks the following rules to determine whether the number is valid:
|
||||||
|
|
||||||
* Conforms to the XXX-XX-XXXX format.
|
* Conforms to the XXX-XX-XXXX format.
|
||||||
* No group consists entirely of zeroes.
|
* No group consists entirely of zeroes.
|
||||||
* The leading group is not "666" (block "666" will never be allocated).
|
* The leading group is not "666" (block "666" will never be allocated).
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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')
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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 'ŠĐĆŽćžšđ'
|
||||||
|
|
Loading…
Reference in New Issue