Commit 1311964f authored by Xiaowu Zhang's avatar Xiaowu Zhang

Formulator DateTimeFiled: support list input style

parent 6fc417d0
...@@ -1390,6 +1390,17 @@ class DateTimeWidget(Widget): ...@@ -1390,6 +1390,17 @@ class DateTimeWidget(Widget):
input_order = 'my' input_order = 'my'
return input_order return input_order
def create_items(self, start, end, digits=0):
result = [("-", "")]
if digits:
format_string = "%0" + str(digits) + "d"
else:
format_string = "%s"
for i in range(start, end):
s = format_string % i
result.append((s, s))
return result
def render_dict(self, field, value, render_prefix=None): def render_dict(self, field, value, render_prefix=None):
""" """
This is yet another field rendering. It is designed to allow code to This is yet another field rendering. It is designed to allow code to
...@@ -1426,13 +1437,13 @@ class DateTimeWidget(Widget): ...@@ -1426,13 +1437,13 @@ class DateTimeWidget(Widget):
if (value in (None, '')) and (field.get_value('default_now')) and \ if (value in (None, '')) and (field.get_value('default_now')) and \
((REQUEST is None) or (not REQUEST.form.has_key('subfield_%s_%s' % (key, 'year')))): ((REQUEST is None) or (not REQUEST.form.has_key('subfield_%s_%s' % (key, 'year')))):
value = DateTime() value = DateTime()
year = None year = ""
month = None month = ""
day = None day = ""
hour = None hour = ""
minute = None minute = ""
ampm = None ampm = ""
timezone = None timezone = ""
if isinstance(value, DateTime): if isinstance(value, DateTime):
year = "%04d" % value.year() year = "%04d" % value.year()
month = "%02d" % value.month() month = "%02d" % value.month()
...@@ -1498,27 +1509,47 @@ class DateTimeWidget(Widget): ...@@ -1498,27 +1509,47 @@ class DateTimeWidget(Widget):
return date_result return date_result
def render_sub_field(self, field, key, name, value, size, maxlength, REQUEST): def render_sub_field(self, field, key, name, value, size, maxlength, REQUEST):
if value is None: if name in ('hour', 'minute'):
value = "" return render_element("input", type="text",
if name in ('year', 'month', 'day', 'hour', 'minute', 'ampm'): name= field.generate_subfield_key(name, key=key),
value=value, size=size, maxlength=maxlength)
if name in ('year', 'month', 'day', 'ampm'):
style = field.get_value('input_style')
if style == 'text':
return render_element("input", type="text", return render_element("input", type="text",
name= field.generate_subfield_key(name, key=key), name= field.generate_subfield_key(name, key=key),
value=value, size=size, maxlength=maxlength) value=value, size=size, maxlength=maxlength)
if name == 'year':
items = self.create_items(int(value)-5, int(value)+5, digits=4)
elif name == 'month':
items = self.create_items(1, 13, digits=2)
elif name == 'day':
items = self.create_items(1, 32, digits=2)
else: else:
items =[("am","am"), ("pm","pm")]
rendered_items = self.render_sub_list(field, value, items, REQUEST)
return render_element('select',
name= field.generate_subfield_key(name, key=key),
css_class=field.get_value('css_class', REQUEST=REQUEST),
size=1,
contents=string.join(rendered_items, "\n"))
gmt_timezones = [('GMT%s' %zone, 'GMT%s' %zone,) for zone in range(-12, 0)]\ gmt_timezones = [('GMT%s' %zone, 'GMT%s' %zone,) for zone in range(-12, 0)]\
+ [('GMT', 'GMT',),] \ + [('GMT', 'GMT',),] \
+ [('GMT+%s' %zone, 'GMT+%s' %zone,) for zone in range(1, 13)] + [('GMT+%s' %zone, 'GMT+%s' %zone,) for zone in range(1, 13)]
value = value.replace("UTC", "GMT")
rendered_items = self.render_sub_list(field, value, gmt_timezones, REQUEST) rendered_items = self.render_sub_list(field, value, gmt_timezones, REQUEST)
return render_element('select', return render_element('select',
name= field.generate_subfield_key(name, key=key), name= field.generate_subfield_key(name, key=key),
css_class=field.get_value('css_class', REQUEST=REQUEST), css_class=field.get_value('css_class', REQUEST=REQUEST),
size=1, size=1,
contents=string.join(rendered_items, "\n")) contents=string.join(rendered_items, "\n"))
def render_sub_list(self, field, value, items, REQUEST): def render_sub_list(self, field, value, items, REQUEST):
selected_found = 0 selected_found = 0
rendered_items = [] rendered_items = []
value = value.replace("UTC", "GMT")
for item in items: for item in items:
try: try:
item_text, item_value = item item_text, item_value = item
...@@ -1532,7 +1563,6 @@ class DateTimeWidget(Widget): ...@@ -1532,7 +1563,6 @@ class DateTimeWidget(Widget):
rendered_item = render_element('option', contents=item_text, value=item_value) rendered_item = render_element('option', contents=item_text, value=item_value)
rendered_items.append(rendered_item) rendered_items.append(rendered_item)
return rendered_items return rendered_items
def format_value(self, field, value, mode='html'): def format_value(self, field, value, mode='html'):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment