2006-12-25 04:22:38 +08:00
|
|
|
"""
|
|
|
|
Extra HTML Widget classes
|
|
|
|
"""
|
|
|
|
|
2010-05-03 23:30:11 +08:00
|
|
|
import time
|
2007-05-17 05:20:35 +08:00
|
|
|
import datetime
|
2008-03-20 15:44:46 +08:00
|
|
|
import re
|
2007-05-17 05:20:35 +08:00
|
|
|
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms.widgets import Widget, Select
|
2010-05-23 18:38:23 +08:00
|
|
|
from django.utils import datetime_safe
|
2006-12-25 04:22:38 +08:00
|
|
|
from django.utils.dates import MONTHS
|
2007-11-29 04:19:58 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2009-12-23 01:58:49 +08:00
|
|
|
from django.utils.formats import get_format
|
|
|
|
from django.conf import settings
|
2006-12-25 04:22:38 +08:00
|
|
|
|
|
|
|
__all__ = ('SelectDateWidget',)
|
|
|
|
|
2008-03-20 15:44:46 +08:00
|
|
|
RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
|
|
|
|
|
2011-02-06 02:16:27 +08:00
|
|
|
def _parse_date_fmt():
|
|
|
|
fmt = get_format('DATE_FORMAT')
|
|
|
|
escaped = False
|
|
|
|
output = []
|
|
|
|
for char in fmt:
|
|
|
|
if escaped:
|
|
|
|
escaped = False
|
|
|
|
elif char == '\\':
|
|
|
|
escaped = True
|
|
|
|
elif char in 'Yy':
|
|
|
|
output.append('year')
|
|
|
|
#if not self.first_select: self.first_select = 'year'
|
|
|
|
elif char in 'bEFMmNn':
|
|
|
|
output.append('month')
|
|
|
|
#if not self.first_select: self.first_select = 'month'
|
|
|
|
elif char in 'dj':
|
|
|
|
output.append('day')
|
|
|
|
#if not self.first_select: self.first_select = 'day'
|
|
|
|
return output
|
|
|
|
|
2006-12-25 04:22:38 +08:00
|
|
|
class SelectDateWidget(Widget):
|
|
|
|
"""
|
|
|
|
A Widget that splits date input into three <select> boxes.
|
|
|
|
|
|
|
|
This also serves as an example of a Widget that has more than one HTML
|
|
|
|
element and hence implements value_from_datadict.
|
|
|
|
"""
|
2009-04-19 01:35:53 +08:00
|
|
|
none_value = (0, '---')
|
2006-12-25 04:22:38 +08:00
|
|
|
month_field = '%s_month'
|
|
|
|
day_field = '%s_day'
|
|
|
|
year_field = '%s_year'
|
|
|
|
|
2009-04-19 01:35:53 +08:00
|
|
|
def __init__(self, attrs=None, years=None, required=True):
|
2006-12-25 04:22:38 +08:00
|
|
|
# years is an optional list/tuple of years to use in the "year" select box.
|
|
|
|
self.attrs = attrs or {}
|
2009-04-19 01:35:53 +08:00
|
|
|
self.required = required
|
2006-12-25 04:22:38 +08:00
|
|
|
if years:
|
|
|
|
self.years = years
|
|
|
|
else:
|
|
|
|
this_year = datetime.date.today().year
|
|
|
|
self.years = range(this_year, this_year+10)
|
|
|
|
|
|
|
|
def render(self, name, value, attrs=None):
|
|
|
|
try:
|
|
|
|
year_val, month_val, day_val = value.year, value.month, value.day
|
2008-03-20 15:44:46 +08:00
|
|
|
except AttributeError:
|
2006-12-25 04:22:38 +08:00
|
|
|
year_val = month_val = day_val = None
|
2008-03-20 15:44:46 +08:00
|
|
|
if isinstance(value, basestring):
|
2010-04-28 23:39:26 +08:00
|
|
|
if settings.USE_L10N:
|
|
|
|
try:
|
|
|
|
input_format = get_format('DATE_INPUT_FORMATS')[0]
|
2010-05-03 23:30:11 +08:00
|
|
|
# Python 2.4 compatibility:
|
|
|
|
# v = datetime.datetime.strptime(value, input_format)
|
2011-02-05 07:46:30 +08:00
|
|
|
# would be clearer, but datetime.strptime was added in
|
2010-05-03 23:30:11 +08:00
|
|
|
# Python 2.5
|
|
|
|
v = datetime.datetime(*(time.strptime(value, input_format)[0:6]))
|
2010-04-28 23:39:26 +08:00
|
|
|
year_val, month_val, day_val = v.year, v.month, v.day
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
match = RE_DATE.match(value)
|
|
|
|
if match:
|
|
|
|
year_val, month_val, day_val = [int(v) for v in match.groups()]
|
2009-12-23 01:58:49 +08:00
|
|
|
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)
|
2006-12-25 04:22:38 +08:00
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
output = []
|
2011-02-06 02:16:27 +08:00
|
|
|
for field in _parse_date_fmt():
|
|
|
|
if field == 'year':
|
2009-12-23 01:58:49 +08:00
|
|
|
output.append(year_html)
|
2011-02-06 02:16:27 +08:00
|
|
|
elif field == 'month':
|
2009-12-23 01:58:49 +08:00
|
|
|
output.append(month_html)
|
2011-02-06 02:16:27 +08:00
|
|
|
elif field == 'day':
|
2009-12-23 01:58:49 +08:00
|
|
|
output.append(day_html)
|
2007-11-29 04:19:58 +08:00
|
|
|
return mark_safe(u'\n'.join(output))
|
2006-12-25 04:22:38 +08:00
|
|
|
|
2008-03-18 22:20:43 +08:00
|
|
|
def id_for_label(self, id_):
|
2011-02-06 02:16:27 +08:00
|
|
|
first_select = None
|
|
|
|
field_list = _parse_date_fmt()
|
|
|
|
if field_list:
|
|
|
|
first_select = field_list[0]
|
|
|
|
if first_select is not None:
|
|
|
|
return '%s_%s' % (id_, first_select)
|
|
|
|
else:
|
|
|
|
return '%s_month' % id_
|
2008-03-18 22:20:43 +08:00
|
|
|
id_for_label = classmethod(id_for_label)
|
|
|
|
|
2007-08-06 21:58:56 +08:00
|
|
|
def value_from_datadict(self, data, files, name):
|
2009-04-19 01:35:53 +08:00
|
|
|
y = data.get(self.year_field % name)
|
|
|
|
m = data.get(self.month_field % name)
|
|
|
|
d = data.get(self.day_field % name)
|
|
|
|
if y == m == d == "0":
|
|
|
|
return None
|
2006-12-25 04:22:38 +08:00
|
|
|
if y and m and d:
|
2009-12-23 01:58:49 +08:00
|
|
|
if settings.USE_L10N:
|
|
|
|
input_format = get_format('DATE_INPUT_FORMATS')[0]
|
|
|
|
try:
|
|
|
|
date_value = datetime.date(int(y), int(m), int(d))
|
|
|
|
except ValueError:
|
2011-02-05 07:46:30 +08:00
|
|
|
return '%s-%s-%s' % (y, m, d)
|
2009-12-23 01:58:49 +08:00
|
|
|
else:
|
2010-05-23 18:38:23 +08:00
|
|
|
date_value = datetime_safe.new_date(date_value)
|
2009-12-23 01:58:49 +08:00
|
|
|
return date_value.strftime(input_format)
|
|
|
|
else:
|
|
|
|
return '%s-%s-%s' % (y, m, d)
|
2007-08-12 10:15:35 +08:00
|
|
|
return data.get(name, None)
|
2009-12-23 01:58:49 +08:00
|
|
|
|
|
|
|
def create_select(self, name, field, value, val, choices):
|
|
|
|
if 'id' in self.attrs:
|
|
|
|
id_ = self.attrs['id']
|
|
|
|
else:
|
|
|
|
id_ = 'id_%s' % name
|
2010-04-28 23:39:26 +08:00
|
|
|
if not (self.required and val):
|
2009-12-23 01:58:49 +08:00
|
|
|
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
|