Ticket #12986: 12986-3.diff
File 12986-3.diff, 5.1 KB (added by , 15 years ago) |
---|
-
../django/forms/extras/widgets.py
13 13 14 14 __all__ = ('SelectDateWidget',) 15 15 16 RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')17 18 16 class SelectDateWidget(Widget): 19 17 """ 20 18 A Widget that splits date input into three <select> boxes. 21 19 22 20 This also serves as an example of a Widget that has more than one HTML 23 21 element and hence implements value_from_datadict. 22 23 If format localization is used (USE_L10N = True), combination of %Y, %m and %d directives in default 24 input format is needed for invalid dates to be rendered. 24 25 """ 25 26 none_value = (0, '---') 26 27 month_field = '%s_month' … … 36 37 else: 37 38 this_year = datetime.date.today().year 38 39 self.years = range(this_year, this_year+10) 39 40 40 41 def render(self, name, value, attrs=None): 41 42 try: 42 43 year_val, month_val, day_val = value.year, value.month, value.day 43 44 except AttributeError: 44 45 year_val = month_val = day_val = None 45 46 if isinstance(value, basestring): 46 match = RE_DATE.match(value) 47 if match: 48 year_val, month_val, day_val = [int(v) for v in match.groups()] 49 47 input_format = get_format('DATE_INPUT_FORMATS')[0] 48 try: 49 val = datetime.datetime.strptime(value, input_format) 50 year_val, month_val, day_val = val.year, val.month, val.day 51 except ValueError: 52 # When selected day is invalid in month convert input format to regex and receive year, day, month values 53 # so the failed date still can be rendered. 54 format_to_re_dic = { 55 '%Y': r'(?P<y>\d+)?', 56 '%m': r'(?P<m>\d+?)?', 57 '%d': r'(?P<d>\d+?)?' 58 } 59 re_str = input_format 60 for k, v in format_to_re_dic.iteritems(): 61 re_str = re_str.replace(k, v) 62 match = re.match('^' + re_str + '$', value) 63 if match: 64 year_val, month_val, day_val = int(match.group('y')), int(match.group('m')), int(match.group('d')) 50 65 choices = [(i, i) for i in self.years] 51 66 year_html = self.create_select(name, self.year_field, value, year_val, choices) 52 67 choices = MONTHS.items() … … 81 96 if y == m == d == "0": 82 97 return None 83 98 if y and m and d: 84 if settings.USE_L10N: 85 input_format = get_format('DATE_INPUT_FORMATS')[0] 86 try: 87 date_value = datetime.date(int(y), int(m), int(d)) 88 except ValueError: 89 pass 90 else: 91 return date_value.strftime(input_format) 92 else: 93 return '%s-%s-%s' % (y, m, d) 99 input_format = get_format('DATE_INPUT_FORMATS')[0] 100 return input_format.replace('%Y', y).replace('%m', m).replace('%d', d) 94 101 return data.get(name, None) 95 102 96 103 def create_select(self, name, field, value, val, choices): … … 98 105 id_ = self.attrs['id'] 99 106 else: 100 107 id_ = 'id_%s' % name 101 if not (self.required and value): 102 choices.insert(0, self.none_value) 108 choices.insert(0, self.none_value) 103 109 local_attrs = self.build_attrs(id=field % id_) 104 110 s = Select(choices=choices) 105 111 select_html = s.render(field % name, val, local_attrs) -
../tests/regressiontests/forms/extra.py
364 364 2008-04-01 365 365 366 366 367 USE_L10N tests 368 369 >>> from django.utils import translation 370 >>> translation.activate('nl') 371 >>> from django.conf import settings 372 >>> settings.USE_L10N=True 373 >>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') 374 '13-8-2010' 375 >>> res = w.render('date', '13-08-2010') 376 >>> u'<option value="13" selected="selected">13</option>' in res 377 True 378 >>> u'<option value="2010" selected="selected">2010</option>' in res 379 True 380 >>> u'<option value="8" selected="selected">augustus</option>' in res 381 True 382 >>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') 383 '13-8-2010' 384 >>> res = w.render('date', '31-2-2010') 385 >>> u'<option value="31" selected="selected">31</option>' in res 386 True 387 >>> u'<option value="2010" selected="selected">2010</option>' in res 388 True 389 >>> u'<option value="2" selected="selected">februari</option>' in res 390 True 391 >>> translation.deactivate() 392 367 393 # MultiWidget and MultiValueField ############################################# 368 394 # MultiWidgets are widgets composed of other widgets. They are usually 369 395 # combined with MultiValueFields - a field that is composed of other fields.