Ticket #4161: 4161.diff

File 4161.diff, 15.5 KB (added by Ivan Sagalaev <Maniac@…>, 17 years ago)

Patch

  • django/db/models/manipulators.py

     
    77from django.utils.functional import curry
    88from django.utils.datastructures import DotExpandedDict
    99from django.utils.text import capfirst
     10from django.utils.encoding import smart_str
    1011import types
    1112
    1213def add_manipulators(sender):
     
    111112        if self.change:
    112113            self.fields_added, self.fields_changed, self.fields_deleted = [], [], []
    113114            for f in self.opts.fields:
    114                 if not f.primary_key and str(getattr(self.original_object, f.attname)) != str(getattr(new_object, f.attname)):
     115                if not f.primary_key and smart_str(getattr(self.original_object, f.attname)) != smart_str(getattr(new_object, f.attname)):
    115116                    self.fields_changed.append(f.verbose_name)
    116117
    117118        # Save many-to-many objects. Example: Set sites for a poll.
     
    211212                                self.fields_added.append('%s "%s"' % (related.opts.verbose_name, new_rel_obj))
    212213                            else:
    213214                                for f in related.opts.fields:
    214                                     if not f.primary_key and f != related.field and str(getattr(old_rel_obj, f.attname)) != str(getattr(new_rel_obj, f.attname)):
     215                                    if not f.primary_key and f != related.field and smart_str(getattr(old_rel_obj, f.attname)) != smart_str(getattr(new_rel_obj, f.attname)):
    215216                                        self.fields_changed.append('%s for %s "%s"' % (f.verbose_name, related.opts.verbose_name, new_rel_obj))
    216217
    217218                        # Save many-to-many objects.
  • django/oldforms/__init__.py

     
    33from django.utils.html import escape
    44from django.conf import settings
    55from django.utils.translation import ugettext, ungettext
     6from django.utils.encoding import smart_unicode, smart_str
    67
    78FORM_FIELD_ID_PREFIX = 'id_'
    89
     
    166167
    167168    def __str__(self):
    168169        "Renders the field"
    169         return str(self.formfield.render(self.data))
     170        return unicode(self).encode('utf-8')
    170171
     172    def __unicode__(self):
     173        "Renders the field"
     174        return self.formfield.render(self.data)
     175
    171176    def __repr__(self):
    172177        return '<FormFieldWrapper for "%s">' % self.formfield.field_name
    173178
     
    196201        self.formfield_dict = formfield_dict
    197202
    198203    def __str__(self):
    199         return str(self.formfield_dict)
     204        return unicode(self).encode('utf-8')
    200205
     206    def __str__(self):
     207        return unicode(self.formfield_dict)
     208
    201209    def __getitem__(self, template_key):
    202210        "Look up field by template key; raise KeyError on failure"
    203211        return self.formfield_dict[template_key]
     
    294302    Subclasses should also implement a render(data) method, which is responsible
    295303    for rending the form field in XHTML.
    296304    """
     305
    297306    def __str__(self):
    298         return self.render('')
     307        return unicode(self).encode('utf-8')
    299308
     309    def __unicode__(self):
     310        return self.render(u'')
     311
    300312    def __repr__(self):
    301313        return 'FormField "%s"' % self.field_name
    302314
     
    398410
    399411    def render(self, data):
    400412        if data is None:
    401             data = ''
    402         maxlength = ''
     413            data = u''
     414        maxlength = u''
    403415        if self.maxlength:
    404             maxlength = 'maxlength="%s" ' % self.maxlength
    405         if isinstance(data, unicode):
    406             data = data.encode(settings.DEFAULT_CHARSET)
    407         return '<input type="%s" id="%s" class="v%s%s" name="%s" size="%s" value="%s" %s/>' % \
    408             (self.input_type, self.get_id(), self.__class__.__name__, self.is_required and ' required' or '',
     416            maxlength = u'maxlength="%s" ' % self.maxlength
     417        return u'<input type="%s" id="%s" class="v%s%s" name="%s" size="%s" value="%s" %s/>' % \
     418            (self.input_type, self.get_id(), self.__class__.__name__, self.is_required and u' required' or '',
    409419            self.field_name, self.length, escape(data), maxlength)
    410420
    411421    def html2python(data):
     
    428438    def render(self, data):
    429439        if data is None:
    430440            data = ''
    431         if isinstance(data, unicode):
    432             data = data.encode(settings.DEFAULT_CHARSET)
    433         return '<textarea id="%s" class="v%s%s" name="%s" rows="%s" cols="%s">%s</textarea>' % \
    434             (self.get_id(), self.__class__.__name__, self.is_required and ' required' or '',
     441        return u'<textarea id="%s" class="v%s%s" name="%s" rows="%s" cols="%s">%s</textarea>' % \
     442            (self.get_id(), self.__class__.__name__, self.is_required and u' required' or u'',
    435443            self.field_name, self.rows, self.cols, escape(data))
    436444
    437445class HiddenField(FormField):
     
    441449        self.validator_list = validator_list[:]
    442450
    443451    def render(self, data):
    444         return '<input type="hidden" id="%s" name="%s" value="%s" />' % \
     452        return u'<input type="hidden" id="%s" name="%s" value="%s" />' % \
    445453            (self.get_id(), self.field_name, escape(data))
    446454
    447455class CheckboxField(FormField):
     
    456464        checked_html = ''
    457465        if data or (data is '' and self.checked_by_default):
    458466            checked_html = ' checked="checked"'
    459         return '<input type="checkbox" id="%s" class="v%s" name="%s"%s />' % \
     467        return u'<input type="checkbox" id="%s" class="v%s" name="%s"%s />' % \
    460468            (self.get_id(), self.__class__.__name__,
    461469            self.field_name, checked_html)
    462470
     
    471479    def __init__(self, field_name, choices=None, size=1, is_required=False, validator_list=None, member_name=None):
    472480        if validator_list is None: validator_list = []
    473481        if choices is None: choices = []
     482        choices = [(k, smart_unicode(v)) for k, v in choices]
    474483        self.field_name = field_name
    475484        # choices is a list of (value, human-readable key) tuples because order matters
    476485        self.choices, self.size, self.is_required = choices, size, is_required
     
    479488            self.member_name = member_name
    480489
    481490    def render(self, data):
    482         output = ['<select id="%s" class="v%s%s" name="%s" size="%s">' % \
     491        output = [u'<select id="%s" class="v%s%s" name="%s" size="%s">' % \
    483492            (self.get_id(), self.__class__.__name__,
    484              self.is_required and ' required' or '', self.field_name, self.size)]
    485         str_data = str(data) # normalize to string
     493             self.is_required and u' required' or u'', self.field_name, self.size)]
     494        str_data = smart_unicode(data) # normalize to string
    486495        for value, display_name in self.choices:
    487             selected_html = ''
    488             if str(value) == str_data:
    489                 selected_html = ' selected="selected"'
    490             output.append('    <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(display_name)))
    491         output.append('  </select>')
    492         return '\n'.join(output)
     496            selected_html = u''
     497            if smart_unicode(value) == str_data:
     498                selected_html = u' selected="selected"'
     499            output.append(u'    <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(display_name)))
     500        output.append(u'  </select>')
     501        return u'\n'.join(output)
    493502
    494503    def isValidChoice(self, data, form):
    495         str_data = str(data)
    496         str_choices = [str(item[0]) for item in self.choices]
     504        str_data = smart_unicode(data)
     505        str_choices = [smart_str(item[0]) for item in self.choices]
    497506        if str_data not in str_choices:
    498507            raise validators.ValidationError, ugettext("Select a valid choice; '%(data)s' is not in %(choices)s.") % {'data': str_data, 'choices': str_choices}
    499508
     
    509518    def __init__(self, field_name, choices=None, ul_class='', is_required=False, validator_list=None, member_name=None):
    510519        if validator_list is None: validator_list = []
    511520        if choices is None: choices = []
     521        choices = [(k, smart_unicode(v)) for k, v in choices]
    512522        self.field_name = field_name
    513523        # choices is a list of (value, human-readable key) tuples because order matters
    514524        self.choices, self.is_required = choices, is_required
     
    520530    def render(self, data):
    521531        """
    522532        Returns a special object, RadioFieldRenderer, that is iterable *and*
    523         has a default str() rendered output.
     533        has a default unicode() rendered output.
    524534
    525535        This allows for flexible use in templates. You can just use the default
    526536        rendering:
     
    537547        class RadioFieldRenderer:
    538548            def __init__(self, datalist, ul_class):
    539549                self.datalist, self.ul_class = datalist, ul_class
    540             def __str__(self):
    541                 "Default str() output for this radio field -- a <ul>"
    542                 output = ['<ul%s>' % (self.ul_class and ' class="%s"' % self.ul_class or '')]
    543                 output.extend(['<li>%s %s</li>' % (d['field'], d['label']) for d in self.datalist])
    544                 output.append('</ul>')
    545                 return ''.join(output)
     550            def __unicode__(self):
     551                "Default unicode() output for this radio field -- a <ul>"
     552                output = [u'<ul%s>' % (self.ul_class and u' class="%s"' % self.ul_class or u'')]
     553                output.extend([u'<li>%s %s</li>' % (d['field'], d['label']) for d in self.datalist])
     554                output.append(u'</ul>')
     555                return u''.join(output)
    546556            def __iter__(self):
    547557                for d in self.datalist:
    548558                    yield d
    549559            def __len__(self):
    550560                return len(self.datalist)
    551561        datalist = []
    552         str_data = str(data) # normalize to string
     562        str_data = smart_unicode(data) # normalize to string
    553563        for i, (value, display_name) in enumerate(self.choices):
    554564            selected_html = ''
    555             if str(value) == str_data:
    556                 selected_html = ' checked="checked"'
     565            if smart_unicode(value) == str_data:
     566                selected_html = u' checked="checked"'
    557567            datalist.append({
    558568                'value': value,
    559569                'name': display_name,
    560                 'field': '<input type="radio" id="%s" name="%s" value="%s"%s/>' % \
    561                     (self.get_id() + '_' + str(i), self.field_name, value, selected_html),
    562                 'label': '<label for="%s">%s</label>' % \
    563                     (self.get_id() + '_' + str(i), display_name),
     570                'field': u'<input type="radio" id="%s" name="%s" value="%s"%s/>' % \
     571                    (self.get_id() + u'_' + unicode(i), self.field_name, value, selected_html),
     572                'label': u'<label for="%s">%s</label>' % \
     573                    (self.get_id() + u'_' + unicode(i), display_name),
    564574            })
    565575        return RadioFieldRenderer(datalist, self.ul_class)
    566576
    567577    def isValidChoice(self, data, form):
    568         str_data = str(data)
    569         str_choices = [str(item[0]) for item in self.choices]
     578        str_data = smart_unicode(data)
     579        str_choices = [smart_unicode(item[0]) for item in self.choices]
    570580        if str_data not in str_choices:
    571581            raise validators.ValidationError, ugettext("Select a valid choice; '%(data)s' is not in %(choices)s.") % {'data':str_data, 'choices':str_choices}
    572582
     
    590600class SelectMultipleField(SelectField):
    591601    requires_data_list = True
    592602    def render(self, data):
    593         output = ['<select id="%s" class="v%s%s" name="%s" size="%s" multiple="multiple">' % \
    594             (self.get_id(), self.__class__.__name__, self.is_required and ' required' or '',
     603        output = [u'<select id="%s" class="v%s%s" name="%s" size="%s" multiple="multiple">' % \
     604            (self.get_id(), self.__class__.__name__, self.is_required and u' required' or u'',
    595605            self.field_name, self.size)]
    596         str_data_list = map(str, data) # normalize to strings
     606        str_data_list = map(smart_unicode, data) # normalize to strings
    597607        for value, choice in self.choices:
    598             selected_html = ''
    599             if str(value) in str_data_list:
    600                 selected_html = ' selected="selected"'
    601             output.append('    <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(choice)))
    602         output.append('  </select>')
    603         return '\n'.join(output)
     608            selected_html = u''
     609            if smart_unicode(value) in str_data_list:
     610                selected_html = u' selected="selected"'
     611            output.append(u'    <option value="%s"%s>%s</option>' % (escape(value), selected_html, escape(choice)))
     612        output.append(u'  </select>')
     613        return u'\n'.join(output)
    604614
    605615    def isValidChoice(self, field_data, all_data):
    606616        # data is something like ['1', '2', '3']
    607         str_choices = [str(item[0]) for item in self.choices]
    608         for val in map(str, field_data):
     617        str_choices = [smart_unicode(item[0]) for item in self.choices]
     618        for val in map(smart_unicode, field_data):
    609619            if val not in str_choices:
    610620                raise validators.ValidationError, ugettext("Select a valid choice; '%(data)s' is not in %(choices)s.") % {'data':val, 'choices':str_choices}
    611621
     
    642652        new_data.setlist(self.field_name, data_list)
    643653
    644654    def render(self, data):
    645         output = ['<ul%s>' % (self.ul_class and ' class="%s"' % self.ul_class or '')]
    646         str_data_list = map(str, data) # normalize to strings
     655        output = [u'<ul%s>' % (self.ul_class and u' class="%s"' % self.ul_class or u'')]
     656        str_data_list = map(smart_unicode, data) # normalize to strings
    647657        for value, choice in self.choices:
    648             checked_html = ''
    649             if str(value) in str_data_list:
    650                 checked_html = ' checked="checked"'
    651             field_name = '%s%s' % (self.field_name, value)
    652             output.append('<li><input type="checkbox" id="%s" class="v%s" name="%s"%s value="on" /> <label for="%s">%s</label></li>' % \
     658            checked_html = u''
     659            if smart_unicode(value) in str_data_list:
     660                checked_html = u' checked="checked"'
     661            field_name = u'%s%s' % (self.field_name, value)
     662            output.append(u'<li><input type="checkbox" id="%s" class="v%s" name="%s"%s value="on" /> <label for="%s">%s</label></li>' % \
    653663                (self.get_id() + escape(value), self.__class__.__name__, field_name, checked_html,
    654664                self.get_id() + escape(value), choice))
    655         output.append('</ul>')
    656         return '\n'.join(output)
     665        output.append(u'</ul>')
     666        return u'\n'.join(output)
    657667
    658668####################
    659669# FILE UPLOADS     #
     
    674684            raise validators.CriticalValidationError, ugettext("The submitted file is empty.")
    675685
    676686    def render(self, data):
    677         return '<input type="file" id="%s" class="v%s" name="%s" />' % \
     687        return u'<input type="file" id="%s" class="v%s" name="%s" />' % \
    678688            (self.get_id(), self.__class__.__name__, self.field_name)
    679689
    680690    def html2python(data):
     
    985995
    986996    def render(self, data):
    987997        if data is None:
    988             data = ''
     998            data = u''
    989999        elif isinstance(data, (list, tuple)):
    990             data = ','.join(data)
     1000            data = u','.join(data)
    9911001        return super(CommaSeparatedIntegerField, self).render(data)
    9921002
    9931003class RawIdAdminField(CommaSeparatedIntegerField):
Back to Top