Ticket #3672: date-time-widgets-tests-2.diff
File date-time-widgets-tests-2.diff, 4.4 KB (added by , 16 years ago) |
---|
-
django/newforms/widgets.py
8 8 from sets import Set as set # Python 2.3 fallback 9 9 10 10 import copy 11 import datetime 11 12 from itertools import chain 12 13 14 from django.conf import settings 15 from django.utils import dateformat 13 16 from django.utils.datastructures import MultiValueDict 14 17 from django.utils.html import escape, conditional_escape 15 18 from django.utils.translation import ugettext … … 18 21 from util import flatatt 19 22 20 23 __all__ = ( 21 'Widget', 'TextInput', ' PasswordInput',24 'Widget', 'TextInput', 'DateTextInput', 'TimeTextInput', 'PasswordInput', 22 25 'HiddenInput', 'MultipleHiddenInput', 23 26 'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput', 24 27 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', … … 95 98 class TextInput(Input): 96 99 input_type = 'text' 97 100 101 class 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 114 class 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 98 127 class PasswordInput(Input): 99 128 input_type = 'password' 100 129 -
tests/regressiontests/forms/widgets.py
50 50 >>> w.render('email', '', attrs={'class': 'special'}) 51 51 u'<input type="text" class="special" name="email" />' 52 52 53 # DateTextInput Widget ############################################################ 54 55 >>> w = DateTextInput(format='d F Y') 56 >>> w.render('date', '') 57 u'<input type="text" name="date" />' 58 >>> w.render('date', 'bogus') 59 u'<input type="text" name="date" />' 60 >>> w.render('date', datetime.date(2007, 9, 1)) 61 u'<input type="text" name="date" value="01 September 2007" />' 62 >>> w.render('date', datetime.datetime(2007, 1, 9, 22, 11)) 63 u'<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)) 66 u'<input type="text" class="fun" value="Sept. 1, 2007" name="date" />' 67 >>> w.render('date', datetime.datetime(2007, 1, 9, 11, 22)) 68 u'<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', '') 74 u'<input type="text" name="time" />' 75 >>> w.render('time', 'bogus') 76 u'<input type="text" name="time" />' 77 >>> w.render('time', datetime.time(11, 22, 33)) 78 u'<input type="text" name="time" value="11:22:33" />' 79 >>> w.render('time', datetime.time(21, 32)) 80 u'<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)) 83 u'<input type="text" class="shiny" value="11:22 a.m." name="time" />' 84 >>> w.render('time', datetime.datetime(2007, 1, 9, 21, 32)) 85 u'<input type="text" class="shiny" value="9:32 p.m." name="time" />' 86 >>> w.render('time', datetime.datetime(2007, 1, 9)) 87 u'<input type="text" class="shiny" value="midnight" name="time" />' 88 53 89 # PasswordInput Widget ############################################################ 54 90 55 91 >>> w = PasswordInput()