summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2010-03-07 04:06:23 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2010-03-07 04:06:23 +0000
commit72a659f8f83f1875739cf79a390917ad7a3d384c (patch)
tree1f4c69e8b2cdae27383ece415a531f1d6993b8ca
parente8c768910afd441aa39759de27939eee5d141219 (diff)
[1.1.X] Fixed #12858. DateTime related widgets now handle custom formats properly in _has_changed. Backport of r12698 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12699 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/forms/widgets.py27
-rw-r--r--tests/regressiontests/forms/widgets.py28
2 files changed, 54 insertions, 1 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index 95c42ea055..c7bb1cbe9a 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -16,7 +16,8 @@ from django.utils.translation import ugettext
from django.utils.encoding import StrAndUnicode, force_unicode
from django.utils.safestring import mark_safe
from django.utils import datetime_safe
-from datetime import time
+import time
+import datetime
from util import flatatt
from urlparse import urljoin
@@ -319,6 +320,14 @@ class DateInput(Input):
return super(DateInput, self).render(name, value, attrs)
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 = '%Y-%m-%d'
+ initial = datetime.date(*time.strptime(initial, '%Y-%m-%d')[:3])
+ except (TypeError, ValueError):
+ pass
return super(DateInput, self)._has_changed(self._format_value(initial), data)
class DateTimeInput(Input):
@@ -343,6 +352,14 @@ class DateTimeInput(Input):
return super(DateTimeInput, self).render(name, value, attrs)
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 = '%Y-%m-%d %H:%M:%S'
+ initial = datetime.datetime(*time.strptime(initial, input_format)[:6])
+ except (TypeError, ValueError):
+ pass
return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)
class TimeInput(Input):
@@ -366,6 +383,14 @@ class TimeInput(Input):
return super(TimeInput, self).render(name, value, attrs)
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 = '%H:%M:%S'
+ 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)
class CheckboxInput(Widget):
diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
index 204f4d0573..a397cdf63b 100644
--- a/tests/regressiontests/forms/widgets.py
+++ b/tests/regressiontests/forms/widgets.py
@@ -3,6 +3,7 @@ tests = r"""
>>> from django.forms import *
>>> from django.forms.widgets import RadioFieldRenderer
>>> from django.utils.safestring import mark_safe
+>>> from django.utils.encoding import force_unicode
>>> import datetime
>>> import time
>>> import re
@@ -1134,6 +1135,15 @@ u'<input type="text" name="date" value="17/09/2007 12:51" />'
>>> w._has_changed(d, '17/09/2007 12:51')
False
+Make sure a custom format works with _has_changed. The hidden input will use
+force_unicode 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(force_unicode(data), data.strftime(custom_format))
+False
+
+
# DateInput ###################################################################
>>> w = DateInput()
@@ -1159,6 +1169,15 @@ u'<input type="text" name="date" value="17/09/2007" />'
>>> w._has_changed(d, '17/09/2007')
False
+Make sure a custom format works with _has_changed. The hidden input will use
+force_unicode to display the initial value.
+>>> data = datetime.date(2010, 3, 6)
+>>> custom_format = '%d.%m.%Y'
+>>> w = DateInput(format=custom_format)
+>>> w._has_changed(force_unicode(data), data.strftime(custom_format))
+False
+
+
# TimeInput ###################################################################
>>> w = TimeInput()
@@ -1187,6 +1206,15 @@ u'<input type="text" name="time" value="12:51" />'
>>> w._has_changed(t, '12:51')
False
+Make sure a custom format works with _has_changed. The hidden input will use
+force_unicode to display the initial value.
+>>> data = datetime.time(13, 0)
+>>> custom_format = '%I:%M %p'
+>>> w = TimeInput(format=custom_format)
+>>> w._has_changed(force_unicode(data), data.strftime(custom_format))
+False
+
+
# SplitHiddenDateTimeWidget ###################################################
>>> from django.forms.widgets import SplitHiddenDateTimeWidget