newforms: Added __unicode__() methods wherever there were __str__() methods, and changed the __str__() methods to delegate to __unicode__().encode(settings.DEFAULT_CHARSET)
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4163 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a794acbd7a
commit
6c0219cf72
|
@ -6,7 +6,7 @@ from django.utils.datastructures import SortedDict
|
|||
from django.utils.html import escape
|
||||
from fields import Field
|
||||
from widgets import TextInput, Textarea, HiddenInput
|
||||
from util import ErrorDict, ErrorList, ValidationError
|
||||
from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError
|
||||
|
||||
NON_FIELD_ERRORS = '__all__'
|
||||
|
||||
|
@ -32,7 +32,7 @@ class DeclarativeFieldsMetaclass(type):
|
|||
attrs['fields'] = SortedDictFromList(fields)
|
||||
return type.__new__(cls, name, bases, attrs)
|
||||
|
||||
class Form(object):
|
||||
class Form(StrAndUnicode):
|
||||
"A collection of Fields, plus their associated data."
|
||||
__metaclass__ = DeclarativeFieldsMetaclass
|
||||
|
||||
|
@ -43,7 +43,7 @@ class Form(object):
|
|||
self.clean_data = None # Stores the data after clean() has been called.
|
||||
self.__errors = None # Stores the errors after clean() has been called.
|
||||
|
||||
def __str__(self):
|
||||
def __unicode__(self):
|
||||
return self.as_table()
|
||||
|
||||
def __iter__(self):
|
||||
|
@ -155,14 +155,14 @@ class Form(object):
|
|||
"""
|
||||
return self.clean_data
|
||||
|
||||
class BoundField(object):
|
||||
class BoundField(StrAndUnicode):
|
||||
"A Field plus data"
|
||||
def __init__(self, form, field, name):
|
||||
self.form = form
|
||||
self.field = field
|
||||
self.name = name
|
||||
|
||||
def __str__(self):
|
||||
def __unicode__(self):
|
||||
"Renders this field as an HTML widget."
|
||||
# Use the 'widget' attribute on the field to determine which type
|
||||
# of HTML widget to use.
|
||||
|
|
|
@ -1,13 +1,22 @@
|
|||
# Default encoding for input byte strings.
|
||||
DEFAULT_ENCODING = 'utf-8' # TODO: First look at django.conf.settings, then fall back to this.
|
||||
from django.conf import settings
|
||||
|
||||
def smart_unicode(s):
|
||||
if not isinstance(s, basestring):
|
||||
s = unicode(str(s))
|
||||
elif not isinstance(s, unicode):
|
||||
s = unicode(s, DEFAULT_ENCODING)
|
||||
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)
|
||||
|
||||
class ErrorDict(dict):
|
||||
"""
|
||||
A collection of errors that knows how to display itself in various formats.
|
||||
|
|
|
@ -8,7 +8,7 @@ __all__ = (
|
|||
'Select', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple',
|
||||
)
|
||||
|
||||
from util import smart_unicode
|
||||
from util import StrAndUnicode, smart_unicode
|
||||
from django.utils.html import escape
|
||||
from itertools import chain
|
||||
|
||||
|
@ -146,7 +146,7 @@ class SelectMultiple(Widget):
|
|||
output.append(u'</select>')
|
||||
return u'\n'.join(output)
|
||||
|
||||
class RadioInput(object):
|
||||
class RadioInput(StrAndUnicode):
|
||||
"An object used by RadioFieldRenderer that represents a single <input type='radio'>."
|
||||
def __init__(self, name, value, attrs, choice, index):
|
||||
self.name, self.value = name, value
|
||||
|
@ -154,7 +154,7 @@ class RadioInput(object):
|
|||
self.choice_value, self.choice_label = choice
|
||||
self.index = index
|
||||
|
||||
def __str__(self):
|
||||
def __unicode__(self):
|
||||
return u'<label>%s %s</label>' % (self.tag(), self.choice_label)
|
||||
|
||||
def is_checked(self):
|
||||
|
@ -168,7 +168,7 @@ class RadioInput(object):
|
|||
final_attrs['checked'] = 'checked'
|
||||
return u'<input%s />' % flatatt(final_attrs)
|
||||
|
||||
class RadioFieldRenderer(object):
|
||||
class RadioFieldRenderer(StrAndUnicode):
|
||||
"An object used by RadioSelect to enable customization of radio widgets."
|
||||
def __init__(self, name, value, attrs, choices):
|
||||
self.name, self.value, self.attrs = name, value, attrs
|
||||
|
@ -178,7 +178,7 @@ class RadioFieldRenderer(object):
|
|||
for i, choice in enumerate(self.choices):
|
||||
yield RadioInput(self.name, self.value, self.attrs.copy(), choice, i)
|
||||
|
||||
def __str__(self):
|
||||
def __unicode__(self):
|
||||
"Outputs a <ul> for this set of radio fields."
|
||||
return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self])
|
||||
|
||||
|
|
Loading…
Reference in New Issue