From 938117afb5b15e268ee8811fdaec929671d1346b Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Mon, 14 Jul 2014 14:26:47 -0400 Subject: [PATCH] [#1841] add IValidators, IConverters plugin interfaces --- ckan/logic/__init__.py | 18 ++++++++++++++++++ ckan/plugins/interfaces.py | 23 +++++++++++++++++++++++ ckan/plugins/toolkit.py | 4 ++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ckan/logic/__init__.py b/ckan/logic/__init__.py index 49fbc0d7beb..2e2da720a10 100644 --- a/ckan/logic/__init__.py +++ b/ckan/logic/__init__.py @@ -624,6 +624,15 @@ def get_validator(validator): validators = _import_module_functions('ckan.logic.validators') _validators_cache.update(validators) _validators_cache.update({'OneOf': formencode.validators.OneOf}) + + for plugin in p.PluginImplementations(p.IValidators): + for name, validator in plugin.get_validators().items(): + if name in _validators_cache: + raise Exception( + 'The validator %r is already defined' % (name,) + ) + log.debug('Validator function {0} from plugin {1} was inserted'.format(name, plugin.name)) + _validators_cache[name] = validator try: return _validators_cache[validator] except KeyError: @@ -662,6 +671,15 @@ def get_converter(converter): if not _converters_cache: converters = _import_module_functions('ckan.logic.converters') _converters_cache.update(converters) + + for plugin in p.PluginImplementations(p.IConverters): + for name, converter in plugin.get_converters().items(): + if name in _converters_cache: + raise Exception( + 'The converter %r is already defined' % (name,) + ) + log.debug('Converter function {0} from plugin {1} was inserted'.format(name, plugin.name)) + _converters_cache[name] = validator try: return _converters_cache[converter] except KeyError: diff --git a/ckan/plugins/interfaces.py b/ckan/plugins/interfaces.py index 50380bdf471..607bc41ebbb 100644 --- a/ckan/plugins/interfaces.py +++ b/ckan/plugins/interfaces.py @@ -13,6 +13,7 @@ 'IPackageController', 'IPluginObserver', 'IConfigurable', 'IConfigurer', 'IActions', 'IResourceUrlChange', 'IDatasetForm', + 'IValidators', 'IConverters', 'IResourcePreview', 'IResourceController', 'IGroupForm', @@ -523,6 +524,28 @@ def get_actions(self): """ +class IValidators(Interface): + """ + Allow adding of validators to be returned by ``get_validator()``. + """ + def get_validators(self): + """ + Should return a dict, the keys being the name of the validator + function and the values being the functions themselves. + """ + + +class IConverters(Interface): + """ + Allow adding of converters to be returned by ``get_converter()``. + """ + def get_converters(self): + """ + Should return a dict, the keys being the name of the converter + function and the values being the functions themselves. + """ + + class IAuthFunctions(Interface): '''Override CKAN's authorization functions, or add new auth functions.''' diff --git a/ckan/plugins/toolkit.py b/ckan/plugins/toolkit.py index 5c45b21a70b..d8ad2723e64 100644 --- a/ckan/plugins/toolkit.py +++ b/ckan/plugins/toolkit.py @@ -44,8 +44,8 @@ class _Toolkit(object): 'aslist', # converts an object to a list 'literal', # stop tags in a string being escaped 'get_action', # get logic action function - 'get_converter', # get validator function - 'get_validator', # get convertor action function + 'get_converter', # get navl schema converter + 'get_validator', # get navl schema validator 'check_access', # check logic function authorisation 'navl_validate', # implements validate method with navl schema 'ObjectNotFound', # action not found exception