mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
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:
28
django/utils/encoding.py
Normal file
28
django/utils/encoding.py
Normal 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)
|
||||
|
||||
Reference in New Issue
Block a user