Ticket #5481: choicefield_correct_type.3.diff

File choicefield_correct_type.3.diff, 4.6 KB (added by rcoup, 8 years ago)

Forces strings to unicode

  • django/newforms/fields.py

     
    479479        # choices can be any iterable, but we call list() on it because
    480480        # it will be consumed more than once.
    481481        self._choices = self.widget.choices = list(value)
     482        # Build an index mapping the unicode value of each choice key to the
     483        # original key. Strings are forced to unicode as per django policy,
     484        # but other types are left alone.
     485        self._choices_idx = dict([(smart_unicode(k),(isinstance(k, str) and unicode(k) or k)) for k, v in self._choices])
    482486
    483487    choices = property(_get_choices, _set_choices)
    484488
     
    492496        value = smart_unicode(value)
    493497        if value == u'':
    494498            return value
    495         valid_values = set([smart_unicode(k) for k, v in self.choices])
    496         if value not in valid_values:
     499       
     500        if value not in self._choices_idx:
    497501            raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
    498         return value
     502        return self._choices_idx[value]
    499503
    500504class MultipleChoiceField(ChoiceField):
    501505    hidden_widget = MultipleHiddenInput
     
    511515            return []
    512516        if not isinstance(value, (list, tuple)):
    513517            raise ValidationError(ugettext(u'Enter a list of values.'))
    514         new_value = [smart_unicode(val) for val in value]
    515518        # Validate that each value in the value list is in self.choices.
    516         valid_values = set([smart_unicode(k) for k, v in self.choices])
    517         for val in new_value:
    518             if val not in valid_values:
    519                 raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
    520         return new_value
     519        try:
     520            return [self._choices_idx[smart_unicode(val)] for val in value]
     521        except KeyError:
     522            raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
    521523
    522524class ComboField(Field):
    523525    """
  • tests/regressiontests/forms/tests.py

     
    17951795...
    17961796ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
    17971797
     1798>>> class TestX(object):
     1799...     def __unicode__(self):
     1800...         return u'TestX-U'
     1801...     def __str__(self):
     1802...         return 'TestX-S'
     1803>>> f = ChoiceField(choices=[(1, '1'), (True, 'Good'), (TestX(), 'X')])
     1804>>> f.clean('1')
     18051
     1806>>> f.clean('True')
     1807True
     1808>>> type(f.clean('TestX-U'))
     1809<class '__main__.TestX'>
     1810>>> f.clean('TestX-S')
     1811Traceback (most recent call last):
     1812...
     1813ValidationError: [u'Select a valid choice. That choice is not one of the available choices.']
     1814>>> f.clean(True)
     1815True
     1816>>> f.clean(1)
     18171
     1818
    17981819# NullBooleanField ############################################################
    17991820
    18001821>>> f = NullBooleanField()
     
    18751896...
    18761897ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
    18771898
     1899>>> class TestX(object):
     1900...     def __unicode__(self):
     1901...         return u'TestX-U'
     1902...     def __str__(self):
     1903...         return 'TestX-S'
     1904>>> f = MultipleChoiceField(choices=[(1, '1'), (True, 'Good'), (TestX(), 'X')])
     1905>>> f.clean(['1'])
     1906[1]
     1907>>> f.clean(['True'])
     1908[True]
     1909>>> type(f.clean(['TestX-U'])[0])
     1910<class '__main__.TestX'>
     1911>>> f.clean(['TestX-S'])
     1912Traceback (most recent call last):
     1913...
     1914ValidationError: [u'Select a valid choice. TestX-S is not one of the available choices.']
     1915>>> f.clean(['True', '1'])
     1916[True, 1]
     1917>>> f.clean([True, 1])
     1918[True, 1]
     1919
    18781920# ComboField ##################################################################
    18791921
    18801922ComboField takes a list of fields that should be used to validate a value,
  • tests/regressiontests/forms/regressions.py

     
    5353>>> f.clean('\xd1\x88\xd1\x82.')
    5454u'\u0448\u0442.'
    5555
     56>>> UNITS = ((u'\u043c\u0435\u0441.', u'\u043c\u0435\u0441.'), (u'\u0448\u0442.', u'\u0448\u0442.'))
     57>>> f = ChoiceField(choices=UNITS)
     58>>> f.clean(u'\u0448\u0442.')
     59u'\u0448\u0442.'
     60>>> f.clean('\xd1\x88\xd1\x82.')
     61u'\u0448\u0442.'
     62
    5663Translated error messages used to be buggy.
    5764>>> activate('ru')
    5865>>> f = SomeForm({})
Back to Top