summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-10-21 14:50:47 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-10-21 14:50:47 +0000
commit1671fc48ab9f1c73ef04a10f0eb9e1f8452cd004 (patch)
tree1a5290e5d6429e93d5d50829b8ddde0ecadd5bcd
parentf5831b03c908d9d4bf59f0a0cef4e3db221367ca (diff)
Changed the default form presentation of datetime values to not include the
fractional second values (they usually aren't going to be needed). Based on patches from yi.codeplayer@gmail.com, andrews and Wiliam Alves de Souza. Fixed #4428, #4487 git-svn-id: http://code.djangoproject.com/svn/django/trunk@6578 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/newforms/fields.py4
-rw-r--r--django/newforms/widgets.py20
-rw-r--r--docs/newforms.txt9
-rw-r--r--tests/regressiontests/forms/widgets.py15
4 files changed, 43 insertions, 5 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index 26935140c3..11e6f51779 100644
--- a/django/newforms/fields.py
+++ b/django/newforms/fields.py
@@ -11,7 +11,7 @@ from django.utils.translation import ugettext
from django.utils.encoding import StrAndUnicode, smart_unicode
from util import ErrorList, ValidationError
-from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
+from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
try:
from decimal import Decimal, DecimalException
@@ -284,6 +284,8 @@ DEFAULT_DATETIME_INPUT_FORMATS = (
)
class DateTimeField(Field):
+ widget = DateTimeInput
+
def __init__(self, input_formats=None, *args, **kwargs):
super(DateTimeField, self).__init__(*args, **kwargs)
self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py
index 62e2815d0b..6364a28897 100644
--- a/django/newforms/widgets.py
+++ b/django/newforms/widgets.py
@@ -8,6 +8,7 @@ except NameError:
from sets import Set as set # Python 2.3 fallback
import copy
+import datetime
from itertools import chain
from django.utils.datastructures import MultiValueDict
@@ -19,7 +20,7 @@ from util import flatatt
__all__ = (
'Widget', 'TextInput', 'PasswordInput',
'HiddenInput', 'MultipleHiddenInput',
- 'FileInput', 'Textarea', 'CheckboxInput',
+ 'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
)
@@ -133,7 +134,7 @@ class FileInput(Input):
def render(self, name, value, attrs=None):
return super(FileInput, self).render(name, None, attrs=attrs)
-
+
def value_from_datadict(self, data, files, name):
"File widgets take data from FILES, not POST"
return files.get(name, None)
@@ -151,6 +152,19 @@ class Textarea(Widget):
final_attrs = self.build_attrs(attrs, name=name)
return u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))
+class DateTimeInput(Input):
+ input_type = 'text'
+ format = '%Y-%m-%d %H:%M:%S' # '2006-10-25 14:30:59'
+
+ def __init__(self, attrs=None, format=None):
+ super(DateTimeInput, self).__init__(attrs)
+ if format:
+ self.format = format
+
+ def render(self, name, value, attrs=None):
+ return super(DateTimeInput, self).render(name,
+ value.strftime(self.format), attrs)
+
class CheckboxInput(Widget):
def __init__(self, attrs=None, check_test=bool):
super(CheckboxInput, self).__init__(attrs)
@@ -432,5 +446,5 @@ class SplitDateTimeWidget(MultiWidget):
def decompress(self, value):
if value:
- return [value.date(), value.time()]
+ return [value.date(), value.time().replace(microsecond=0)]
return [None, None]
diff --git a/docs/newforms.txt b/docs/newforms.txt
index 24294d68f2..5e33a478ee 100644
--- a/docs/newforms.txt
+++ b/docs/newforms.txt
@@ -1201,7 +1201,7 @@ If no ``input_formats`` argument is provided, the default input formats are::
``DateTimeField``
~~~~~~~~~~~~~~~~~
- * Default widget: ``TextInput``
+ * Default widget: ``DateTimeInput``
* Empty value: ``None``
* Normalizes to: A Python ``datetime.datetime`` object.
* Validates that the given value is either a ``datetime.datetime``,
@@ -1222,6 +1222,9 @@ If no ``input_formats`` argument is provided, the default input formats are::
'%m/%d/%y %H:%M', # '10/25/06 14:30'
'%m/%d/%y', # '10/25/06'
+**New in Django development version:** The ``DateTimeField`` used to use a
+``TextInput`` widget by default. This has now changed.
+
``DecimalField``
~~~~~~~~~~~~~~~~
@@ -1558,6 +1561,7 @@ commonly used groups of widgets:
``MultipleHiddenInput`` Multiple ``<input type='hidden' ...``
instances.
``FileInput`` ``<input type='file' ...``
+ ``DateTimeInput`` ``<input type='text' ...``
``Textarea`` ``<textarea>...</textarea>``
``CheckboxInput`` ``<input type='checkbox' ...``
``Select`` ``<select><option ...``
@@ -1571,6 +1575,9 @@ commonly used groups of widgets:
one for the Date, and one for the Time.
============================ ===========================================
+**New in Django development version:** The ``DateTimeInput`` has been added
+since the last release.
+
Specifying widgets
------------------
diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
index 6b7c858041..032f3cbb91 100644
--- a/tests/regressiontests/forms/widgets.py
+++ b/tests/regressiontests/forms/widgets.py
@@ -851,4 +851,19 @@ included on both widgets.
>>> w = SplitDateTimeWidget(attrs={'class': 'pretty'})
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
+
+# DateTimeInput ###############################################################
+
+>>> w = DateTimeInput()
+>>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
+>>> print d
+2007-09-17 12:51:34.482548
+
+The microseconds are trimmed on display, by default.
+>>> w.render('date', d)
+u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
+>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
+u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
+>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
+u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
"""