Ticket #8962: 8962-datetime-format-r9843.diff
File 8962-datetime-format-r9843.diff, 14.4 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, SplitDateTimeWidget, 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 } … … 850 851 'invalid_time': _(u'Enter a valid time.'), 851 852 } 852 853 853 def __init__(self, *args, **kwargs):854 def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs): 854 855 errors = self.default_error_messages.copy() 855 856 if 'error_messages' in kwargs: 856 857 errors.update(kwargs['error_messages']) 857 858 fields = ( 858 DateField( error_messages={'invalid': errors['invalid_date']}),859 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']}), 860 861 ) 861 862 super(SplitDateTimeField, self).__init__(fields, *args, **kwargs) 862 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', … … 285 285 return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), 286 286 conditional_escape(force_unicode(value)))) 287 287 288 class DateInput(Input): 289 input_type = 'text' 290 format = '%Y-%m-%d' # '2006-10-25' 291 292 def __init__(self, attrs=None, format=None): 293 super(DateInput, self).__init__(attrs) 294 if format: 295 self.format = format 296 297 def render(self, name, value, attrs=None): 298 if value is None: 299 value = '' 300 elif hasattr(value, 'strftime'): 301 value = datetime_safe.new_date(value) 302 value = value.strftime(self.format) 303 return super(DateInput, self).render(name, value, attrs) 304 288 305 class DateTimeInput(Input): 289 306 input_type = 'text' 290 307 format = '%Y-%m-%d %H:%M:%S' # '2006-10-25 14:30:59' … … 304 321 305 322 class TimeInput(Input): 306 323 input_type = 'text' 324 format = '%H:%M:%S' # '14:30:59' 307 325 326 def __init__(self, attrs=None, format=None): 327 super(TimeInput, self).__init__(attrs) 328 if format: 329 self.format = format 330 308 331 def render(self, name, value, attrs=None): 309 332 if value is None: 310 333 value = '' 311 elif isinstance(value, time):312 value = value. replace(microsecond=0)334 elif hasattr(value, 'strftime'): 335 value = value.strftime(self.format) 313 336 return super(TimeInput, self).render(name, value, attrs) 314 337 315 338 class CheckboxInput(Widget): … … 654 677 """ 655 678 A Widget that splits datetime input into two <input type="text"> boxes. 656 679 """ 657 def __init__(self, attrs=None): 658 widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs)) 680 date_format = DateInput.format 681 time_format = TimeInput.format 682 683 def __init__(self, attrs=None, date_format=None, time_format=None): 684 if date_format: 685 self.date_format = date_format 686 if time_format: 687 self.time_format = time_format 688 widgets = (DateInput(attrs=attrs, format=self.date_format), 689 TimeInput(attrs=attrs, format=self.time_format)) 659 690 super(SplitDateTimeWidget, self).__init__(widgets, attrs) 660 691 661 692 def decompress(self, value): … … 670 701 def __init__(self, attrs=None): 671 702 widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs)) 672 703 super(SplitDateTimeWidget, self).__init__(widgets, attrs) 673 -
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
250 250 Eugene Lazutkin <http://lazutkin.com/blog/> 251 251 lcordier@point45.com 252 252 Jeong-Min Lee <falsetru@gmail.com> 253 Tai Lee <real.human@mrmachine.net> 253 254 Jannis Leidel <jl@websushi.org> 254 255 Christopher Lenz <http://www.cmlenz.net/> 255 256 lerouxb@gmail.com … … 289 290 Aljosa Mohorovic <aljosa.mohorovic@gmail.com> 290 291 Ramiro Morales <rm0@gmx.net> 291 292 Eric Moritz <http://eric.themoritzfamily.com/> 292 mrmachine <real.human@mrmachine.net>293 293 Robin Munn <http://www.geekforgod.com/> 294 294 James Murty 295 295 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
397 397 398 398 .. class:: DateField(**kwargs) 399 399 400 * Default widget: `` TextInput``400 * Default widget: ``DateInput`` 401 401 * Empty value: ``None`` 402 402 * Normalizes to: A Python ``datetime.date`` object. 403 403 * Validates that the given value is either a ``datetime.date``, … … 419 419 '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006' 420 420 '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006' 421 421 422 .. versionchanged:: development version 423 The ``DateField`` previously used a ``TextInput`` widget by default. It now 424 uses a ``DateInput`` widget. 425 422 426 ``DateTimeField`` 423 427 ~~~~~~~~~~~~~~~~~ 424 428 … … 738 742 739 743 .. class:: SplitDateTimeField(**kwargs) 740 744 745 * Default widget: ``SplitDateTimeWidget`` 746 * Empty value: ``None`` 747 * Normalizes to: A Python ``datetime.datetime`` object. 748 * Validates that the given value is a ``datetime.datetime`` or string 749 formatted in a particular datetime format. 750 * Error message keys: ``required``, ``invalid`` 751 752 Takes two optional arguments: 753 754 .. attribute:: SplitDateTimeField.input_date_formats 755 756 A list of formats used to attempt to convert a string to a valid 757 ``datetime.date`` object. 758 759 If no ``input_date_formats`` argument is provided, the default input formats 760 for ``DateField`` are used. 761 762 .. attribute:: SplitDateTimeField.input_time_formats 763 764 A list of formats used to attempt to convert a string to a valid 765 ``datetime.time`` object. 766 767 If no ``input_time_formats`` argument is provided, the default input formats 768 for ``TimeField`` are used. 769 770 .. versionchanged:: development version 771 The ``SplitDateTimeField`` previously used two ``TextInput`` widgets by 772 default. The ``input_date_formats`` and ``input_time_formats`` arguments 773 are also new in the development version. 774 741 775 Fields which handle relationships 742 776 --------------------------------- 743 777 -
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