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

File date-time-widgets-tests-2.diff, 4.4 KB (added by Adi J. Sieker, 16 years ago)

updated to work with trunk and use translated DATE_FORMAT and TIME_FORMAT as well

  • django/newforms/widgets.py

     
    88    from sets import Set as set   # Python 2.3 fallback
    99
    1010import copy
     11import datetime
    1112from itertools import chain
    1213
     14from django.conf import settings
     15from django.utils import dateformat
    1316from django.utils.datastructures import MultiValueDict
    1417from django.utils.html import escape, conditional_escape
    1518from django.utils.translation import ugettext
     
    1821from util import flatatt
    1922
    2023__all__ = (
    21     'Widget', 'TextInput', 'PasswordInput',
     24    'Widget', 'TextInput', 'DateTextInput', 'TimeTextInput', 'PasswordInput',
    2225    'HiddenInput', 'MultipleHiddenInput',
    2326    'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
    2427    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
     
    9598class TextInput(Input):
    9699    input_type = 'text'
    97100
     101class DateTextInput(TextInput):
     102    """Renders value in text input using default or specified date format."""
     103    def __init__(self, format=None, attrs=None):
     104        super(DateTextInput, self).__init__(attrs)
     105        self.format = format or (ugettext('DATE_FORMAT') or settings.DATE_FORMAT)
     106
     107    def render(self, name, value, attrs=None):
     108        if value and (isinstance(value, datetime.date) or isinstance(value, datetime.datetime)):
     109            formatted_value = dateformat.format(value, self.format)
     110        else:
     111            formatted_value = None
     112        return super(DateTextInput, self).render(name, formatted_value, attrs)
     113
     114class TimeTextInput(TextInput):
     115    """Renders time in text input using default or specified time format."""
     116    def __init__(self, format=None, attrs=None):
     117        super(TimeTextInput, self).__init__(attrs)
     118        self.format = format or (ugettext('TIME_FORMAT') or settings.TIMEFORMAT)
     119
     120    def render(self, name, value, attrs=None):
     121        if value and (isinstance(value, datetime.time) or isinstance(value, datetime.datetime)):
     122            formatted_value = dateformat.time_format(value, self.format)
     123        else:
     124            formatted_value = None
     125        return super(TimeTextInput, self).render(name, formatted_value, attrs)
     126
    98127class PasswordInput(Input):
    99128    input_type = 'password'
    100129
  • tests/regressiontests/forms/widgets.py

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