Ticket #3533: dateortime_widget.patch

File dateortime_widget.patch, 4.2 KB (added by Chris Beaven, 18 years ago)

with tests

  • django/newforms/widgets.py

     
    44
    55__all__ = (
    66    'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'MultipleHiddenInput',
    7     'FileInput', 'Textarea', 'CheckboxInput',
     7    'FileInput', 'Textarea', 'CheckboxInput', 'DateOrTimeTextInput',
    88    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple',
    99    'MultiWidget', 'SplitDateTimeWidget',
    1010)
     
    1414from django.utils.html import escape
    1515from django.utils.translation import gettext
    1616from itertools import chain
     17from datetime import date, datetime, time
    1718
    1819try:
    1920    set # Only available in Python 2.4+
     
    116117class FileInput(Input):
    117118    input_type = 'file'
    118119
     120class 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
    119133class Textarea(Widget):
    120134    def render(self, name, value, attrs=None):
    121135        if value is None: value = ''
     
    343357    """
    344358    A Widget that splits datetime input into two <input type="text"> boxes.
    345359    """
    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))
    348363        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
    349364
    350365    def decompress(self, value):
  • tests/regressiontests/forms/tests.py

     
    186186>>> w.render('email', 'ŠĐĆŽćžšđ', attrs={'class': 'fun'})
    187187u'<input type="file" class="fun" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" name="email" />'
    188188
     189# DateOrTimeTextInput Widget ##################################################
     190
     191>>> test_time = datetime.datetime(2006, 1, 10, 13, 30)
     192
     193>>> w = DateOrTimeTextInput()
     194>>> w.render('date', None)
     195u'<input type="text" name="date" />'
     196>>> w.render('date', 'non-date')
     197u'<input type="text" name="date" value="non-date" />'
     198
     199Defaults to %Y-%m-%d
     200>>> w.render('date', test_time)
     201u'<input type="text" name="date" value="2006-01-10" />'
     202
     203Pass 'format' attribute to change the strftime when rendering.
     204>>> w = DateOrTimeTextInput(format='%m/%d/%Y')
     205>>> w.render('date', test_time)
     206u'<input type="text" name="date" value="01/10/2006" />'
     207>>> w = DateOrTimeTextInput(format='%H:%M:%S')
     208>>> w.render('time', test_time)
     209u'<input type="text" name="time" value="13:30:00" />'
     210
     211Works with time and date objects too
     212>>> w = DateOrTimeTextInput(format='%H:%M:%S')
     213>>> w.render('time', test_time.time())
     214u'<input type="text" name="time" value="13:30:00" />'
     215>>> w = DateOrTimeTextInput(format='%m/%d/%Y')
     216>>> w.render('date', test_time.date())
     217u'<input type="text" name="date" value="01/10/2006" />'
     218
    189219# Textarea Widget #############################################################
    190220
    191221>>> w = Textarea()
     
    787817>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
    788818u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
    789819
     820You 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))
     823u'<input type="text" name="date_0" value="01/10/2006" /><input type="text" name="date_1" value="7:30 AM" />'
     824
    790825##########
    791826# Fields #
    792827##########
Back to Top