Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: Added __unicode__() methods wherever there were __str__() m…

…ethods, 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
  • Loading branch information...
commit 6c0219cf72dc8525284bde509cfac95d546f9783 1 parent a794acb
Adrian Holovaty authored December 05, 2006
10  django/newforms/forms.py
@@ -6,7 +6,7 @@
6 6
 from django.utils.html import escape
7 7
 from fields import Field
8 8
 from widgets import TextInput, Textarea, HiddenInput
9  
-from util import ErrorDict, ErrorList, ValidationError
  9
+from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError
10 10
 
11 11
 NON_FIELD_ERRORS = '__all__'
12 12
 
@@ -32,7 +32,7 @@ def __new__(cls, name, bases, attrs):
32 32
         attrs['fields'] = SortedDictFromList(fields)
33 33
         return type.__new__(cls, name, bases, attrs)
34 34
 
35  
-class Form(object):
  35
+class Form(StrAndUnicode):
36 36
     "A collection of Fields, plus their associated data."
37 37
     __metaclass__ = DeclarativeFieldsMetaclass
38 38
 
@@ -43,7 +43,7 @@ def __init__(self, data=None, auto_id=False): # TODO: prefix stuff
43 43
         self.clean_data = None # Stores the data after clean() has been called.
44 44
         self.__errors = None # Stores the errors after clean() has been called.
45 45
 
46  
-    def __str__(self):
  46
+    def __unicode__(self):
47 47
         return self.as_table()
48 48
 
49 49
     def __iter__(self):
@@ -155,14 +155,14 @@ def clean(self):
155 155
         """
156 156
         return self.clean_data
157 157
 
158  
-class BoundField(object):
  158
+class BoundField(StrAndUnicode):
159 159
     "A Field plus data"
160 160
     def __init__(self, form, field, name):
161 161
         self.form = form
162 162
         self.field = field
163 163
         self.name = name
164 164
 
165  
-    def __str__(self):
  165
+    def __unicode__(self):
166 166
         "Renders this field as an HTML widget."
167 167
         # Use the 'widget' attribute on the field to determine which type
168 168
         # of HTML widget to use.
15  django/newforms/util.py
... ...
@@ -1,13 +1,22 @@
1  
-# Default encoding for input byte strings.
2  
-DEFAULT_ENCODING = 'utf-8' # TODO: First look at django.conf.settings, then fall back to this.
  1
+from django.conf import settings
3 2
 
4 3
 def smart_unicode(s):
5 4
     if not isinstance(s, basestring):
6 5
         s = unicode(str(s))
7 6
     elif not isinstance(s, unicode):
8  
-        s = unicode(s, DEFAULT_ENCODING)
  7
+        s = unicode(s, settings.DEFAULT_CHARSET)
9 8
     return s
10 9
 
  10
+class StrAndUnicode(object):
  11
+    """
  12
+    A class whose __str__ returns its __unicode__ as a bytestring
  13
+    according to settings.DEFAULT_CHARSET.
  14
+
  15
+    Useful as a mix-in.
  16
+    """
  17
+    def __str__(self):
  18
+        return self.__unicode__().encode(settings.DEFAULT_CHARSET)
  19
+
11 20
 class ErrorDict(dict):
12 21
     """
13 22
     A collection of errors that knows how to display itself in various formats.
10  django/newforms/widgets.py
@@ -8,7 +8,7 @@
8 8
     'Select', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple',
9 9
 )
10 10
 
11  
-from util import smart_unicode
  11
+from util import StrAndUnicode, smart_unicode
12 12
 from django.utils.html import escape
13 13
 from itertools import chain
14 14
 
@@ -146,7 +146,7 @@ def render(self, name, value, attrs=None, choices=()):
146 146
         output.append(u'</select>')
147 147
         return u'\n'.join(output)
148 148
 
149  
-class RadioInput(object):
  149
+class RadioInput(StrAndUnicode):
150 150
     "An object used by RadioFieldRenderer that represents a single <input type='radio'>."
151 151
     def __init__(self, name, value, attrs, choice, index):
152 152
         self.name, self.value = name, value
@@ -154,7 +154,7 @@ def __init__(self, name, value, attrs, choice, index):
154 154
         self.choice_value, self.choice_label = choice
155 155
         self.index = index
156 156
 
157  
-    def __str__(self):
  157
+    def __unicode__(self):
158 158
         return u'<label>%s %s</label>' % (self.tag(), self.choice_label)
159 159
 
160 160
     def is_checked(self):
@@ -168,7 +168,7 @@ def tag(self):
168 168
             final_attrs['checked'] = 'checked'
169 169
         return u'<input%s />' % flatatt(final_attrs)
170 170
 
171  
-class RadioFieldRenderer(object):
  171
+class RadioFieldRenderer(StrAndUnicode):
172 172
     "An object used by RadioSelect to enable customization of radio widgets."
173 173
     def __init__(self, name, value, attrs, choices):
174 174
         self.name, self.value, self.attrs = name, value, attrs
@@ -178,7 +178,7 @@ def __iter__(self):
178 178
         for i, choice in enumerate(self.choices):
179 179
             yield RadioInput(self.name, self.value, self.attrs.copy(), choice, i)
180 180
 
181  
-    def __str__(self):
  181
+    def __unicode__(self):
182 182
         "Outputs a <ul> for this set of radio fields."
183 183
         return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self])
184 184
 

0 notes on commit 6c0219c

Please sign in to comment.
Something went wrong with that request. Please try again.