diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-05-14 16:06:27 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-05-14 16:06:27 +0000 |
| commit | fbfefbf83d1396db1ea4095b2b64e1841689b364 (patch) | |
| tree | d2eb31ada06c261b5dec94ab20fc3a487ba5815a | |
| parent | 3916cfbaff3b0b41d977a5efb6a9d9b41b067d90 (diff) | |
unicode: Changed a few more places in newforms where str() was being used with
potential non-ASCII arguments. Refs #3406 (and added a test for the latter).
git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5236 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/newforms/fields.py | 2 | ||||
| -rw-r--r-- | django/newforms/forms.py | 6 | ||||
| -rw-r--r-- | django/newforms/models.py | 3 | ||||
| -rw-r--r-- | tests/regressiontests/forms/regressions.py | 10 |
4 files changed, 16 insertions, 5 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py index b9a210db8c..970a5185da 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -359,7 +359,7 @@ class ChoiceField(Field): value = smart_unicode(value) if value == u'': return value - valid_values = set([str(k) for k, v in self.choices]) + valid_values = set([smart_unicode(k) for k, v in self.choices]) if value not in valid_values: raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.')) return value diff --git a/django/newforms/forms.py b/django/newforms/forms.py index edd1992cf4..2230057c1d 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -4,7 +4,7 @@ Form classes from django.utils.datastructures import SortedDict, MultiValueDict from django.utils.html import escape -from django.utils.encoding import StrAndUnicode +from django.utils.encoding import StrAndUnicode, smart_unicode from fields import Field from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput from util import flatatt, ErrorDict, ErrorList, ValidationError @@ -311,8 +311,8 @@ class BoundField(StrAndUnicode): associated Form has specified auto_id. Returns an empty string otherwise. """ auto_id = self.form.auto_id - if auto_id and '%s' in str(auto_id): - return str(auto_id) % self.html_name + if auto_id and '%s' in smart_unicode(auto_id): + return smart_unicode(auto_id) % self.html_name elif auto_id: return self.html_name return '' diff --git a/django/newforms/models.py b/django/newforms/models.py index cee6ab1c74..c89cb9cadd 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -4,6 +4,7 @@ and database field objects. """ from django.utils.translation import ugettext +from django.utils.encoding import smart_unicode from util import ValidationError from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList from fields import Field, ChoiceField @@ -120,7 +121,7 @@ class QuerySetIterator(object): if self.empty_label is not None: yield (u"", self.empty_label) for obj in self.queryset: - yield (obj._get_pk_val(), str(obj)) + yield (obj._get_pk_val(), smart_unicode(obj)) # Clear the QuerySet cache if required. if not self.cache_choices: self.queryset._result_cache = None diff --git a/tests/regressiontests/forms/regressions.py b/tests/regressiontests/forms/regressions.py index c5a94db6f9..caa0f68741 100644 --- a/tests/regressiontests/forms/regressions.py +++ b/tests/regressiontests/forms/regressions.py @@ -36,4 +36,14 @@ Unicode decoding problems... >>> f = SomeForm() >>> f.as_p() u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>' + +Testing choice validation with UTF-8 bytestrings as input (these are the +Russian abbreviations "мес." and "шт.". + +>>> UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\xd1\x88\xd1\x82.')) +>>> f = ChoiceField(choices=UNITS) +>>> f.clean(u'\u0448\u0442.') +u'\u0448\u0442.' +>>> f.clean('\xd1\x88\xd1\x82.') +u'\u0448\u0442.' """ |
