Ticket #3672: ticket#3672--patch-test.diff
File ticket#3672--patch-test.diff, 5.3 KB (added by , 16 years ago) |
---|
-
django/newforms/fields.py
26 26 from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str 27 27 28 28 from util import ErrorList, ValidationError 29 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput 29 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, DateInput 30 30 from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile 31 31 32 32 __all__ = ( … … 264 264 ) 265 265 266 266 class DateField(Field): 267 widget = DateInput 267 268 default_error_messages = { 268 269 'invalid': _(u'Enter a valid date.'), 269 270 } … … 271 272 def __init__(self, input_formats=None, *args, **kwargs): 272 273 super(DateField, self).__init__(*args, **kwargs) 273 274 self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS 275 self.widget.format = self.input_formats[0] 274 276 275 277 def clean(self, value): 276 278 """ … … 343 345 def __init__(self, input_formats=None, *args, **kwargs): 344 346 super(DateTimeField, self).__init__(*args, **kwargs) 345 347 self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS 348 self.widget.format = self.input_formats[0] 346 349 347 350 def clean(self, value): 348 351 """ -
django/newforms/widgets.py
20 20 __all__ = ( 21 21 'Widget', 'TextInput', 'PasswordInput', 22 22 'HiddenInput', 'MultipleHiddenInput', 23 'FileInput', 'DateTimeInput', ' Textarea', 'CheckboxInput',23 'FileInput', 'DateTimeInput', 'DateInput', 'Textarea', 'CheckboxInput', 24 24 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 25 25 'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget', 26 26 ) … … 173 173 value = value.strftime(self.format) 174 174 return super(DateTimeInput, self).render(name, value, attrs) 175 175 176 class DateInput(DateTimeInput): 177 format = '%Y-%m-%d' 178 176 179 class CheckboxInput(Widget): 177 180 def __init__(self, attrs=None, check_test=bool): 178 181 super(CheckboxInput, self).__init__(attrs) … … 371 374 label_for = u' for="%s"' % final_attrs['id'] 372 375 else: 373 376 label_for = '' 374 377 375 378 cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) 376 379 option_value = force_unicode(option_value) 377 380 rendered_cb = cb.render(name, option_value) -
tests/regressiontests/forms/models.py
1 1 import datetime 2 2 3 3 from django.db import models 4 from django import newforms as forms 4 5 5 6 class BoundaryModel(models.Model): 6 7 positive_integer = models.PositiveIntegerField(null=True, blank=True) … … 14 15 """For ModelChoiceField and ModelMultipleChoiceField tests.""" 15 16 name = models.CharField(max_length=10) 16 17 18 class DateTimeModel(models.Model): 19 date = models.DateField(default = datetime.date(1980, 1, 1)) 20 time = models.DateTimeField(default = datetime.datetime(1980, 1, 1, 0, 0, 0)) 21 22 class DateTimeForm(forms.ModelForm): 23 date = forms.DateField(input_formats=('%d/%m/%Y',)) 24 time = forms.DateTimeField(input_formats=('%d/%m/%Y %H:%M:%S',)) 25 26 class Meta: 27 model = DateTimeModel 28 17 29 __test__ = {'API_TESTS': """ 18 30 >>> from django.newforms import form_for_model, form_for_instance 19 31 … … 50 62 datetime.date(1969, 4, 4) 51 63 >>> InstanceForm().fields['value'].initial 52 64 12 65 66 # DateField and DateTimeField should format the instance's values according to input_formats 67 >>> i = DateTimeModel() 68 >>> f = DateTimeForm(instance=i) 69 >>> print f['date'] 70 <input type="text" name="date" value="01/01/1980" id="id_date" /> 71 >>> print f['time'] 72 <input type="text" name="time" value="01/01/1980 00:00:00" id="id_time" /> 53 73 """} -
tests/regressiontests/forms/widgets.py
929 929 u'<input type="text" name="date" value="2007-09-17 12:51:34" />' 930 930 >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51)) 931 931 u'<input type="text" name="date" value="2007-09-17 12:51:00" />' 932 933 Set Format 934 >>> w = DateTimeInput(format="%d/%m/%Y %H:%M:%S") 935 >>> d = datetime.datetime(2007, 9, 17, 12, 51, 34) 936 >>> w.render('date', d) 937 u'<input type="text" name="date" value="17/09/2007 12:51:34" />' 938 >>> w.format = "%d-%m-%Y %H:%M:%S" 939 >>> w.render('date', d) 940 u'<input type="text" name="date" value="17-09-2007 12:51:34" />' 941 942 # DateTimeInput ############################################################### 943 944 >>> w = DateInput() 945 >>> d = datetime.date(2007, 9, 17) 946 >>> w.render('date', d) 947 u'<input type="text" name="date" value="2007-09-17" />' 948 949 932 950 """