Ticket #3672: ticket#3672--patch-test.diff

File ticket#3672--patch-test.diff, 5.3 KB (added by Dinoboff, 16 years ago)

Patch and test

  • django/newforms/fields.py

     
    2626from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str
    2727
    2828from util import ErrorList, ValidationError
    29 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
     29from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, DateInput
    3030from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
    3131
    3232__all__ = (
     
    264264)
    265265
    266266class DateField(Field):
     267    widget = DateInput
    267268    default_error_messages = {
    268269        'invalid': _(u'Enter a valid date.'),
    269270    }
     
    271272    def __init__(self, input_formats=None, *args, **kwargs):
    272273        super(DateField, self).__init__(*args, **kwargs)
    273274        self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
     275        self.widget.format = self.input_formats[0]
    274276
    275277    def clean(self, value):
    276278        """
     
    343345    def __init__(self, input_formats=None, *args, **kwargs):
    344346        super(DateTimeField, self).__init__(*args, **kwargs)
    345347        self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
     348        self.widget.format = self.input_formats[0]
    346349
    347350    def clean(self, value):
    348351        """
  • django/newforms/widgets.py

     
    2020__all__ = (
    2121    'Widget', 'TextInput', 'PasswordInput',
    2222    'HiddenInput', 'MultipleHiddenInput',
    23     'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
     23    'FileInput', 'DateTimeInput', 'DateInput', 'Textarea', 'CheckboxInput',
    2424    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
    2525    'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
    2626)
     
    173173            value = value.strftime(self.format)
    174174        return super(DateTimeInput, self).render(name, value, attrs)
    175175
     176class DateInput(DateTimeInput):
     177    format = '%Y-%m-%d'
     178
    176179class CheckboxInput(Widget):
    177180    def __init__(self, attrs=None, check_test=bool):
    178181        super(CheckboxInput, self).__init__(attrs)
     
    371374                label_for = u' for="%s"' % final_attrs['id']
    372375            else:
    373376                label_for = ''
    374                
     377
    375378            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
    376379            option_value = force_unicode(option_value)
    377380            rendered_cb = cb.render(name, option_value)
  • tests/regressiontests/forms/models.py

     
    11import datetime
    22
    33from django.db import models
     4from django import newforms as forms
    45
    56class BoundaryModel(models.Model):
    67    positive_integer = models.PositiveIntegerField(null=True, blank=True)
     
    1415    """For ModelChoiceField and ModelMultipleChoiceField tests."""
    1516    name = models.CharField(max_length=10)
    1617
     18class 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
     22class 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
    1729__test__ = {'API_TESTS': """
    1830>>> from django.newforms import form_for_model, form_for_instance
    1931
     
    5062datetime.date(1969, 4, 4)
    5163>>> InstanceForm().fields['value'].initial
    526412
     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" />
    5373"""}
  • tests/regressiontests/forms/widgets.py

     
    929929u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
    930930>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    931931u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
     932
     933Set 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)
     937u'<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)
     940u'<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)
     947u'<input type="text" name="date" value="2007-09-17" />'
     948
     949
    932950"""
Back to Top