Ticket #12858: 12698-backport.diff

File 12698-backport.diff, 7.1 KB (added by jkocherhans, 7 years ago)

I don't think this is the correct way to backport this, but the real way might be too hard to be worth it.

  • django/forms/widgets.py

    Property changes on: .
    ___________________________________________________________________
    Modified: svnmerge-integrated
       - /django/trunk:1-11500,11523,11527-11528,11531-11552,11554,11577,11579-11581,11588-11589,11591-11592,11596-11599,11601-11617,11619-11626,11628-11635,11637-11638,11643-11644,11648-11653,11656,11670,11678,11681,11684,11686,11688,11691,11693,11695,11697,11699,11701,11703,11705,11707,11714,11719,11732,11734,11740,11748,11751,11753,11756,11760,11800,11802,11808,11815,11817,11820,11822,11824,11826,11828,11831,11833,11835,11837,11839,11841,11844,11857,11864,11874,11876,11878,11885,11898,11901,11905,11909,11912,11914,11917,11938,11953,11961,11977,11979,11984,11986,11988,11990,11992,11994,11996,11998,12001,12004,12006,12011,12022,12024,12044-12045,12048,12054-12056,12059,12064,12066,12068,12070,12079,12086,12088,12104,12118,12132,12137-12138,12140-12141,12144,12150-12152,12220-12221,12229,12249,12253,12276,12282,12284,12293,12313,12317-12324,12333,12341,12343,12346,12353,12362,12379,12384,12398,12405,12408-12411,12419-12420,12423,12425-12426,12429,12434,12436,12439-12442,12448,12457,12461-12464,12467,12473,12475,12490,12492,12497-12498,12502,12513,12515-12516,12518,12523,12526,12528,12533,12535,12537,12539,12541,12548,12553,12556,12558-12560,12562,12567,12569-12570,12573,12576,12579,12581,12584,12616,12621-12622,12631,12648,12650,12652,12659,12661,12676,12679,12681,12683,12688,12696
       + /django/trunk:1-11500,11523,11527-11528,11531-11552,11554,11577,11579-11581,11588-11589,11591-11592,11596-11599,11601-11617,11619-11626,11628-11635,11637-11638,11643-11644,11648-11653,11656,11670,11678,11681,11684,11686,11688,11691,11693,11695,11697,11699,11701,11703,11705,11707,11714,11719,11732,11734,11740,11748,11751,11753,11756,11760,11800,11802,11808,11815,11817,11820,11822,11824,11826,11828,11831,11833,11835,11837,11839,11841,11844,11857,11864,11874,11876,11878,11885,11898,11901,11905,11909,11912,11914,11917,11938,11953,11961,11977,11979,11984,11986,11988,11990,11992,11994,11996,11998,12001,12004,12006,12011,12022,12024,12044-12045,12048,12054-12056,12059,12064,12066,12068,12070,12079,12086,12088,12104,12118,12132,12137-12138,12140-12141,12144,12150-12152,12220-12221,12229,12249,12253,12276,12282,12284,12293,12313,12317-12324,12333,12341,12343,12346,12353,12362,12379,12384,12398,12405,12408-12411,12419-12420,12423,12425-12426,12429,12434,12436,12439-12442,12448,12457,12461-12464,12467,12473,12475,12490,12492,12497-12498,12502,12513,12515-12516,12518,12523,12526,12528,12533,12535,12537,12539,12541,12548,12553,12556,12558-12560,12562,12567,12569-12570,12573,12576,12579,12581,12584,12616,12621-12622,12631,12648,12650,12652,12659,12661,12676,12679,12681,12683,12688,12696,12698
    
     
    1616from django.utils.encoding import StrAndUnicode, force_unicode
    1717from django.utils.safestring import mark_safe
    1818from django.utils import datetime_safe
    19 from datetime import time
     19import time
     20import datetime
    2021from util import flatatt
    2122from urlparse import urljoin
    2223
     
    319320        return super(DateInput, self).render(name, value, attrs)
    320321
    321322    def _has_changed(self, initial, data):
     323        # If our field has show_hidden_initial=True, initial will be a string
     324        # formatted by HiddenInput using formats.localize_input, which is not
     325        # necessarily the format used for this widget. Attempt to convert it.
     326        try:
     327            input_format = '%Y-%m-%d'
     328            initial = datetime.date(*time.strptime(initial, '%Y-%m-%d')[:3])
     329        except (TypeError, ValueError):
     330            pass
    322331        return super(DateInput, self)._has_changed(self._format_value(initial), data)
    323332
    324333class DateTimeInput(Input):
     
    343352        return super(DateTimeInput, self).render(name, value, attrs)
    344353
    345354    def _has_changed(self, initial, data):
     355        # If our field has show_hidden_initial=True, initial will be a string
     356        # formatted by HiddenInput using formats.localize_input, which is not
     357        # necessarily the format used for this widget. Attempt to convert it.
     358        try:
     359            input_format = '%Y-%m-%d %H:%M:%S'
     360            initial = datetime.datetime(*time.strptime(initial, input_format)[:6])
     361        except (TypeError, ValueError):
     362            pass
    346363        return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)
    347364
    348365class TimeInput(Input):
     
    366383        return super(TimeInput, self).render(name, value, attrs)
    367384
    368385    def _has_changed(self, initial, data):
     386        # If our field has show_hidden_initial=True, initial will be a string
     387        # formatted by HiddenInput using formats.localize_input, which is not
     388        # necessarily the format used for this  widget. Attempt to convert it.
     389        try:
     390            input_format = '%H:%M:%S'
     391            initial = datetime.time(*time.strptime(initial, input_format)[3:6])
     392        except (TypeError, ValueError):
     393            pass
    369394        return super(TimeInput, self)._has_changed(self._format_value(initial), data)
    370395
    371396class CheckboxInput(Widget):
  • tests/regressiontests/forms/widgets.py

     
    33>>> from django.forms import *
    44>>> from django.forms.widgets import RadioFieldRenderer
    55>>> from django.utils.safestring import mark_safe
     6>>> from django.utils.encoding import force_unicode
    67>>> import datetime
    78>>> import time
    89>>> import re
     
    11341135>>> w._has_changed(d, '17/09/2007 12:51')
    11351136False
    11361137
     1138Make sure a custom format works with _has_changed. The hidden input will use
     1139force_unicode to display the initial value.
     1140>>> data = datetime.datetime(2010, 3, 6, 12, 0, 0)
     1141>>> custom_format = '%d.%m.%Y %H:%M'
     1142>>> w = DateTimeInput(format=custom_format)
     1143>>> w._has_changed(force_unicode(data), data.strftime(custom_format))
     1144False
     1145
     1146
    11371147# DateInput ###################################################################
    11381148
    11391149>>> w = DateInput()
     
    11591169>>> w._has_changed(d, '17/09/2007')
    11601170False
    11611171
     1172Make sure a custom format works with _has_changed. The hidden input will use
     1173force_unicode to display the initial value.
     1174>>> data = datetime.date(2010, 3, 6)
     1175>>> custom_format = '%d.%m.%Y'
     1176>>> w = DateInput(format=custom_format)
     1177>>> w._has_changed(force_unicode(data), data.strftime(custom_format))
     1178False
     1179
     1180
    11621181# TimeInput ###################################################################
    11631182
    11641183>>> w = TimeInput()
     
    11871206>>> w._has_changed(t, '12:51')
    11881207False
    11891208
     1209Make sure a custom format works with _has_changed. The hidden input will use
     1210force_unicode to display the initial value.
     1211>>> data = datetime.time(13, 0)
     1212>>> custom_format = '%I:%M %p'
     1213>>> w = TimeInput(format=custom_format)
     1214>>> w._has_changed(force_unicode(data), data.strftime(custom_format))
     1215False
     1216
     1217
    11901218# SplitHiddenDateTimeWidget ###################################################
    11911219
    11921220>>> from django.forms.widgets import SplitHiddenDateTimeWidget
Back to Top