Ticket #13560: 13560.2.diff

File 13560.2.diff, 10.2 KB (added by russellm, 5 years ago)

Minor cleanup of v1 patch

  • django/contrib/admin/widgets.py

    diff -r 4e639b179fc5 django/contrib/admin/widgets.py
    a b  
    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 -r 4e639b179fc5 django/forms/fields.py
    a b  
    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)
     
    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
     
    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 -r 4e639b179fc5 django/forms/forms.py
    a b  
    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 -r 4e639b179fc5 django/forms/widgets.py
    a b  
    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
     
    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:
     
    208209    """
    209210    input_type = None # Subclasses must define this.
    210211
     212    def _format_value(self, value):
     213        if self.is_localized:
     214            return formats.localize_input(value)
     215        return value
     216
    211217    def render(self, name, value, attrs=None):
    212         if value is None: value = ''
     218        if value is None:
     219            value = ''
    213220        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
    214221        if value != '':
    215222            # Only add the 'value' attribute if a value is non-empty.
    216             final_attrs['value'] = force_unicode(value)
     223            final_attrs['value'] = force_unicode(self._format_value(value))
    217224        return mark_safe(u'<input%s />' % flatatt(final_attrs))
    218225
    219226class TextInput(Input):
     
    295302
    296303class DateInput(Input):
    297304    input_type = 'text'
    298     format = None
     305    format = '%Y-%m-%d'     # '2006-10-25'
    299306
    300307    def __init__(self, attrs=None, format=None):
    301308        super(DateInput, self).__init__(attrs)
     
    303310            self.format = format
    304311
    305312    def _format_value(self, value):
    306         if value is None:
    307             return ''
     313        if self.is_localized:
     314            return formats.localize_input(value)
    308315        elif hasattr(value, 'strftime'):
    309             return formats.localize_input(value, self.format)
     316            value = datetime_safe.new_date(value)
     317            return value.strftime(self.format)
    310318        return value
    311319
    312     def render(self, name, value, attrs=None):
    313         value = self._format_value(value)
    314         return super(DateInput, self).render(name, value, attrs)
    315 
    316320    def _has_changed(self, initial, data):
    317321        # If our field has show_hidden_initial=True, initial will be a string
    318322        # formatted by HiddenInput using formats.localize_input, which is not
     
    326330
    327331class DateTimeInput(Input):
    328332    input_type = 'text'
    329     format = None
     333    format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'
    330334
    331335    def __init__(self, attrs=None, format=None):
    332336        super(DateTimeInput, self).__init__(attrs)
     
    334338            self.format = format
    335339
    336340    def _format_value(self, value):
    337         if value is None:
    338             return ''
     341        if self.is_localized:
     342            return formats.localize_input(value)
    339343        elif hasattr(value, 'strftime'):
    340             return formats.localize_input(value, self.format)
     344            value = datetime_safe.new_datetime(value)
     345            return value.strftime(self.format)
    341346        return value
    342347
    343     def render(self, name, value, attrs=None):
    344         value = self._format_value(value)
    345         return super(DateTimeInput, self).render(name, value, attrs)
    346 
    347348    def _has_changed(self, initial, data):
    348349        # If our field has show_hidden_initial=True, initial will be a string
    349350        # formatted by HiddenInput using formats.localize_input, which is not
     
    357358
    358359class TimeInput(Input):
    359360    input_type = 'text'
    360     format = None
     361    format = '%H:%M:%S'     # '14:30:59'
    361362
    362363    def __init__(self, attrs=None, format=None):
    363364        super(TimeInput, self).__init__(attrs)
     
    365366            self.format = format
    366367
    367368    def _format_value(self, value):
    368         if value is None:
    369             return ''
     369        if self.is_localized:
     370            return formats.localize_input(value)
    370371        elif hasattr(value, 'strftime'):
    371             return formats.localize_input(value, self.format)
     372            return value.strftime(self.format)
    372373        return value
    373374
    374     def render(self, name, value, attrs=None):
    375         value = self._format_value(value)
    376         return super(TimeInput, self).render(name, value, attrs)
    377 
    378375    def _has_changed(self, initial, data):
    379376        # If our field has show_hidden_initial=True, initial will be a string
    380377        # formatted by HiddenInput using formats.localize_input, which is not
     
    674671        super(MultiWidget, self).__init__(attrs)
    675672
    676673    def render(self, name, value, attrs=None):
     674        if self.is_localized:
     675            for widget in self.widgets:
     676                widget.is_localized = self.is_localized
    677677        # value is a list of values, each corresponding to a widget
    678678        # in self.widgets.
    679679        if not isinstance(value, list):
  • tests/regressiontests/admin_widgets/models.py

    diff -r 4e639b179fc5 tests/regressiontests/admin_widgets/models.py
    a b  
    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 -r 4e639b179fc5 tests/regressiontests/forms/widgets.py
    a b  
    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()
     
    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()
     
    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()
     
    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 -r 4e639b179fc5 tests/regressiontests/i18n/forms.py
    a b  
    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