From 6c0219cf72dc8525284bde509cfac95d546f9783 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 5 Dec 2006 20:08:27 +0000 Subject: [PATCH] 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 --- django/newforms/forms.py | 10 +++++----- django/newforms/util.py | 15 ++++++++++++--- django/newforms/widgets.py | 10 +++++----- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/django/newforms/forms.py b/django/newforms/forms.py index c907a03a04..503e17937a 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -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. diff --git a/django/newforms/util.py b/django/newforms/util.py index a5cc4932ea..a78623a17b 100644 --- a/django/newforms/util.py +++ b/django/newforms/util.py @@ -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. diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 274ba01225..1a2d7d67c9 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -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'') return u'\n'.join(output) -class RadioInput(object): +class RadioInput(StrAndUnicode): "An object used by RadioFieldRenderer that represents a single ." 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'' % (self.tag(), self.choice_label) def is_checked(self): @@ -168,7 +168,7 @@ class RadioInput(object): final_attrs['checked'] = 'checked' return u'' % 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