Skip to content

Commit

Permalink
Moved smart_unicode and StrAndUnicode to django.utils.encoding. They …
Browse files Browse the repository at this point in the history
…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
malcolmt committed Apr 4, 2007
1 parent f791a59 commit 1bddac3
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion django/contrib/localflavor/br/forms.py
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion django/contrib/localflavor/fr/forms.py
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion django/contrib/localflavor/it/forms.py
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions django/contrib/localflavor/usa/forms.py
Expand Up @@ -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

Expand Down Expand Up @@ -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).
Expand Down
3 changes: 2 additions & 1 deletion django/newforms/fields.py
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion django/newforms/forms.py
Expand Up @@ -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')
Expand Down
31 changes: 1 addition & 30 deletions 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.
Expand Down
3 changes: 2 additions & 1 deletion django/newforms/widgets.py
Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions 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)

2 changes: 1 addition & 1 deletion tests/regressiontests/forms/tests.py
Expand Up @@ -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 'ŠĐĆŽćžšđ'
Expand Down

0 comments on commit 1bddac3

Please sign in to comment.