Skip to content

Commit

Permalink
[#1841] add IValidators, IConverters plugin interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Jul 14, 2014
1 parent fa0294a commit 938117a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
18 changes: 18 additions & 0 deletions ckan/logic/__init__.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions ckan/plugins/interfaces.py
Expand Up @@ -13,6 +13,7 @@
'IPackageController', 'IPluginObserver',
'IConfigurable', 'IConfigurer',
'IActions', 'IResourceUrlChange', 'IDatasetForm',
'IValidators', 'IConverters',
'IResourcePreview',
'IResourceController',
'IGroupForm',
Expand Down Expand Up @@ -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.'''

Expand Down
4 changes: 2 additions & 2 deletions ckan/plugins/toolkit.py
Expand Up @@ -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
Expand Down

0 comments on commit 938117a

Please sign in to comment.