Commit f17d1341 authored by Willem ter Berg's avatar Willem ter Berg

loosened datetime validation, now accepts purely date stirngs

parent 047b50b9
......@@ -35,23 +35,16 @@ def single_valued(key, data, errors, context):
def multi_valued(allow_duplicates=False):
"""
Defines and returns a function which ensures that a given value in a dictionary is a list which may or may not allow
duplicates.
Checks if a given value is a list, possibly allowing duplicates. If a single value is given it is converted into a
list of size 1.
:param allow_duplicates: bool, Whether or not to allow duplicates
:return: function, The new validation method
:param key: tuple, The key containing the value
:param data: dict, The data dictionary representing the package
:param errors: dict, The validation errors so far
:param context: dict, The current CKAN context
:return: The original, possibly modified, arguments
"""
def multi_valued_validator(key, data, errors, context):
"""
Checks if a given value is a list, possibly allowing duplicates. If a single value is given it is converted into
a list of size 1.
:param key: tuple, The key containing the value
:param data: dict, The data dictionary representing the package
:param errors: dict, The validation errors so far
:param context: dict, The current CKAN context
:return: The original, possibly modified, arguments
"""
convert_string_to_list = tk.get_validator('convert_string_to_list')
key, data, errors, context = convert_string_to_list(key, data, errors, context)
errors[key] = [] if not errors[key] else errors[key]
......@@ -175,15 +168,21 @@ def uri(key, data, errors, context):
def date(datetime_format):
"""
Checks if a given value is a datetime or a list of datetimes.
Creates a function which validates datetime objects based on the given datetime_format.
:param key: tuple, The key containing the value
:param data: dict, The data dictionary representing the package
:param errors: dict, The validation errors so far
:param context: dict, The current CKAN context
:return: The original, possibly modified, arguments
:param datetime_format: The datetime_format to use for validation
:return: function, the validation function
"""
def valid_date(key, data, errors, context):
"""
Checks if a given value is a datetime or a list of datetimes.
:param key: tuple, The key containing the value
:param data: dict, The data dictionary representing the package
:param errors: dict, The validation errors so far
:param context: dict, The current CKAN context
:return: The original, possibly modified, arguments
"""
if not data[key]:
return key, data, errors, context
......@@ -203,9 +202,20 @@ def date(datetime_format):
try:
datetime.strptime(data[key], datetime_format)
except ValueError:
errors[key].append(
u'expected date with format [{}]; got [{}]'.format(datetime_format, data[key])
)
if datetime_format != '%Y-%m-%dT%H:%M:%S':
errors[key].append(
u'expected date with format [{}]; got [{}]'.format(datetime_format, data[key])
)
return key, data, errors, context
try:
datetime.strptime(data[key], '%Y-%m-%d')
data[key] = data[key] + 'T00:00:00'
except ValueError:
errors[key].append(
u'expected date with format [{}]; got [{}]'.format(datetime_format, data[key])
)
return key, data, errors, context
......
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