[ckan-changes] commit/ckanextiati: amercader: Add validators for dates, countries and booleans
Bitbucket
commits-noreply at bitbucket.org
Fri Oct 28 16:42:16 UTC 2011
1 new commit in ckanextiati:
https://bitbucket.org/okfn/ckanextiati/changeset/6c06c5502152/
changeset: 6c06c5502152
branch: spreadsheet-support
user: amercader
date: 2011-10-28 17:57:55
summary: Add validators for dates, countries and booleans
affected #: 3 files
diff -r 8a2d55a9d505ac42757d15d2c30f5cae7dffb75a -r 6c06c5502152521105beab126d693c215b7451a9 ckanext/iati/controllers/spreadsheet.py
--- a/ckanext/iati/controllers/spreadsheet.py
+++ b/ckanext/iati/controllers/spreadsheet.py
@@ -9,12 +9,15 @@
from ckan.logic import get_action, NotFound, ValidationError, NotAuthorized
from ckan.logic.converters import date_to_db
from ckan.logic.validators import int_validator
-from ckan.lib.navl.validators import not_empty
+from ckan.lib.navl.validators import not_empty, ignore_empty
from ckan.lib.navl.dictization_functions import validate
from ckanext.iati.authz import get_user_administered_groups
-from ckanext.iati.logic.validators import iati_dataset_name_from_csv, file_type_validator
-from ckanext.iati.logic.converters import iso_date
+from ckanext.iati.logic.validators import (iati_dataset_name_from_csv,
+ file_type_validator,
+ db_date,
+ yes_no,
+ country_code)
log = logging.getLogger(__name__)
@@ -25,14 +28,14 @@
('contact-email', 'package', 'author_email', []),
('source-url', 'resources', 'url', []),
('format', 'resources', 'format', []),
- ('file-type','extras', 'filetype', [file_type_validator]),
- ('recipient-country','extras', 'country', []),
- ('activity-period-start','extras', 'activity_period-from', [iso_date]),
- ('activity-period-end','extras', 'activity_period-to', [iso_date]),
- ('last-updated-datetime','extras', 'data_updated', [iso_date]),
- ('generated-datetime','extras', 'record_updated', [iso_date]),
- ('activity-count','extras', 'activity_count', [int_validator]),
- ('verification-status','extras', 'verified', []),
+ ('file-type','extras', 'filetype', [ignore_empty, file_type_validator]),
+ ('recipient-country','extras', 'country', [ignore_empty, country_code]),
+ ('activity-period-start','extras', 'activity_period-from', [ignore_empty, db_date]),
+ ('activity-period-end','extras', 'activity_period-to', [ignore_empty, db_date]),
+ ('last-updated-datetime','extras', 'data_updated', [ignore_empty, db_date]),
+ ('generated-datetime','extras', 'record_updated', [ignore_empty, db_date]),
+ ('activity-count','extras', 'activity_count', [ignore_empty,int_validator]),
+ ('verification-status','extras', 'verified', [ignore_empty,yes_no]),
('default-language','extras', 'language', [])
]
diff -r 8a2d55a9d505ac42757d15d2c30f5cae7dffb75a -r 6c06c5502152521105beab126d693c215b7451a9 ckanext/iati/logic/converters.py
--- a/ckanext/iati/logic/converters.py
+++ b/ckanext/iati/logic/converters.py
@@ -12,11 +12,3 @@
return 'yes' if not isinstance(value, Missing) else 'no'
-def iso_date(value,context):
- from ckan.lib.field_types import DateType, DateConvertError
- try:
- value = DateType.iso_to_db(value)
- except DateConvertError, e:
- raise Invalid(str(e))
- return value
-
diff -r 8a2d55a9d505ac42757d15d2c30f5cae7dffb75a -r 6c06c5502152521105beab126d693c215b7451a9 ckanext/iati/logic/validators.py
--- a/ckanext/iati/logic/validators.py
+++ b/ckanext/iati/logic/validators.py
@@ -1,7 +1,8 @@
from ckan.logic import get_action
-from ckan.lib.navl.dictization_functions import unflatten
+from ckan.lib.navl.dictization_functions import unflatten, Invalid
+from ckan.lib.field_types import DateType, DateConvertError
-from ckanext.iati.lists import FILE_TYPES
+from ckanext.iati.lists import FILE_TYPES, COUNTRIES
def iati_dataset_name(key,data,errors,context):
@@ -40,8 +41,37 @@
def file_type_validator(key,data,errors, context=None):
value = data.get(key)
- allowed_values = [t[0] for t in FILE_TYPES]
- if not value or not value in allowed_values:
+ allowed_values = [t[0] for t in FILE_TYPES]
+ if not value in allowed_values:
errors[key].append('File type must be one of [%s]' % ', '.join(allowed_values))
+def db_date(value, context):
+ try:
+ timedate_dict = DateType.parse_timedate(value, 'db')
+ except DateConvertError, e:
+ # Cannot parse
+ raise Invalid(str(e))
+ try:
+ value = DateType.format(timedate_dict, 'db')
+ except DateConvertError, e:
+ # Values out of range
+ raise Invalid(str(e))
+ return value
+
+def yes_no(value,context):
+
+ value = value.lower()
+ if not value in ['yes','no']:
+ raise Invalid('Value must be one of [yes, no]')
+
+ return value
+
+def country_code(value,context):
+
+ value = value.upper()
+ if not value in [c[0] for c in COUNTRIES]:
+ raise Invalid('Unknown country code "%s"' % value)
+
+ return value
+
Repository URL: https://bitbucket.org/okfn/ckanextiati/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the ckan-changes
mailing list