Ticket #13032: 13032.1.diff

File 13032.1.diff, 7.2 KB (added by Jannis Leidel, 15 years ago)

Added localize parameter to form fields to be able to selectively enable localization

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index b31fe80..0ad5f93 100644
    a b class Field(object):  
    7171
    7272    def __init__(self, required=True, widget=None, label=None, initial=None,
    7373                 help_text=None, error_messages=None, show_hidden_initial=False,
    74                  validators=[]):
     74                 validators=[], localize=False):
    7575        # required -- Boolean that specifies whether the field is required.
    7676        #             True by default.
    7777        # widget -- A Widget class, or instance of a Widget class, that should
    class Field(object):  
    8585        # initial -- A value to use in this Field's initial display. This value
    8686        #            is *not* used as a fallback if data isn't given.
    8787        # 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.
    8890        # show_hidden_initial -- Boolean that specifies if it is needed to render a
    8991        #                        hidden widget with initial value after widget.
    9092        # validators -- List of addtional validators to use
     93        # localize -- Boolean that specifies if the field should be localized.
    9194        if label is not None:
    9295            label = smart_unicode(label)
    9396        self.required, self.label, self.initial = required, label, initial
    class Field(object):  
    100103        if isinstance(widget, type):
    101104            widget = widget()
    102105
     106        # Trigger the localization machinery if needed.
     107        if localize:
     108            widget.is_localized = True
     109        self.localize = localize
     110
    103111        # Hook into self.widget_attrs() for any Field-specific HTML attributes.
    104112        extra_attrs = self.widget_attrs(widget)
    105113        if extra_attrs:
    class IntegerField(Field):  
    213221        value = super(IntegerField, self).to_python(value)
    214222        if value in validators.EMPTY_VALUES:
    215223            return None
    216         value = formats.sanitize_separators(value)
     224        if self.localize:
     225            value = formats.sanitize_separators(value)
    217226        try:
    218227            value = int(str(value))
    219228        except (ValueError, TypeError):
    class FloatField(IntegerField):  
    233242        value = super(IntegerField, self).to_python(value)
    234243        if value in validators.EMPTY_VALUES:
    235244            return None
    236         value = formats.sanitize_separators(value)
     245        if self.localize:
     246            value = formats.sanitize_separators(value)
    237247        try:
    238248            value = float(value)
    239249        except (ValueError, TypeError):
    class DecimalField(Field):  
    268278        """
    269279        if value in validators.EMPTY_VALUES:
    270280            return None
    271         value = formats.sanitize_separators(value)
     281        if self.localize:
     282            value = formats.sanitize_separators(value)
    272283        value = smart_str(value).strip()
    273284        try:
    274285            value = Decimal(value)
  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 8b4112b..99c19d8 100644
    a b class Widget(object):  
    134134    __metaclass__ = MediaDefiningClass
    135135    is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
    136136    needs_multipart_form = False # Determines does this widget need multipart-encrypted form
     137    is_localized = False
    137138
    138139    def __init__(self, attrs=None):
    139140        if attrs is not None:
    class Input(Widget):  
    214215        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
    215216        if value != '':
    216217            # 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)
    218221        return mark_safe(u'<input%s />' % flatatt(final_attrs))
    219222
    220223class TextInput(Input):
    class MultiWidget(Widget):  
    675678        super(MultiWidget, self).__init__(attrs)
    676679
    677680    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
    678684        # value is a list of values, each corresponding to a widget
    679685        # in self.widgets.
    680686        if not isinstance(value, list):
  • tests/regressiontests/forms/widgets.py

    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  
    12611261>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    12621262u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" name="date_1" value="12:51:00" />'
    12631263>>> activate('de-at')
     1264>>> w.is_localized = True
    12641265>>> settings.USE_L10N = True
    12651266>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    12661267u'<input type="hidden" name="date_0" value="17.09.2007" /><input type="hidden" name="date_1" value="12:51:00" />'
  • tests/regressiontests/i18n/forms.py

    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  
    33from models import Company
    44
    55class 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)
    1212
    1313class SelectDateForm(forms.Form):
    1414    date_field = forms.DateField(widget=SelectDateWidget)
    1515
    1616class CompanyForm(forms.ModelForm):
     17    cents_payed = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)
     18    products_delivered = forms.IntegerField(localize=True)
     19
    1720    class Meta:
    1821        model = Company
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 31150a6..9cc1089 100644
    a b class FormattingTests(TestCase):  
    410410            self.assertEqual(localize_input(datetime.datetime(2009, 12, 31, 6, 0, 0)), '31.12.2009 06:00:00')
    411411            self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added'])
    412412            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())
    414415        finally:
    415416            deactivate()
    416417
Back to Top