Commit 6fc417d0 authored by Xiaowu Zhang's avatar Xiaowu Zhang

Formulator DateTimeField: support timezone

parent 8a9ce166
...@@ -127,188 +127,9 @@ class DateTimeField(ZMIField): ...@@ -127,188 +127,9 @@ class DateTimeField(ZMIField):
widget = Widget.DateTimeWidgetInstance widget = Widget.DateTimeWidgetInstance
validator = Validator.DateTimeValidatorInstance validator = Validator.DateTimeValidatorInstance
"""
def __init__(self, id, **kw):
# icky but necessary...
apply(ZMIField.__init__, (self, id), kw)
input_style = self.get_value('input_style')
if input_style == 'text':
self.sub_form = create_datetime_text_sub_form()
elif input_style == 'list':
self.sub_form = create_datetime_list_sub_form()
else:
assert 0, "Unknown input_style '%s'" % input_style
def on_value_input_style_changed(self, value):
if value == 'text':
self.sub_form = create_datetime_text_sub_form()
elif value == 'list':
self.sub_form = create_datetime_list_sub_form()
year_field = self.sub_form.get_field('year', include_disabled=1)
year_field.overrides['items'] = BoundMethod(self,
'override_year_items')
else:
assert 0, "Unknown input_style."
self.on_value_css_class_changed(self.values['css_class'])
def on_value_timezone_style_changed(self, value):
if value:
input_style = self.get_value('input_style')
self.on_value_input_style_changed(input_style)
def on_value_css_class_changed(self, value):
for field in self.sub_form.get_fields():
field.values['css_class'] = value
field._p_changed = 1
def override_year_items(self):
start_datetime = self.get_value('start_datetime')
end_datetime = self.get_value('end_datetime')
current_year = DateTime().year()
if start_datetime:
first_year = start_datetime.year()
else:
first_year = current_year
if end_datetime:
last_year = end_datetime.year() + 1
else:
last_year = first_year + 11
return create_items(first_year, last_year, digits=4)
"""
def _get_user_input_value(self, key, REQUEST): def _get_user_input_value(self, key, REQUEST):
""" """
Try to get a value of the field from the REQUEST Try to get a value of the field from the REQUEST
""" """
if REQUEST.form['subfield_%s_%s' % (key, 'year')]: if REQUEST.form['subfield_%s_%s' % (key, 'year')]:
return None return None
\ No newline at end of file
gmt_timezones = [('GMT%s' %zone, 'GMT%s' %zone,) for zone in range(-12, 0)]\
+ [('GMT', 'GMT',),] \
+ [('GMT+%s' %zone, 'GMT+%s' %zone,) for zone in range(1, 13)]
def create_datetime_text_sub_form():
sub_form = BasicForm()
year = IntegerField('year',
title="Year",
required=0,
display_width=4,
display_maxwidth=4,
max_length=4)
month = IntegerField('month',
title="Month",
required=0,
display_width=2,
display_maxwidth=2,
max_length=2)
day = IntegerField('day',
title="Day",
required=0,
display_width=2,
display_maxwidth=2,
max_length=2)
sub_form.add_group("date")
sub_form.add_fields([year, month, day], "date")
hour = IntegerField('hour',
title="Hour",
required=0,
display_width=2,
display_maxwidth=2,
max_length=2)
minute = IntegerField('minute',
title="Minute",
required=0,
display_width=2,
display_maxwidth=2,
max_length=2)
ampm = StringField('ampm',
title="am/pm",
required=0,
display_width=2,
display_maxwidth=2,
max_length=2)
timezone = ListField('timezone',
title = "Timezone",
required = 0,
default = 'GMT',
items = gmt_timezones,
size = 1)
sub_form.add_fields([hour, minute, ampm, timezone], "time")
return sub_form
def create_datetime_list_sub_form():
""" Patch Products.Formulator.StandardFields so we can add timezone subfield """
sub_form = BasicForm()
year = ListField('year',
title="Year",
required=0,
default="",
items=create_items(2000, 2010, digits=4),
size=1)
month = ListField('month',
title="Month",
required=0,
default="",
items=create_items(1, 13, digits=2),
size=1)
day = ListField('day',
title="Day",
required=0,
default="",
items=create_items(1, 32, digits=2),
size=1)
sub_form.add_group("date")
sub_form.add_fields([year, month, day], "date")
hour = IntegerField('hour',
title="Hour",
required=0,
display_width=2,
display_maxwidth=2,
max_length=2)
minute = IntegerField('minute',
title="Minute",
required=0,
display_width=2,
display_maxwidth=2,
max_length=2)
ampm = ListField('ampm',
title="am/pm",
required=0,
default="am",
items=[("am","am"),
("pm","pm")],
size=1)
timezone = ListField('timezone',
title = "Timezone",
required = 0,
default = 'GMT',
items = gmt_timezones,
size = 1)
sub_form.add_group("time")
sub_form.add_fields([hour, minute, ampm, timezone], "time")
return sub_form
def create_items(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
...@@ -739,6 +739,8 @@ class DateTimeValidator(Validator): ...@@ -739,6 +739,8 @@ class DateTimeValidator(Validator):
def validate_sub_field(self, field, key, name, REQUEST): def validate_sub_field(self, field, key, name, REQUEST):
id = field.generate_subfield_key(name, validation = 1, key = key) id = field.generate_subfield_key(name, validation = 1, key = key)
if name == "timezone":
return REQUEST.get(id)
return IntegerValidatorInstance.validate(field, id, REQUEST) return IntegerValidatorInstance.validate(field, id, REQUEST)
def validate(self, field, key, REQUEST): def validate(self, field, key, REQUEST):
...@@ -801,10 +803,10 @@ class DateTimeValidator(Validator): ...@@ -801,10 +803,10 @@ class DateTimeValidator(Validator):
# handle possible timezone input # handle possible timezone input
timezone = '' timezone = ''
"""
if field.get_value('timezone_style'): if field.get_value('timezone_style'):
timezone = field.validate_sub_field('timezone', REQUEST, key=key) timezone = self.validate_sub_field(field, key, 'timezone', REQUEST)
"""
try: try:
# handling of hidden day, which can be first or last day of the month: # handling of hidden day, which can be first or last day of the month:
if field.get_value('hidden_day_is_last_day'): if field.get_value('hidden_day_is_last_day'):
......
...@@ -1488,20 +1488,52 @@ class DateTimeWidget(Widget): ...@@ -1488,20 +1488,52 @@ class DateTimeWidget(Widget):
if use_ampm: if use_ampm:
time_result += ' ' + self.render_sub_field(field, key, 'ampm', time_result += ' ' + self.render_sub_field(field, key, 'ampm',
ampm, 2, 2, REQUEST) ampm, 2, 2, REQUEST)
"""
if use_timezone: if use_timezone:
time_result += ' ' + field.render_sub_field('timezone', time_result += ' ' + self.render_sub_field(field, key, 'timezone',
timezone, REQUEST, key=key) timezone, 2, 2, REQUEST)
"""
return date_result + '   ' + time_result return date_result + '   ' + time_result
else: else:
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 value is None:
value = "" value = ""
return render_element("input", type="text", if name in ('year', 'month', 'day', 'hour', 'minute', 'ampm'):
name= field.generate_subfield_key(name, key=key), return render_element("input", type="text",
value=value, size=size, maxlength=maxlength) name= field.generate_subfield_key(name, key=key),
value=value, size=size, maxlength=maxlength)
else:
gmt_timezones = [('GMT%s' %zone, 'GMT%s' %zone,) for zone in range(-12, 0)]\
+ [('GMT', 'GMT',),] \
+ [('GMT+%s' %zone, 'GMT+%s' %zone,) for zone in range(1, 13)]
rendered_items = self.render_sub_list(field, value, gmt_timezones, 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"))
def render_sub_list(self, field, value, items, REQUEST):
selected_found = 0
rendered_items = []
value = value.replace("UTC", "GMT")
for item in items:
try:
item_text, item_value = item
except ValueError:
item_text = item
item_value = item
if item_value == value and not selected_found:
rendered_item = render_element('option', contents=item_text, value=item_value, selected=1)
selected_found = 1
else:
rendered_item = render_element('option', contents=item_text, value=item_value)
rendered_items.append(rendered_item)
return rendered_items
def format_value(self, field, value, mode='html'): def format_value(self, field, value, mode='html'):
# Is it still usefull to test the None value, # Is it still usefull to test the None value,
......
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