Ticket #3672: date-time-widgets-tests.diff

File date-time-widgets-tests.diff, 4.4 KB (added by scott@…, 17 years ago)

Widgets (slightly remodeled) and tests as a diff

  • django/newforms/widgets.py

     
    77except NameError:
    88    from sets import Set as set   # Python 2.3 fallback
    99
     10import datetime
    1011from itertools import chain
     12from django.conf import settings
     13from django.utils import dateformat
    1114from django.utils.datastructures import MultiValueDict
    1215from django.utils.html import escape
    1316from django.utils.translation import ugettext
     
    1518from util import flatatt
    1619
    1720__all__ = (
    18     'Widget', 'TextInput', 'PasswordInput',
     21    'Widget', 'TextInput', 'DateTextInput', 'TimeTextInput', 'PasswordInput',
    1922    'HiddenInput', 'MultipleHiddenInput',
    2023    'FileInput', 'Textarea', 'CheckboxInput',
    2124    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
     
    8487class TextInput(Input):
    8588    input_type = 'text'
    8689
     90class DateTextInput(TextInput):
     91    """Renders value in text input using default or specified date format."""
     92    def __init__(self, format=None, attrs=None):
     93        super(DateTextInput, self).__init__(attrs)
     94        self.format = format or settings.DATE_FORMAT
     95
     96    def render(self, name, value, attrs=None):
     97        if value and (isinstance(value, datetime.date) or isinstance(value, datetime.datetime)):
     98            formatted_value = dateformat.format(value, self.format)
     99        else:
     100            formatted_value = None
     101        return super(DateTextInput, self).render(name, formatted_value, attrs)
     102
     103class TimeTextInput(TextInput):
     104    """Renders time in text input using default or specified time format."""
     105    def __init__(self, format=None, attrs=None):
     106        super(TimeTextInput, self).__init__(attrs)
     107        self.format = format or settings.TIME_FORMAT
     108
     109    def render(self, name, value, attrs=None):
     110        if value and (isinstance(value, datetime.time) or isinstance(value, datetime.datetime)):
     111            formatted_value = dateformat.time_format(value, self.format)
     112        else:
     113            formatted_value = None
     114        return super(TimeTextInput, self).render(name, formatted_value, attrs)
     115
    87116class PasswordInput(Input):
    88117    input_type = 'password'
    89118
  • tests/regressiontests/forms/tests.py

     
    5353>>> w.render('email', '', attrs={'class': 'special'})
    5454u'<input type="text" class="special" name="email" />'
    5555
     56# DateTextInput Widget ############################################################
     57
     58>>> w = DateTextInput(format='d F Y')
     59>>> w.render('date', '')
     60u'<input type="text" name="date" />'
     61>>> w.render('date', 'bogus')
     62u'<input type="text" name="date" />'
     63>>> w.render('date', datetime.date(2007, 9, 1))
     64u'<input type="text" name="date" value="01 September 2007" />'
     65>>> w.render('date', datetime.datetime(2007, 1, 9, 22, 11))
     66u'<input type="text" name="date" value="09 January 2007" />'
     67>>> w = DateTextInput(format='N j, Y', attrs={'class': 'fun'})
     68>>> w.render('date', datetime.date(2007, 9, 1))
     69u'<input type="text" class="fun" value="Sept. 1, 2007" name="date" />'
     70>>> w.render('date', datetime.datetime(2007, 1, 9, 11, 22))
     71u'<input type="text" class="fun" value="Jan. 9, 2007" name="date" />'
     72
     73# TimeTextInput Widget ############################################################
     74
     75>>> w = TimeTextInput(format='H:i:s')
     76>>> w.render('time', '')
     77u'<input type="text" name="time" />'
     78>>> w.render('time', 'bogus')
     79u'<input type="text" name="time" />'
     80>>> w.render('time', datetime.time(11, 22, 33))
     81u'<input type="text" name="time" value="11:22:33" />'
     82>>> w.render('time', datetime.time(21, 32))
     83u'<input type="text" name="time" value="21:32:00" />'
     84>>> w = TimeTextInput(format='P', attrs={'class': 'shiny'})
     85>>> w.render('time', datetime.datetime(2007, 1, 9, 11, 22, 33))
     86u'<input type="text" class="shiny" value="11:22 a.m." name="time" />'
     87>>> w.render('time', datetime.datetime(2007, 1, 9, 21, 32))
     88u'<input type="text" class="shiny" value="9:32 p.m." name="time" />'
     89>>> w.render('time', datetime.datetime(2007, 1, 9))
     90u'<input type="text" class="shiny" value="midnight" name="time" />'
     91
    5692# PasswordInput Widget ############################################################
    5793
    5894>>> w = PasswordInput()
Back to Top