Ticket #3672: date-time-widgets-tests.diff
File date-time-widgets-tests.diff, 4.4 KB (added by , 17 years ago) |
---|
-
django/newforms/widgets.py
7 7 except NameError: 8 8 from sets import Set as set # Python 2.3 fallback 9 9 10 import datetime 10 11 from itertools import chain 12 from django.conf import settings 13 from django.utils import dateformat 11 14 from django.utils.datastructures import MultiValueDict 12 15 from django.utils.html import escape 13 16 from django.utils.translation import ugettext … … 15 18 from util import flatatt 16 19 17 20 __all__ = ( 18 'Widget', 'TextInput', ' PasswordInput',21 'Widget', 'TextInput', 'DateTextInput', 'TimeTextInput', 'PasswordInput', 19 22 'HiddenInput', 'MultipleHiddenInput', 20 23 'FileInput', 'Textarea', 'CheckboxInput', 21 24 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', … … 84 87 class TextInput(Input): 85 88 input_type = 'text' 86 89 90 class 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 103 class 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 87 116 class PasswordInput(Input): 88 117 input_type = 'password' 89 118 -
tests/regressiontests/forms/tests.py
53 53 >>> w.render('email', '', attrs={'class': 'special'}) 54 54 u'<input type="text" class="special" name="email" />' 55 55 56 # DateTextInput Widget ############################################################ 57 58 >>> w = DateTextInput(format='d F Y') 59 >>> w.render('date', '') 60 u'<input type="text" name="date" />' 61 >>> w.render('date', 'bogus') 62 u'<input type="text" name="date" />' 63 >>> w.render('date', datetime.date(2007, 9, 1)) 64 u'<input type="text" name="date" value="01 September 2007" />' 65 >>> w.render('date', datetime.datetime(2007, 1, 9, 22, 11)) 66 u'<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)) 69 u'<input type="text" class="fun" value="Sept. 1, 2007" name="date" />' 70 >>> w.render('date', datetime.datetime(2007, 1, 9, 11, 22)) 71 u'<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', '') 77 u'<input type="text" name="time" />' 78 >>> w.render('time', 'bogus') 79 u'<input type="text" name="time" />' 80 >>> w.render('time', datetime.time(11, 22, 33)) 81 u'<input type="text" name="time" value="11:22:33" />' 82 >>> w.render('time', datetime.time(21, 32)) 83 u'<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)) 86 u'<input type="text" class="shiny" value="11:22 a.m." name="time" />' 87 >>> w.render('time', datetime.datetime(2007, 1, 9, 21, 32)) 88 u'<input type="text" class="shiny" value="9:32 p.m." name="time" />' 89 >>> w.render('time', datetime.datetime(2007, 1, 9)) 90 u'<input type="text" class="shiny" value="midnight" name="time" />' 91 56 92 # PasswordInput Widget ############################################################ 57 93 58 94 >>> w = PasswordInput()