Ticket #13560: 13560.1.diff

File 13560.1.diff, 9.6 KB (added by jezdez, 5 years ago)

Updated form localization to also happen in the widgets for render time. Also fixes a bunch of bugs introduced in r12029.

  • django/contrib/admin/widgets.py

    diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py
    index d10c9c1..1d321d0 100644
    a b class FilteredSelectMultiple(forms.SelectMultiple): 
    4444            (name, self.verbose_name.replace('"', '\\"'), int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX))
    4545        return mark_safe(u''.join(output))
    4646
    47 class AdminDateWidget(forms.DateTimeInput):
     47class AdminDateWidget(forms.DateInput):
    4848    class Media:
    4949        js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js",
    5050              settings.ADMIN_MEDIA_PREFIX + "js/admin/DateTimeShortcuts.js")
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 14152c8..0bae4ba 100644
    a b class Field(object): 
    105105
    106106        # Trigger the localization machinery if needed.
    107107        self.localize = localize
     108        if self.localize:
     109            widget.is_localized = True
    108110
    109111        # Hook into self.widget_attrs() for any Field-specific HTML attributes.
    110112        extra_attrs = self.widget_attrs(widget)
    class Field(object): 
    125127
    126128        self.validators = self.default_validators + validators
    127129
    128     def localize_value(self, value):
    129         return formats.localize_input(value)
    130 
    131130    def to_python(self, value):
    132131        return value
    133132
    class SplitDateTimeField(MultiValueField): 
    843842        errors = self.default_error_messages.copy()
    844843        if 'error_messages' in kwargs:
    845844            errors.update(kwargs['error_messages'])
     845        localize = kwargs.get('localize', False)
    846846        fields = (
    847             DateField(input_formats=input_date_formats, error_messages={'invalid': errors['invalid_date']}),
    848             TimeField(input_formats=input_time_formats, error_messages={'invalid': errors['invalid_time']}),
     847            DateField(input_formats=input_date_formats,
     848                      error_messages={'invalid': errors['invalid_date']},
     849                      localize=localize),
     850            TimeField(input_formats=input_time_formats,
     851                      error_messages={'invalid': errors['invalid_time']},
     852                      localize=localize),
    849853        )
    850854        super(SplitDateTimeField, self).__init__(fields, *args, **kwargs)
    851855
  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index 6076065..b3718ef 100644
    a b class BoundField(StrAndUnicode): 
    443443            name = self.html_name
    444444        else:
    445445            name = self.html_initial_name
    446         if self.field.localize:
    447             data = self.field.localize_value(data)
    448446        return widget.render(name, data, attrs=attrs)
    449447
    450448    def as_text(self, attrs=None, **kwargs):
  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 73d0f89..a9b52aa 100644
    a b from django.utils.html import escape, conditional_escape 
    1010from django.utils.translation import ugettext
    1111from django.utils.encoding import StrAndUnicode, force_unicode
    1212from django.utils.safestring import mark_safe
    13 from django.utils import formats
     13from django.utils import datetime_safe, formats
    1414import time
    1515import datetime
    1616from util import flatatt
    class Widget(object): 
    133133    __metaclass__ = MediaDefiningClass
    134134    is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
    135135    needs_multipart_form = False # Determines does this widget need multipart-encrypted form
     136    is_localized = False
    136137
    137138    def __init__(self, attrs=None):
    138139        if attrs is not None:
    class Input(Widget): 
    209210    input_type = None # Subclasses must define this.
    210211
    211212    def render(self, name, value, attrs=None):
    212         if value is None: value = ''
     213        if value is None:
     214            value = ''
    213215        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
    214216        if value != '':
    215217            # Only add the 'value' attribute if a value is non-empty.
     218            if self.is_localized:
     219                value = formats.localize_input(value)
    216220            final_attrs['value'] = force_unicode(value)
    217221        return mark_safe(u'<input%s />' % flatatt(final_attrs))
    218222
    class Textarea(Widget): 
    295299
    296300class DateInput(Input):
    297301    input_type = 'text'
    298     format = None
     302    format = '%Y-%m-%d'     # '2006-10-25'
    299303
    300304    def __init__(self, attrs=None, format=None):
    301305        super(DateInput, self).__init__(attrs)
    class DateInput(Input): 
    305309    def _format_value(self, value):
    306310        if value is None:
    307311            return ''
     312        elif self.is_localized:
     313            return value
    308314        elif hasattr(value, 'strftime'):
    309             return formats.localize_input(value, self.format)
     315            value = datetime_safe.new_date(value)
     316            return value.strftime(self.format)
    310317        return value
    311318
    312319    def render(self, name, value, attrs=None):
    class DateInput(Input): 
    326333
    327334class DateTimeInput(Input):
    328335    input_type = 'text'
    329     format = None
     336    format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'
    330337
    331338    def __init__(self, attrs=None, format=None):
    332339        super(DateTimeInput, self).__init__(attrs)
    class DateTimeInput(Input): 
    336343    def _format_value(self, value):
    337344        if value is None:
    338345            return ''
     346        elif self.is_localized:
     347            return value
    339348        elif hasattr(value, 'strftime'):
    340             return formats.localize_input(value, self.format)
     349            value = datetime_safe.new_datetime(value)
     350            return value.strftime(self.format)
    341351        return value
    342352
    343353    def render(self, name, value, attrs=None):
    class DateTimeInput(Input): 
    357367
    358368class TimeInput(Input):
    359369    input_type = 'text'
    360     format = None
     370    format = '%H:%M:%S'     # '14:30:59'
    361371
    362372    def __init__(self, attrs=None, format=None):
    363373        super(TimeInput, self).__init__(attrs)
    class TimeInput(Input): 
    367377    def _format_value(self, value):
    368378        if value is None:
    369379            return ''
     380        elif self.is_localized:
     381            return value
    370382        elif hasattr(value, 'strftime'):
    371             return formats.localize_input(value, self.format)
     383            return value.strftime(self.format)
    372384        return value
    373385
    374386    def render(self, name, value, attrs=None):
    class MultiWidget(Widget): 
    674686        super(MultiWidget, self).__init__(attrs)
    675687
    676688    def render(self, name, value, attrs=None):
     689        if self.is_localized:
     690            for widget in self.widgets:
     691                widget.is_localized = self.is_localized
    677692        # value is a list of values, each corresponding to a widget
    678693        # in self.widgets.
    679694        if not isinstance(value, list):
  • tests/regressiontests/admin_widgets/models.py

    diff --git a/tests/regressiontests/admin_widgets/models.py b/tests/regressiontests/admin_widgets/models.py
    index 5632548..59d625b 100644
    a b HTML escaped. 
    105105<p class="datetime">Date: <input value="2007-12-01" type="text" class="vDateField" name="test_0" size="10" /><br />Time: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
    106106>>> activate('de-at')
    107107>>> settings.USE_L10N = True
     108>>> w.is_localized = True
    108109>>> print conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30)))
    109110<p class="datetime">Datum: <input value="01.12.2007" type="text" class="vDateField" name="test_0" size="10" /><br />Zeit: <input value="09:30:00" type="text" class="vTimeField" name="test_1" size="8" /></p>
    110111>>> deactivate()
  • tests/regressiontests/forms/widgets.py

    diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
    index 0b2cceb..39d7d56 100644
    a b u'<input type="text" name="date" value="2007-09-17 12:51:34" />' 
    11351135u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
    11361136>>> activate('de-at')
    11371137>>> settings.USE_L10N = True
     1138>>> w.is_localized = True
    11381139>>> w.render('date', d)
    11391140u'<input type="text" name="date" value="17.09.2007 12:51:34" />'
    11401141>>> deactivate()
    u'<input type="text" name="date" value="2007-09-17" />' 
    11761177
    11771178>>> activate('de-at')
    11781179>>> settings.USE_L10N = True
     1180>>> w.is_localized = True
    11791181>>> w.render('date', d)
    11801182u'<input type="text" name="date" value="17.09.2007" />'
    11811183>>> deactivate()
    u'<input type="text" name="time" value="13:12:11" />' 
    12201222
    12211223>>> activate('de-at')
    12221224>>> settings.USE_L10N = True
     1225>>> w.is_localized = True
    12231226>>> w.render('date', d)
    12241227u'<input type="text" name="date" value="17.09.2007" />'
    12251228>>> deactivate()
    u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" n 
    12591262u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" name="date_1" value="12:51:00" />'
    12601263>>> activate('de-at')
    12611264>>> settings.USE_L10N = True
     1265>>> w.is_localized = True
    12621266>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    12631267u'<input type="hidden" name="date_0" value="17.09.2007" /><input type="hidden" name="date_1" value="12:51:00" />'
    12641268>>> deactivate()
  • tests/regressiontests/i18n/forms.py

    diff --git a/tests/regressiontests/i18n/forms.py b/tests/regressiontests/i18n/forms.py
    index 216d1fe..156441c 100644
    a b class SelectDateForm(forms.Form): 
    1616class CompanyForm(forms.ModelForm):
    1717    cents_payed = forms.DecimalField(max_digits=4, decimal_places=2, localize=True)
    1818    products_delivered = forms.IntegerField(localize=True)
     19    date_added = forms.DateTimeField(localize=True)
    1920
    2021    class Meta:
    2122        model = Company
Back to Top