Fixed #5917 -- More error robustness in date parsing in SelectDateWidget, plus

keep the original date selected on redisplay, even if it was bogus (e.g. 31
Feb). Patch from SmileyChris.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7337 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-03-20 07:44:46 +00:00
parent 516102dc75
commit 675db84f23
2 changed files with 75 additions and 2 deletions

View File

@ -3,6 +3,7 @@ Extra HTML Widget classes
""" """
import datetime import datetime
import re
from django.newforms.widgets import Widget, Select from django.newforms.widgets import Widget, Select
from django.utils.dates import MONTHS from django.utils.dates import MONTHS
@ -10,6 +11,8 @@ from django.utils.safestring import mark_safe
__all__ = ('SelectDateWidget',) __all__ = ('SelectDateWidget',)
RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
class SelectDateWidget(Widget): class SelectDateWidget(Widget):
""" """
A Widget that splits date input into three <select> boxes. A Widget that splits date input into three <select> boxes.
@ -32,10 +35,13 @@ class SelectDateWidget(Widget):
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None):
try: try:
value = datetime.date(*map(int, value.split('-')))
year_val, month_val, day_val = value.year, value.month, value.day year_val, month_val, day_val = value.year, value.month, value.day
except (AttributeError, TypeError, ValueError): except AttributeError:
year_val = month_val = day_val = None year_val = month_val = day_val = None
if isinstance(value, basestring):
match = RE_DATE.match(value)
if match:
year_val, month_val, day_val = [int(v) for v in match.groups()]
output = [] output = []

View File

@ -144,6 +144,73 @@ True
<option value="2016">2016</option> <option value="2016">2016</option>
</select> </select>
Accepts a datetime or a string:
>>> w.render('mydate', datetime.date(2010, 4, 15)) == w.render('mydate', '2010-04-15')
True
Invalid dates still render the failed date:
>>> print w.render('mydate', '2010-02-31')
<select name="mydate_month" id="id_mydate_month">
<option value="1">January</option>
<option value="2" selected="selected">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="mydate_day" id="id_mydate_day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31" selected="selected">31</option>
</select>
<select name="mydate_year" id="id_mydate_year">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010" selected="selected">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
Using a SelectDateWidget in a form: Using a SelectDateWidget in a form:
>>> class GetDate(Form): >>> class GetDate(Form):