Ticket #7499: time_input_widget.diff

File time_input_widget.diff, 3.7 KB (added by Kevin McConnell, 16 years ago)

Adds a TimeInput widget

  • 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    format = '%H:%M:%S'     # '14:30:59'
     307
     308    def __init__(self, attrs=None, format=None):
     309        super(TimeInput, self).__init__(attrs)
     310        if format:
     311            self.format = format
     312
     313    def render(self, name, value, attrs=None):
     314        if value is None:
     315            value = ''
     316        elif hasattr(value, 'strftime'):
     317            value = value.strftime(self.format)
     318        return super(TimeInput, self).render(name, value, attrs)
     319
    304320class CheckboxInput(Widget):
    305321    def __init__(self, attrs=None, check_test=bool):
    306322        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