Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[soc2009/i18n] Fixed #6231. SelectDateWidget uses localized DATE_FORM…

…AT setting.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/i18n-improvements@11412 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b6ceeb15958958587df8ee600c026b2251e9c4e0 1 parent b6d352c
@garcia-marc garcia-marc authored
Showing with 34 additions and 31 deletions.
  1. +29 −31 django/forms/extras/widgets.py
  2. +5 −0 tests/regressiontests/i18n/tests.py
View
60 django/forms/extras/widgets.py
@@ -8,6 +8,7 @@
from django.forms.widgets import Widget, Select
from django.utils.dates import MONTHS
from django.utils.safestring import mark_safe
+from django.utils.formats import getformat
__all__ = ('SelectDateWidget',)
@@ -45,38 +46,22 @@ def render(self, name, value, attrs=None):
if match:
year_val, month_val, day_val = [int(v) for v in match.groups()]
- output = []
-
- if 'id' in self.attrs:
- id_ = self.attrs['id']
- else:
- id_ = 'id_%s' % name
-
- month_choices = MONTHS.items()
- if not (self.required and value):
- month_choices.append(self.none_value)
- month_choices.sort()
- local_attrs = self.build_attrs(id=self.month_field % id_)
- s = Select(choices=month_choices)
- select_html = s.render(self.month_field % name, month_val, local_attrs)
- output.append(select_html)
-
- day_choices = [(i, i) for i in range(1, 32)]
- if not (self.required and value):
- day_choices.insert(0, self.none_value)
- local_attrs['id'] = self.day_field % id_
- s = Select(choices=day_choices)
- select_html = s.render(self.day_field % name, day_val, local_attrs)
- output.append(select_html)
-
- year_choices = [(i, i) for i in self.years]
- if not (self.required and value):
- year_choices.insert(0, self.none_value)
- local_attrs['id'] = self.year_field % id_
- s = Select(choices=year_choices)
- select_html = s.render(self.year_field % name, year_val, local_attrs)
- output.append(select_html)
+ choices = [(i, i) for i in self.years]
+ year_html = self.create_select(name, self.year_field, value, year_val, choices)
+ choices = MONTHS.items()
+ month_html = self.create_select(name, self.month_field, value, month_val, choices)
+ choices = [(i, i) for i in range(1, 32)]
+ day_html = self.create_select(name, self.day_field, value, day_val, choices)
+ format = getformat('DATE_FORMAT')
+ output = []
+ for char in format:
+ if char in 'Yy':
+ output.append(year_html)
+ elif char in 'bFMmNn':
+ output.append(month_html)
+ elif char in 'dj':
+ output.append(day_html)
return mark_safe(u'\n'.join(output))
def id_for_label(self, id_):
@@ -92,3 +77,16 @@ def value_from_datadict(self, data, files, name):
if y and m and d:
return '%s-%s-%s' % (y, m, d)
return data.get(name, None)
+
+ def create_select(self, name, field, value, val, choices):
+ if 'id' in self.attrs:
+ id_ = self.attrs['id']
+ else:
+ id_ = 'id_%s' % name
+ if not (self.required and value):
+ choices.insert(0, self.none_value)
+ local_attrs = self.build_attrs(id=field % id_)
+ s = Select(choices=choices)
+ select_html = s.render(field % name, val, local_attrs)
+ return select_html
+
View
5 tests/regressiontests/i18n/tests.py
@@ -72,6 +72,7 @@
>>> from django.utils.formats import getformat, date_format, number_format, localize
>>> from django import template
>>> from django import forms
+>>> from django.forms.extras import SelectDateWidget
>>> n = decimal.Decimal('66666.666')
>>> f = 99999.999
@@ -144,6 +145,8 @@
datetime.datetime(2009, 12, 31, 20, 50)
>>> form.cleaned_data['time_field']
datetime.time(20, 50)
+>>> SelectDateWidget().render('mydate', datetime.date(2009, 12, 31))
+u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12" selected="selected">December</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>'
Catalan locale
@@ -199,6 +202,8 @@
datetime.datetime(2009, 12, 31, 20, 50)
>>> form.cleaned_data['time_field']
datetime.time(20, 50)
+>>> SelectDateWidget().render('mydate', datetime.date(2009, 12, 31))
+u'<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_month" id="id_mydate_month">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12" selected="selected">December</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>'
"""
Please sign in to comment.
Something went wrong with that request. Please try again.