Skip to content

Commit

Permalink
Fixed #12858. DateTime related widgets now handle custom formats prop…
Browse files Browse the repository at this point in the history
…erly in _has_changed. Thanks for the initial patch, camillo.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12698 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jkocherhans committed Mar 7, 2010
1 parent 686dac0 commit 5beb5f7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
30 changes: 28 additions & 2 deletions django/forms/widgets.py
Expand Up @@ -10,8 +10,10 @@
from django.utils.translation import ugettext from django.utils.translation import ugettext
from django.utils.encoding import StrAndUnicode, force_unicode from django.utils.encoding import StrAndUnicode, force_unicode
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import datetime_safe, formats from django.utils import formats
from datetime import time import time
import datetime
from django.utils.formats import get_format
from util import flatatt from util import flatatt
from urlparse import urljoin from urlparse import urljoin


Expand Down Expand Up @@ -313,6 +315,14 @@ def render(self, name, value, attrs=None):
return super(DateInput, self).render(name, value, attrs) return super(DateInput, self).render(name, value, attrs)


def _has_changed(self, initial, data): def _has_changed(self, initial, data):
# If our field has show_hidden_initial=True, initial will be a string
# formatted by HiddenInput using formats.localize_input, which is not
# necessarily the format used for this widget. Attempt to convert it.
try:
input_format = get_format('DATE_INPUT_FORMATS')[0]
initial = datetime.date(*time.strptime(initial, input_format)[:3])
except (TypeError, ValueError):
pass
return super(DateInput, self)._has_changed(self._format_value(initial), data) return super(DateInput, self)._has_changed(self._format_value(initial), data)


class DateTimeInput(Input): class DateTimeInput(Input):
Expand All @@ -336,6 +346,14 @@ def render(self, name, value, attrs=None):
return super(DateTimeInput, self).render(name, value, attrs) return super(DateTimeInput, self).render(name, value, attrs)


def _has_changed(self, initial, data): def _has_changed(self, initial, data):
# If our field has show_hidden_initial=True, initial will be a string
# formatted by HiddenInput using formats.localize_input, which is not
# necessarily the format used for this widget. Attempt to convert it.
try:
input_format = get_format('DATETIME_INPUT_FORMATS')[0]
initial = datetime.datetime(*time.strptime(initial, input_format)[:6])
except (TypeError, ValueError):
pass
return super(DateTimeInput, self)._has_changed(self._format_value(initial), data) return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)


class TimeInput(Input): class TimeInput(Input):
Expand All @@ -359,6 +377,14 @@ def render(self, name, value, attrs=None):
return super(TimeInput, self).render(name, value, attrs) return super(TimeInput, self).render(name, value, attrs)


def _has_changed(self, initial, data): def _has_changed(self, initial, data):
# If our field has show_hidden_initial=True, initial will be a string
# formatted by HiddenInput using formats.localize_input, which is not
# necessarily the format used for this widget. Attempt to convert it.
try:
input_format = get_format('TIME_INPUT_FORMATS')[0]
initial = datetime.time(*time.strptime(initial, input_format)[3:6])
except (TypeError, ValueError):
pass
return super(TimeInput, self)._has_changed(self._format_value(initial), data) return super(TimeInput, self)._has_changed(self._format_value(initial), data)


class CheckboxInput(Widget): class CheckboxInput(Widget):
Expand Down
27 changes: 27 additions & 0 deletions tests/regressiontests/forms/widgets.py
Expand Up @@ -3,6 +3,7 @@
>>> from django.forms import * >>> from django.forms import *
>>> from django.forms.widgets import RadioFieldRenderer >>> from django.forms.widgets import RadioFieldRenderer
>>> from django.utils.safestring import mark_safe >>> from django.utils.safestring import mark_safe
>>> from django.utils import formats
>>> import datetime >>> import datetime
>>> import time >>> import time
>>> import re >>> import re
Expand Down Expand Up @@ -1149,6 +1150,14 @@
>>> w._has_changed(d, '17/09/2007 12:51') >>> w._has_changed(d, '17/09/2007 12:51')
False False
Make sure a custom format works with _has_changed. The hidden input will use
format.localize_input to display the initial value.
>>> data = datetime.datetime(2010, 3, 6, 12, 0, 0)
>>> custom_format = '%d.%m.%Y %H:%M'
>>> w = DateTimeInput(format=custom_format)
>>> w._has_changed(formats.localize_input(data), data.strftime(custom_format))
False
# DateInput ################################################################### # DateInput ###################################################################
Expand Down Expand Up @@ -1182,6 +1191,15 @@
>>> w._has_changed(d, '17/09/2007') >>> w._has_changed(d, '17/09/2007')
False False
Make sure a custom format works with _has_changed. The hidden input will use
format.localize_input to display the initial value.
>>> data = datetime.date(2010, 3, 6)
>>> custom_format = '%d.%m.%Y'
>>> w = DateInput(format=custom_format)
>>> w._has_changed(formats.localize_input(data), data.strftime(custom_format))
False
# TimeInput ################################################################### # TimeInput ###################################################################
>>> w = TimeInput() >>> w = TimeInput()
Expand Down Expand Up @@ -1217,6 +1235,15 @@
>>> w._has_changed(t, '12:51') >>> w._has_changed(t, '12:51')
False False
Make sure a custom format works with _has_changed. The hidden input will use
format.localize_input to display the initial value.
>>> data = datetime.time(13, 0)
>>> custom_format = '%I:%M %p'
>>> w = TimeInput(format=custom_format)
>>> w._has_changed(formats.localize_input(data), data.strftime(custom_format))
False
# SplitHiddenDateTimeWidget ################################################### # SplitHiddenDateTimeWidget ###################################################
>>> from django.forms.widgets import SplitHiddenDateTimeWidget >>> from django.forms.widgets import SplitHiddenDateTimeWidget
Expand Down

0 comments on commit 5beb5f7

Please sign in to comment.