Ticket #8962: 8962-datetime-format-r8983.2.diff
File 8962-datetime-format-r8983.2.diff, 14.6 KB (added by , 16 years ago) |
---|
-
django/forms/fields.py
28 28 from django.utils.encoding import smart_unicode, smart_str 29 29 30 30 from util import ErrorList, ValidationError 31 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, Date TimeInput, TimeInput, SplitHiddenDateTimeWidget31 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget 32 32 from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile 33 33 34 34 __all__ = ( … … 283 283 ) 284 284 285 285 class DateField(Field): 286 widget = DateInput 286 287 default_error_messages = { 287 288 'invalid': _(u'Enter a valid date.'), 288 289 } … … 843 844 self.widget.choices = self.choices 844 845 845 846 class SplitDateTimeField(MultiValueField): 847 widget = SplitDateTimeWidget 846 848 hidden_widget = SplitHiddenDateTimeWidget 847 849 default_error_messages = { 848 850 'invalid_date': _(u'Enter a valid date.'), 849 851 'invalid_time': _(u'Enter a valid time.'), 850 852 } 851 853 852 def __init__(self, *args, **kwargs):854 def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs): 853 855 errors = self.default_error_messages.copy() 854 856 if 'error_messages' in kwargs: 855 857 errors.update(kwargs['error_messages']) 856 858 fields = ( 857 DateField( error_messages={'invalid': errors['invalid_date']}),858 TimeField( error_messages={'invalid': errors['invalid_time']}),859 DateField(input_formats=input_date_formats, error_messages={'invalid': errors['invalid_date']}), 860 TimeField(input_formats=input_time_formats, error_messages={'invalid': errors['invalid_time']}), 859 861 ) 860 862 super(SplitDateTimeField, self).__init__(fields, *args, **kwargs) 861 863 -
django/forms/widgets.py
23 23 __all__ = ( 24 24 'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput', 25 25 'HiddenInput', 'MultipleHiddenInput', 26 'FileInput', 'Date TimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',26 'FileInput', 'DateInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput', 27 27 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 28 28 'CheckboxSelectMultiple', 'MultiWidget', 29 29 'SplitDateTimeWidget', … … 286 286 return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), 287 287 conditional_escape(force_unicode(value)))) 288 288 289 class DateInput(Input): 290 input_type = 'text' 291 format = '%Y-%m-%d' # '2006-10-25' 292 293 def __init__(self, attrs=None, format=None): 294 super(DateInput, self).__init__(attrs) 295 if format: 296 self.format = format 297 298 def render(self, name, value, attrs=None): 299 if value is None: 300 value = '' 301 elif hasattr(value, 'strftime'): 302 value = datetime_safe.new_date(value) 303 value = value.strftime(self.format) 304 return super(DateInput, self).render(name, value, attrs) 305 289 306 class DateTimeInput(Input): 290 307 input_type = 'text' 291 308 format = '%Y-%m-%d %H:%M:%S' # '2006-10-25 14:30:59' … … 305 322 306 323 class TimeInput(Input): 307 324 input_type = 'text' 325 format = '%H:%M:%S' # '14:30:59' 308 326 327 def __init__(self, attrs=None, format=None): 328 super(TimeInput, self).__init__(attrs) 329 if format: 330 self.format = format 331 309 332 def render(self, name, value, attrs=None): 310 333 if value is None: 311 334 value = '' 312 elif isinstance(value, time):313 value = value. replace(microsecond=0)335 elif hasattr(value, 'strftime'): 336 value = value.strftime(self.format) 314 337 return super(TimeInput, self).render(name, value, attrs) 315 338 316 339 class CheckboxInput(Widget): … … 655 678 """ 656 679 A Widget that splits datetime input into two <input type="text"> boxes. 657 680 """ 658 def __init__(self, attrs=None): 659 widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs)) 681 date_format = DateInput.format 682 time_format = TimeInput.format 683 684 def __init__(self, attrs=None, date_format=None, time_format=None): 685 if date_format: 686 self.date_format = date_format 687 if time_format: 688 self.time_format = time_format 689 widgets = (DateInput(attrs=attrs, format=self.date_format), 690 TimeInput(attrs=attrs, format=self.time_format)) 660 691 super(SplitDateTimeWidget, self).__init__(widgets, attrs) 661 692 662 693 def decompress(self, value): … … 671 702 def __init__(self, attrs=None): 672 703 widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs)) 673 704 super(SplitDateTimeWidget, self).__init__(widgets, attrs) 674 -
django/contrib/localflavor/generic/forms.py
36 36 def __init__(self, input_formats=None, *args, **kwargs): 37 37 input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS 38 38 super(DateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs) 39 40 class SplitDateTimeField(forms.SplitDateTimeField): 41 """ 42 Split date and time input fields which use non-US date and time input 43 formats by default. 44 """ 45 def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs): 46 input_date_formats = input_date_formats or DEFAULT_DATE_INPUT_FORMATS 47 super(SplitDateTimeField, self).__init__(input_date_formats=input_date_formats, 48 input_time_formats=input_time_formats, *args, **kwargs) -
tests/regressiontests/forms/widgets.py
1071 1071 >>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30)) 1072 1072 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" />' 1073 1073 1074 Use 'date_format' and 'time_format' to change the way a value is displayed. 1075 >>> w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M') 1076 >>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30)) 1077 u'<input type="text" name="date_0" value="10/01/2006" /><input type="text" name="date_1" value="07:30" />' 1078 1074 1079 >>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:40:00']) 1075 1080 False 1076 1081 >>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:41:00']) … … 1093 1098 >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51)) 1094 1099 u'<input type="text" name="date" value="2007-09-17 12:51:00" />' 1095 1100 1101 Use 'format' to change the way a value is displayed. 1102 >>> w = DateTimeInput(format='%d/%m/%Y %H:%M') 1103 >>> w.render('date', d) 1104 u'<input type="text" name="date" value="17/09/2007 12:51" />' 1105 1106 # DateInput ################################################################### 1107 1108 >>> w = DateInput() 1109 >>> w.render('date', None) 1110 u'<input type="text" name="date" />' 1111 >>> d = datetime.date(2007, 9, 17) 1112 >>> print d 1113 2007-09-17 1114 1115 >>> w.render('date', d) 1116 u'<input type="text" name="date" value="2007-09-17" />' 1117 >>> w.render('date', datetime.date(2007, 9, 17)) 1118 u'<input type="text" name="date" value="2007-09-17" />' 1119 1120 We should be able to initialize from a unicode value. 1121 >>> w.render('date', u'2007-09-17') 1122 u'<input type="text" name="date" value="2007-09-17" />' 1123 1124 Use 'format' to change the way a value is displayed. 1125 >>> w = DateInput(format='%d/%m/%Y') 1126 >>> w.render('date', d) 1127 u'<input type="text" name="date" value="17/09/2007" />' 1128 1096 1129 # TimeInput ################################################################### 1097 1130 1098 1131 >>> w = TimeInput() … … 1114 1147 >>> w.render('time', u'13:12:11') 1115 1148 u'<input type="text" name="time" value="13:12:11" />' 1116 1149 1150 Use 'format' to change the way a value is displayed. 1151 >>> w = TimeInput(format='%H:%M') 1152 >>> w.render('time', t) 1153 u'<input type="text" name="time" value="12:51" />' 1154 1117 1155 # SplitHiddenDateTimeWidget ################################################### 1118 1156 1119 1157 >>> from django.forms.widgets import SplitHiddenDateTimeWidget … … 1121 1159 >>> w = SplitHiddenDateTimeWidget() 1122 1160 >>> w.render('date', '') 1123 1161 u'<input type="hidden" name="date_0" /><input type="hidden" name="date_1" />' 1162 >>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548) 1163 >>> print d 1164 2007-09-17 12:51:34.482548 1124 1165 >>> w.render('date', d) 1125 1166 u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" name="date_1" value="12:51:34" />' 1126 1167 >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34)) -
AUTHORS
239 239 Eugene Lazutkin <http://lazutkin.com/blog/> 240 240 lcordier@point45.com 241 241 Jeong-Min Lee <falsetru@gmail.com> 242 Tai Lee <real.human@mrmachine.net> 242 243 Jannis Leidel <jl@websushi.org> 243 244 Christopher Lenz <http://www.cmlenz.net/> 244 245 lerouxb@gmail.com … … 278 279 Aljosa Mohorovic <aljosa.mohorovic@gmail.com> 279 280 Ramiro Morales <rm0@gmx.net> 280 281 Eric Moritz <http://eric.themoritzfamily.com/> 281 mrmachine <real.human@mrmachine.net>282 282 Robin Munn <http://www.geekforgod.com/> 283 283 James Murty 284 284 msundstr -
docs/ref/contrib/localflavor.txt
65 65 * `United States of America`_ 66 66 67 67 The ``django.contrib.localflavor`` package also includes a ``generic`` subpackage, 68 containing useful code that is not specific to one particular country or 69 culture. Currently, it defines date and datetime input fields based on those 70 from :ref:`forms <topics-forms-index>`, but with non-US default formats.68 containing useful code that is not specific to one particular country or culture. 69 Currently, it defines date, datetime and split datetime input fields based on 70 those from :ref:`forms <topics-forms-index>`, but with non-US default formats. 71 71 Here's an example of how to use them:: 72 72 73 73 from django import forms -
docs/ref/forms/fields.txt
396 396 397 397 .. class:: DateField(**kwargs) 398 398 399 * Default widget: `` TextInput``399 * Default widget: ``DateInput`` 400 400 * Empty value: ``None`` 401 401 * Normalizes to: A Python ``datetime.date`` object. 402 402 * Validates that the given value is either a ``datetime.date``, … … 418 418 '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006' 419 419 '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006' 420 420 421 .. versionchanged:: development version 422 The ``DateField`` previously used a ``TextInput`` widget by default. It now 423 uses a ``DateInput`` widget. 424 421 425 ``DateTimeField`` 422 426 ~~~~~~~~~~~~~~~~~ 423 427 … … 737 741 738 742 .. class:: SplitDateTimeField(**kwargs) 739 743 744 * Default widget: ``SplitDateTimeWidget`` 745 * Empty value: ``None`` 746 * Normalizes to: A Python ``datetime.datetime`` object. 747 * Validates that the given value is a ``datetime.datetime`` or string 748 formatted in a particular datetime format. 749 * Error message keys: ``required``, ``invalid`` 750 751 Takes two optional arguments: 752 753 .. attribute:: SplitDateTimeField.input_date_formats 754 755 A list of formats used to attempt to convert a string to a valid 756 ``datetime.date`` object. 757 758 If no ``input_date_formats`` argument is provided, the default input formats 759 for ``DateField`` are used. 760 761 .. attribute:: SplitDateTimeField.input_time_formats 762 763 A list of formats used to attempt to convert a string to a valid 764 ``datetime.time`` object. 765 766 If no ``input_time_formats`` argument is provided, the default input formats 767 for ``TimeField`` are used. 768 769 .. versionchanged:: development version 770 The ``SplitDateTimeField`` previously used two ``TextInput`` widgets by 771 default. The ``input_date_formats`` and ``input_time_formats`` arguments 772 are also new in the development version. 773 740 774 Fields which handle relationships 741 775 --------------------------------- 742 776 -
docs/ref/forms/widgets.txt
36 36 37 37 File upload input: ``<input type='file' ...>`` 38 38 39 .. class:: DateInput 40 41 .. versionadded:: development version 42 43 Date input as a simple text box: ``<input type='text' ...>`` 44 45 Takes one optional argument: 46 47 .. attribute:: DateInput.format 48 49 The format in which this field's initial value will be displayed. 50 51 If no ``format`` argument is provided, the default format is ``'%Y-%m-%d'``. 52 39 53 .. class:: DateTimeInput 40 54 41 55 .. versionadded:: 1.0 42 56 43 57 Date/time input as a simple text box: ``<input type='text' ...>`` 44 58 59 Takes one optional argument: 60 61 .. attribute:: DateTimeInput.format 62 63 The format in which this field's initial value will be displayed. 64 65 If no ``format`` argument is provided, the default format is ``'%Y-%m-%d %H:%M:%S'``. 66 67 .. class:: TimeInput 68 69 Time input as a simple text box: ``<input type='text' ...>`` 70 71 Takes one optional argument: 72 73 .. attribute:: TimeInput.format 74 75 The format in which this field's initial value will be displayed. 76 77 If no ``format`` argument is provided, the default format is ``'%H:%M:%S'``. 78 79 .. versionchanged:: development version 80 The ``format`` argument is new in the development version. 81 45 82 .. class:: Textarea 46 83 47 84 Text area: ``<textarea>...</textarea>`` … … 91 128 92 129 .. class:: SplitDateTimeWidget 93 130 94 Wrapper around two ``TextInput`` widgets: one for the date, and one for the95 time.131 Wrapper around two widgets: ``DateInput`` for the date, and ``TimeInput`` 132 for the time. 96 133 134 Takes two optional arguments, ``date_format`` and ``time_format``, which 135 work just like the ``format`` argument for ``DateInput`` and ``TimeInput``. 136 97 137 Specifying widgets 98 138 ------------------ 99 139