diff --git a/django/forms/fields.py b/django/forms/fields.py
index b31fe80..0ad5f93 100644
a
|
b
|
class Field(object):
|
71 | 71 | |
72 | 72 | def __init__(self, required=True, widget=None, label=None, initial=None, |
73 | 73 | help_text=None, error_messages=None, show_hidden_initial=False, |
74 | | validators=[]): |
| 74 | validators=[], localize=False): |
75 | 75 | # required -- Boolean that specifies whether the field is required. |
76 | 76 | # True by default. |
77 | 77 | # widget -- A Widget class, or instance of a Widget class, that should |
… |
… |
class Field(object):
|
85 | 85 | # initial -- A value to use in this Field's initial display. This value |
86 | 86 | # is *not* used as a fallback if data isn't given. |
87 | 87 | # help_text -- An optional string to use as "help text" for this Field. |
| 88 | # error_messages -- An optional dictionary to override the default |
| 89 | # messages that the field will raise. |
88 | 90 | # show_hidden_initial -- Boolean that specifies if it is needed to render a |
89 | 91 | # hidden widget with initial value after widget. |
90 | 92 | # validators -- List of addtional validators to use |
| 93 | # localize -- Boolean that specifies if the field should be localized. |
91 | 94 | if label is not None: |
92 | 95 | label = smart_unicode(label) |
93 | 96 | self.required, self.label, self.initial = required, label, initial |
… |
… |
class Field(object):
|
100 | 103 | if isinstance(widget, type): |
101 | 104 | widget = widget() |
102 | 105 | |
| 106 | # Trigger the localization machinery if needed. |
| 107 | if localize: |
| 108 | widget.is_localized = True |
| 109 | self.localize = localize |
| 110 | |
103 | 111 | # Hook into self.widget_attrs() for any Field-specific HTML attributes. |
104 | 112 | extra_attrs = self.widget_attrs(widget) |
105 | 113 | if extra_attrs: |
… |
… |
class IntegerField(Field):
|
213 | 221 | value = super(IntegerField, self).to_python(value) |
214 | 222 | if value in validators.EMPTY_VALUES: |
215 | 223 | return None |
216 | | value = formats.sanitize_separators(value) |
| 224 | if self.localize: |
| 225 | value = formats.sanitize_separators(value) |
217 | 226 | try: |
218 | 227 | value = int(str(value)) |
219 | 228 | except (ValueError, TypeError): |
… |
… |
class FloatField(IntegerField):
|
233 | 242 | value = super(IntegerField, self).to_python(value) |
234 | 243 | if value in validators.EMPTY_VALUES: |
235 | 244 | return None |
236 | | value = formats.sanitize_separators(value) |
| 245 | if self.localize: |
| 246 | value = formats.sanitize_separators(value) |
237 | 247 | try: |
238 | 248 | value = float(value) |
239 | 249 | except (ValueError, TypeError): |
… |
… |
class DecimalField(Field):
|
268 | 278 | """ |
269 | 279 | if value in validators.EMPTY_VALUES: |
270 | 280 | return None |
271 | | value = formats.sanitize_separators(value) |
| 281 | if self.localize: |
| 282 | value = formats.sanitize_separators(value) |
272 | 283 | value = smart_str(value).strip() |
273 | 284 | try: |
274 | 285 | value = Decimal(value) |
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index 8b4112b..99c19d8 100644
a
|
b
|
class Widget(object):
|
134 | 134 | __metaclass__ = MediaDefiningClass |
135 | 135 | is_hidden = False # Determines whether this corresponds to an <input type="hidden">. |
136 | 136 | needs_multipart_form = False # Determines does this widget need multipart-encrypted form |
| 137 | is_localized = False |
137 | 138 | |
138 | 139 | def __init__(self, attrs=None): |
139 | 140 | if attrs is not None: |
… |
… |
class Input(Widget):
|
214 | 215 | final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) |
215 | 216 | if value != '': |
216 | 217 | # Only add the 'value' attribute if a value is non-empty. |
217 | | final_attrs['value'] = force_unicode(formats.localize_input(value)) |
| 218 | if self.is_localized: |
| 219 | value = formats.localize_input(value) |
| 220 | final_attrs['value'] = force_unicode(value) |
218 | 221 | return mark_safe(u'<input%s />' % flatatt(final_attrs)) |
219 | 222 | |
220 | 223 | class TextInput(Input): |
… |
… |
class MultiWidget(Widget):
|
675 | 678 | super(MultiWidget, self).__init__(attrs) |
676 | 679 | |
677 | 680 | def render(self, name, value, attrs=None): |
| 681 | if self.is_localized: |
| 682 | for widget in self.widgets: |
| 683 | widget.is_localized = self.is_localized |
678 | 684 | # value is a list of values, each corresponding to a widget |
679 | 685 | # in self.widgets. |
680 | 686 | if not isinstance(value, list): |
diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
index cc83a88..7a12c7b 100644
a
|
b
|
u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" n
|
1261 | 1261 | >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51)) |
1262 | 1262 | u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" name="date_1" value="12:51:00" />' |
1263 | 1263 | >>> activate('de-at') |
| 1264 | >>> w.is_localized = True |
1264 | 1265 | >>> settings.USE_L10N = True |
1265 | 1266 | >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51)) |
1266 | 1267 | u'<input type="hidden" name="date_0" value="17.09.2007" /><input type="hidden" name="date_1" value="12:51:00" />' |
diff --git a/tests/regressiontests/i18n/forms.py b/tests/regressiontests/i18n/forms.py
index 8ed834c..216d1fe 100644
a
|
b
|
from django.forms.extras import SelectDateWidget
|
3 | 3 | from models import Company |
4 | 4 | |
5 | 5 | class I18nForm(forms.Form): |
6 | | decimal_field = forms.DecimalField() |
7 | | float_field = forms.FloatField() |
8 | | date_field = forms.DateField() |
9 | | datetime_field = forms.DateTimeField() |
10 | | time_field = forms.TimeField() |
11 | | integer_field = forms.IntegerField() |
| 6 | decimal_field = forms.DecimalField(localize=True) |
| 7 | float_field = forms.FloatField(localize=True) |
| 8 | date_field = forms.DateField(localize=True) |
| 9 | datetime_field = forms.DateTimeField(localize=True) |
| 10 | time_field = forms.TimeField(localize=True) |
| 11 | integer_field = forms.IntegerField(localize=True) |
12 | 12 | |
13 | 13 | class SelectDateForm(forms.Form): |
14 | 14 | date_field = forms.DateField(widget=SelectDateWidget) |
15 | 15 | |
16 | 16 | class CompanyForm(forms.ModelForm): |
| 17 | cents_payed = forms.DecimalField(max_digits=4, decimal_places=2, localize=True) |
| 18 | products_delivered = forms.IntegerField(localize=True) |
| 19 | |
17 | 20 | class Meta: |
18 | 21 | model = Company |
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
index 31150a6..9cc1089 100644
a
|
b
|
class FormattingTests(TestCase):
|
410 | 410 | self.assertEqual(localize_input(datetime.datetime(2009, 12, 31, 6, 0, 0)), '31.12.2009 06:00:00') |
411 | 411 | self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added']) |
412 | 412 | settings.USE_THOUSAND_SEPARATOR = True |
413 | | self.assert_(u'12.000' in form6.as_ul()) |
| 413 | # Checking for the localized "products_delivered" field |
| 414 | self.assert_(u'<input type="text" name="products_delivered" value="12.000" id="id_products_delivered" />' in form6.as_ul()) |
414 | 415 | finally: |
415 | 416 | deactivate() |
416 | 417 | |