Ticket #7499: time_input_widget.2.diff

File time_input_widget.2.diff, 3.5 KB (added by Kevin McConnell, 11 years ago)

Updated patch

  • django/forms/fields.py

     
    2727from django.utils.encoding import smart_unicode, smart_str
    2828
    2929from util import ErrorList, ValidationError
    30 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
     30from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput
    3131from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
    3232
    3333__all__ = (
    3434    'Field', 'CharField', 'IntegerField',
    3535    'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
    3636    'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
    37     'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
     37    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField', 'TimeField',
    3838    'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField',
    3939    'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
    4040    'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
     
    311311)
    312312
    313313class TimeField(Field):
     314    widget = TimeInput()
    314315    default_error_messages = {
    315316        'invalid': _(u'Enter a valid time.')
    316317    }
  • django/forms/widgets.py

     
    2222__all__ = (
    2323    'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
    2424    'HiddenInput', 'MultipleHiddenInput',
    25     'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
     25    'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
    2626    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
    2727    'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
    2828)
     
    301301            value = value.strftime(self.format)
    302302        return super(DateTimeInput, self).render(name, value, attrs)
    303303
     304class TimeInput(Input):
     305    input_type = 'text'
     306
     307    def render(self, name, value, attrs=None):
     308        if value is None:
     309            value = ''
     310        elif hasattr(value, 'replace'):
     311            value = value.replace(microsecond=0)
     312        return super(TimeInput, self).render(name, value, attrs)
     313
    304314class CheckboxInput(Widget):
    305315    def __init__(self, attrs=None, check_test=bool):
    306316        super(CheckboxInput, self).__init__(attrs)
  • tests/regressiontests/forms/widgets.py

     
    10871087u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
    10881088>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    10891089u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
     1090
     1091# TimeInput ###############################################################
     1092
     1093>>> w = TimeInput()
     1094>>> w.render('time', None)
     1095u'<input type="text" name="time" />'
     1096>>> t = datetime.time(12, 51, 34, 482548)
     1097>>> print t
     109812:51:34.482548
     1099
     1100The microseconds are trimmed on display, by default.
     1101>>> w.render('time', t)
     1102u'<input type="text" name="time" value="12:51:34" />'
     1103>>> w.render('time', datetime.time(12, 51, 34))
     1104u'<input type="text" name="time" value="12:51:34" />'
     1105>>> w.render('time', datetime.time(12, 51))
     1106u'<input type="text" name="time" value="12:51:00" />'
    10901107"""
     1108
Back to Top