Django

Code

Changeset 4163

Show
Ignore:
Timestamp:
12/05/06 14:08:27 (2 years ago)
Author:
adrian
Message:

newforms: Added unicode() methods wherever there were str() methods, and changed the str() methods to delegate to unicode().encode(settings.DEFAULT_CHARSET)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/forms.py

    r4160 r4163  
    77from fields import Field 
    88from widgets import TextInput, Textarea, HiddenInput 
    9 from util import ErrorDict, ErrorList, ValidationError 
     9from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError 
    1010 
    1111NON_FIELD_ERRORS = '__all__' 
     
    3333        return type.__new__(cls, name, bases, attrs) 
    3434 
    35 class Form(object): 
     35class Form(StrAndUnicode): 
    3636    "A collection of Fields, plus their associated data." 
    3737    __metaclass__ = DeclarativeFieldsMetaclass 
     
    4444        self.__errors = None # Stores the errors after clean() has been called. 
    4545 
    46     def __str__(self): 
     46    def __unicode__(self): 
    4747        return self.as_table() 
    4848 
     
    156156        return self.clean_data 
    157157 
    158 class BoundField(object): 
     158class BoundField(StrAndUnicode): 
    159159    "A Field plus data" 
    160160    def __init__(self, form, field, name): 
     
    163163        self.name = name 
    164164 
    165     def __str__(self): 
     165    def __unicode__(self): 
    166166        "Renders this field as an HTML widget." 
    167167        # Use the 'widget' attribute on the field to determine which type 
  • django/trunk/django/newforms/util.py

    r4077 r4163  
    1 # Default encoding for input byte strings. 
    2 DEFAULT_ENCODING = 'utf-8' # TODO: First look at django.conf.settings, then fall back to this. 
     1from django.conf import settings 
    32 
    43def smart_unicode(s): 
     
    65        s = unicode(str(s)) 
    76    elif not isinstance(s, unicode): 
    8         s = unicode(s, DEFAULT_ENCODING
     7        s = unicode(s, settings.DEFAULT_CHARSET
    98    return s 
     9 
     10class 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) 
    1019 
    1120class ErrorDict(dict): 
  • django/trunk/django/newforms/widgets.py

    r4148 r4163  
    99) 
    1010 
    11 from util import smart_unicode 
     11from util import StrAndUnicode, smart_unicode 
    1212from django.utils.html import escape 
    1313from itertools import chain 
     
    147147        return u'\n'.join(output) 
    148148 
    149 class RadioInput(object): 
     149class RadioInput(StrAndUnicode): 
    150150    "An object used by RadioFieldRenderer that represents a single <input type='radio'>." 
    151151    def __init__(self, name, value, attrs, choice, index): 
     
    155155        self.index = index 
    156156 
    157     def __str__(self): 
     157    def __unicode__(self): 
    158158        return u'<label>%s %s</label>' % (self.tag(), self.choice_label) 
    159159 
     
    169169        return u'<input%s />' % flatatt(final_attrs) 
    170170 
    171 class RadioFieldRenderer(object): 
     171class RadioFieldRenderer(StrAndUnicode): 
    172172    "An object used by RadioSelect to enable customization of radio widgets." 
    173173    def __init__(self, name, value, attrs, choices): 
     
    179179            yield RadioInput(self.name, self.value, self.attrs.copy(), choice, i) 
    180180 
    181     def __str__(self): 
     181    def __unicode__(self): 
    182182        "Outputs a <ul> for this set of radio fields." 
    183183        return u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>' % w for w in self])