Commit 198dd2e8 authored by Xiaowu Zhang's avatar Xiaowu Zhang

DateTimeValidator: improve validator

1: when field in not present in request, raise error
2: remove duplicate validate for ampm
parent d71c4187
...@@ -280,9 +280,9 @@ class IntegerValidator(StringBaseValidator): ...@@ -280,9 +280,9 @@ class IntegerValidator(StringBaseValidator):
self.raise_error('not_integer', field) self.raise_error('not_integer', field)
start = "" start = ""
end = "" end = ""
if hasattr(field, start): if field.has_value('start'):
start = field.get_value('start') start = field.get_value('start')
if hasattr(field, end): if field.has_value('end'):
end = field.get_value('end') end = field.get_value('end')
if start != "" and value < start: if start != "" and value < start:
self.raise_error('integer_out_of_range', field) self.raise_error('integer_out_of_range', field)
...@@ -739,13 +739,11 @@ class DateTimeValidator(Validator): ...@@ -739,13 +739,11 @@ 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": if name in ('timezone', 'ampm'):
return REQUEST.get(id) value = REQUEST.get(id)
if name == "ampm": if value is None:
ampm = REQUEST.get(id) raise KeyError, 'Field %s is not present in request object.' % (repr(id), )
if ampm in ('am', 'pm'): return value
return ampm
raise ValidationError('not_datetime', field)
return IntegerValidatorInstance.validate(field, id, REQUEST) return IntegerValidatorInstance.validate(field, id, REQUEST)
def validate(self, field, key, REQUEST): def validate(self, field, key, REQUEST):
...@@ -792,22 +790,19 @@ class DateTimeValidator(Validator): ...@@ -792,22 +790,19 @@ class DateTimeValidator(Validator):
self.raise_error('not_datetime', field) self.raise_error('not_datetime', field)
if field.get_value('ampm_time_style'): if field.get_value('ampm_time_style'):
try: ampm = self.validate_sub_field(field, key, 'ampm', REQUEST)
ampm = self.validate_sub_field(field, key, 'ampm', REQUEST) if field.get_value('allow_empty_time'):
if field.get_value('allow_empty_time'): if ampm == '':
if ampm == '': ampm = 'am'
ampm = 'am' hour = int(hour)
hour = int(hour) # handling not am or pm
# handling not am or pm # handling hour > 12
# handling hour > 12 if ((ampm != 'am') and (ampm != 'pm')) or (hour > 12):
if ((ampm != 'am') and (ampm != 'pm')) or (hour > 12): self.raise_error('not_datetime', field)
self.raise_error('not_datetime', field) if (ampm == 'pm') and (hour == 0):
if (ampm == 'pm') and (hour == 0): self.raise_error('not_datetime', field)
self.raise_error('not_datetime', field) elif ampm == 'pm' and hour < 12:
elif ampm == 'pm' and hour < 12: hour += 12
hour += 12
except ValidationError:
self.raise_error('not_datetime', field)
# handle possible timezone input # handle possible timezone input
timezone = '' timezone = ''
......
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