Ticket #3533: dateortime_widget.patch
File dateortime_widget.patch, 4.2 KB (added by , 18 years ago) |
---|
-
django/newforms/widgets.py
4 4 5 5 __all__ = ( 6 6 'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'MultipleHiddenInput', 7 'FileInput', 'Textarea', 'CheckboxInput', 7 'FileInput', 'Textarea', 'CheckboxInput', 'DateOrTimeTextInput', 8 8 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple', 9 9 'MultiWidget', 'SplitDateTimeWidget', 10 10 ) … … 14 14 from django.utils.html import escape 15 15 from django.utils.translation import gettext 16 16 from itertools import chain 17 from datetime import date, datetime, time 17 18 18 19 try: 19 20 set # Only available in Python 2.4+ … … 116 117 class FileInput(Input): 117 118 input_type = 'file' 118 119 120 class DateOrTimeTextInput(TextInput): 121 """ 122 A widget that renders date, datetime and time values using a specific format. 123 """ 124 def __init__(self, attrs=None, format='%Y-%m-%d'): 125 self.format = format 126 super(DateOrTimeTextInput, self).__init__(attrs) 127 128 def render(self, name, value, attrs=None): 129 if isinstance(value, (date, datetime, time)): 130 value = value.strftime(self.format) 131 return super(DateOrTimeTextInput, self).render(name, value, attrs) 132 119 133 class Textarea(Widget): 120 134 def render(self, name, value, attrs=None): 121 135 if value is None: value = '' … … 343 357 """ 344 358 A Widget that splits datetime input into two <input type="text"> boxes. 345 359 """ 346 def __init__(self, attrs=None): 347 widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs)) 360 def __init__(self, attrs=None, date_format='%Y-%m-%d', time_format='%H:%M:%S'): 361 widgets = (DateOrTimeTextInput(attrs=attrs, format=date_format), 362 DateOrTimeTextInput(attrs=attrs, format=time_format)) 348 363 super(SplitDateTimeWidget, self).__init__(widgets, attrs) 349 364 350 365 def decompress(self, value): -
tests/regressiontests/forms/tests.py
186 186 >>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'}) 187 187 u'<input type="file" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" name="email" />' 188 188 189 # DateOrTimeTextInput Widget ################################################## 190 191 >>> test_time = datetime.datetime(2006, 1, 10, 13, 30) 192 193 >>> w = DateOrTimeTextInput() 194 >>> w.render('date', None) 195 u'<input type="text" name="date" />' 196 >>> w.render('date', 'non-date') 197 u'<input type="text" name="date" value="non-date" />' 198 199 Defaults to %Y-%m-%d 200 >>> w.render('date', test_time) 201 u'<input type="text" name="date" value="2006-01-10" />' 202 203 Pass 'format' attribute to change the strftime when rendering. 204 >>> w = DateOrTimeTextInput(format='%m/%d/%Y') 205 >>> w.render('date', test_time) 206 u'<input type="text" name="date" value="01/10/2006" />' 207 >>> w = DateOrTimeTextInput(format='%H:%M:%S') 208 >>> w.render('time', test_time) 209 u'<input type="text" name="time" value="13:30:00" />' 210 211 Works with time and date objects too 212 >>> w = DateOrTimeTextInput(format='%H:%M:%S') 213 >>> w.render('time', test_time.time()) 214 u'<input type="text" name="time" value="13:30:00" />' 215 >>> w = DateOrTimeTextInput(format='%m/%d/%Y') 216 >>> w.render('date', test_time.date()) 217 u'<input type="text" name="date" value="01/10/2006" />' 218 189 219 # Textarea Widget ############################################################# 190 220 191 221 >>> w = Textarea() … … 787 817 >>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30)) 788 818 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" />' 789 819 820 You can also pass 'date_format' and 'time_format' to change the strftime format for the date and time. 821 >>> w = SplitDateTimeWidget(date_format='%m/%d/%Y', time_format='%#I:%M %p') 822 >>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30)) 823 u'<input type="text" name="date_0" value="01/10/2006" /><input type="text" name="date_1" value="7:30 AM" />' 824 790 825 ########## 791 826 # Fields # 792 827 ##########