Commit 8a9ce166 authored by Xiaowu Zhang's avatar Xiaowu Zhang

Formulator DateTimeField: Do not use sub form to generate date and time

parent 6faa1b16
......@@ -127,6 +127,7 @@ class DateTimeField(ZMIField):
widget = Widget.DateTimeWidgetInstance
validator = Validator.DateTimeValidatorInstance
"""
def __init__(self, id, **kw):
# icky but necessary...
apply(ZMIField.__init__, (self, id), kw)
......@@ -160,10 +161,7 @@ class DateTimeField(ZMIField):
for field in self.sub_form.get_fields():
field.values['css_class'] = value
field._p_changed = 1
def override_year_items(self):
"""The method gets called to get the right amount of years.
"""
start_datetime = self.get_value('start_datetime')
end_datetime = self.get_value('end_datetime')
current_year = DateTime().year()
......@@ -176,7 +174,7 @@ class DateTimeField(ZMIField):
else:
last_year = first_year + 11
return create_items(first_year, last_year, digits=4)
"""
def _get_user_input_value(self, key, REQUEST):
"""
Try to get a value of the field from the REQUEST
......
......@@ -278,8 +278,11 @@ class IntegerValidator(StringBaseValidator):
value = int(value)
except ValueError:
self.raise_error('not_integer', field)
start = ""
end = ""
if hasattr(field, start):
start = field.get_value('start')
if hasattr(field, end):
end = field.get_value('end')
if start != "" and value < start:
self.raise_error('integer_out_of_range', field)
......@@ -734,29 +737,33 @@ class DateTimeValidator(Validator):
not_datetime = 'You did not enter a valid date and time.'
datetime_out_of_range = 'The date and time you entered were out of range.'
def validate_sub_field(self, field, key, name, REQUEST):
id = field.generate_subfield_key(name, validation = 1, key = key)
return IntegerValidatorInstance.validate(field, id, REQUEST)
def validate(self, field, key, REQUEST):
try:
year = field.validate_sub_field('year', REQUEST, key=key)
month = field.validate_sub_field('month', REQUEST, key=key)
year = self.validate_sub_field(field, key, 'year', REQUEST)
month = self.validate_sub_field(field, key, 'month', REQUEST)
if field.get_value('hide_day'):
day = 1
else:
day = field.validate_sub_field('day', REQUEST, key=key)
day = self.validate_sub_field(field, key, 'day', REQUEST)
if field.get_value('date_only'):
hour = 0
minute = 0
elif field.get_value('allow_empty_time'):
hour = field.validate_sub_field('hour', REQUEST, key=key)
minute = field.validate_sub_field('minute', REQUEST, key=key)
hour = self.validate_sub_field(field, key, 'hour', REQUEST)
minute = self.validate_sub_field(field, key, 'minute', REQUEST)
if hour == '' and minute == '':
hour = 0
minute = 0
elif hour == '' or minute == '':
raise ValidationError('not_datetime', field)
else:
hour = field.validate_sub_field('hour', REQUEST, key=key)
minute = field.validate_sub_field('minute', REQUEST, key=key)
hour = self.validate_sub_field(field, key, 'hour', REQUEST)
minute = self.validate_sub_field(field, key, 'minute', REQUEST)
except ValidationError:
self.raise_error('not_datetime', field)
......@@ -778,7 +785,7 @@ class DateTimeValidator(Validator):
self.raise_error('not_datetime', field)
if field.get_value('ampm_time_style'):
ampm = field.validate_sub_field('ampm', REQUEST, key=key)
ampm = self.validate_sub_field(field, key, 'ampm', REQUEST)
if field.get_value('allow_empty_time'):
if ampm == '':
ampm = 'am'
......@@ -794,9 +801,10 @@ class DateTimeValidator(Validator):
# handle possible timezone input
timezone = ''
"""
if field.get_value('timezone_style'):
timezone = field.validate_sub_field('timezone', REQUEST, key=key)
"""
try:
# handling of hidden day, which can be first or last day of the month:
if field.get_value('hidden_day_is_last_day'):
......
......@@ -1417,10 +1417,6 @@ class DateTimeWidget(Widget):
def render(self, field, key, value, REQUEST, render_prefix=None):
use_ampm = field.get_value('ampm_time_style')
use_timezone = field.get_value('timezone_style')
# FIXME: backwards compatibility hack:
if not hasattr(field, 'sub_form'):
from StandardFields import create_datetime_text_sub_form
field.sub_form = create_datetime_text_sub_form()
# Is it still usefull to test the None value,
# as DateTimeField should be considerer as the other field
......@@ -1428,8 +1424,7 @@ class DateTimeWidget(Widget):
# XXX hasattr(REQUEST, 'form') seems useless,
# because REQUEST always has a form property
if (value in (None, '')) and (field.get_value('default_now')) and \
((REQUEST is None) or (not hasattr(REQUEST, 'form')) 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()
year = None
month = None
......@@ -1449,48 +1444,64 @@ class DateTimeWidget(Widget):
minute = "%02d" % value.minute()
ampm = value.ampm()
timezone = value.timezone()
elif REQUEST is not None:
year = REQUEST.get(field.generate_subfield_key("year", key=key))
month = REQUEST.get(field.generate_subfield_key("month", key=key))
day = REQUEST.get(field.generate_subfield_key("day", key=key))
hour = REQUEST.get(field.generate_subfield_key("hour", key=key))
minute = REQUEST.get(field.generate_subfield_key("minute", key=key))
ampm = REQUEST.get(field.generate_subfield_key("ampm", key=key))
timezone = REQUEST.get(field.generate_subfield_key("timezone", key=key))
input_order = self.getInputOrder(field)
if input_order == 'ymd':
order = [('year', year),
('month', month),
('day', day)]
order = [['year', year, 4, 4],
['month', month, 2, 2],
['day', day, 2, 2]]
elif input_order == 'dmy':
order = [('day', day),
('month', month),
('year', year)]
order = [['day', day, 2, 2],
['month', month, 2, 2],
['year', year, 4, 4]]
elif input_order == 'mdy':
order = [('month', month),
('day', day),
('year', year)]
order = [['month', month, 2, 2],
['day', day, 2, 2],
['year', year, 4, 4]]
elif input_order == 'my':
order = [('month', month),
('year', year)]
order = [['month', month, 2, 2],
['year', year, 4, 4]]
elif input_order == 'ym':
order = [('year', year),
('month', month)]
order = [['year', year, 4, 4],
['month', month, 2, 2]]
else:
order = [('year', year),
('month', month),
('day', day)]
order = [['year', year, 4 ,4],
['month', month, 2, 2],
['day', day, 2, 2]]
result = []
for sub_field_name, sub_field_value in order:
result.append(field.render_sub_field(sub_field_name,
sub_field_value, REQUEST, key=key))
for sub_field_name, sub_field_value, size, maxlength in order:
result.append(self.render_sub_field(field, key, sub_field_name,
sub_field_value, size, maxlength, REQUEST))
date_result = string.join(result, field.get_value('date_separator'))
if not field.get_value('date_only'):
time_result = (field.render_sub_field('hour', hour, REQUEST, key=key) +
time_result = (self.render_sub_field(field, key, 'hour', hour, 2, 2, REQUEST) +
field.get_value('time_separator') +
field.render_sub_field('minute', minute, REQUEST, key=key))
self.render_sub_field(field, key, 'minute', minute, 2, 2, REQUEST))
if use_ampm:
time_result += '&nbsp;' + field.render_sub_field('ampm',
ampm, REQUEST, key=key)
time_result += '&nbsp;' + self.render_sub_field(field, key, 'ampm',
ampm, 2, 2, REQUEST)
"""
if use_timezone:
time_result += '&nbsp;' + field.render_sub_field('timezone',
timezone, REQUEST, key=key)
"""
return date_result + '&nbsp;&nbsp;&nbsp;' + time_result
else:
return date_result
def render_sub_field(self, field, key, name, value, size, maxlength, REQUEST):
if value is None:
value = ""
return render_element("input", type="text",
name= field.generate_subfield_key(name, key=key),
value=value, size=size, maxlength=maxlength)
def format_value(self, field, value, mode='html'):
# 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