diff options
| author | Justin Bronn <jbronn@gmail.com> | 2007-10-26 20:47:20 +0000 |
|---|---|---|
| committer | Justin Bronn <jbronn@gmail.com> | 2007-10-26 20:47:20 +0000 |
| commit | 4ffbddf92d89c3b31cef90043721184a501cd454 (patch) | |
| tree | db8131d40b0a5437270a6b1e8d579113ab3508e8 /django/newforms | |
| parent | f66ee9d0065838a0f6c9c76203a775a78446cdf7 (diff) | |
gis: Merged revisions 6525-6613 via svnmerge from [repos:django/trunk trunk].
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms')
| -rw-r--r-- | django/newforms/fields.py | 10 | ||||
| -rw-r--r-- | django/newforms/widgets.py | 29 |
2 files changed, 35 insertions, 4 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py index a39987e1b5..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 @@ -300,6 +302,12 @@ class DateTimeField(Field): return value if isinstance(value, datetime.date): return datetime.datetime(value.year, value.month, value.day) + if isinstance(value, list): + # Input comes from a SplitDateTimeWidget, for example. So, it's two + # components: date and time. + if len(value) != 2: + raise ValidationError(ugettext(u'Enter a valid date/time.')) + value = '%s %s' % tuple(value) for format in self.input_formats: try: return datetime.datetime(*time.strptime(value, format)[:6]) diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 2c235513d0..076845427d 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -19,7 +19,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 +133,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 +151,22 @@ 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): + if value is None: + value = '' + elif hasattr(value, 'strftime'): + value = value.strftime(self.format) + return super(DateTimeInput, self).render(name, value, attrs) + class CheckboxInput(Widget): def __init__(self, attrs=None, check_test=bool): super(CheckboxInput, self).__init__(attrs) @@ -170,6 +186,13 @@ class CheckboxInput(Widget): final_attrs['value'] = force_unicode(value) # Only add the 'value' attribute if a value is non-empty. return u'<input%s />' % flatatt(final_attrs) + def value_from_datadict(self, data, files, name): + if name not in data: + # A missing value means False because HTML form submission does not + # send results for unselected checkboxes. + return False + return super(CheckboxInput, self).value_from_datadict(data, files, name) + class Select(Widget): def __init__(self, attrs=None, choices=()): super(Select, self).__init__(attrs) @@ -425,5 +448,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] |
