Skip to content

Commit

Permalink
[#482] Add get_validator() and get_converter() functions to plugins.t…
Browse files Browse the repository at this point in the history
…oolkit
  • Loading branch information
tobes committed Feb 27, 2013
1 parent f0fc720 commit 9845416
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
62 changes: 62 additions & 0 deletions ckan/logic/__init__.py
Expand Up @@ -381,3 +381,65 @@ def wrapper(context, data_dict):
wrapper.side_effect_free = True

return wrapper


class UnknownValidator(Exception):
pass


_validators_cache = {}

def clear_validators_cache():
_validators_cache.clear()


def get_validator(validator):
if not _validators_cache:
# find validators
import ckan.lib.navl.validators as validators
for k, v in validators.__dict__.items():
try:
if v.__module__ != 'ckan.lib.navl.validators':
continue
_validators_cache[k] = v
except AttributeError:
pass

import ckan.logic.validators as validators
for k, v in validators.__dict__.items():
try:
if v.__module__ != 'ckan.logic.validators':
continue
_validators_cache[k] = v
except AttributeError:
pass
try:
return _validators_cache[validator]
except KeyError:
raise UnknownValidator('Validator `%s` does not exist' % validator)


class UnknownConverter(Exception):
pass


_converters_cache = {}

def clear_converters_cache():
_converters_cache.clear()


def get_converter(converter):
if not _converters_cache:
import ckan.logic.converters as converters
for k, v in converters.__dict__.items():
try:
if v.__module__ != 'ckan.logic.converters':
continue
_converters_cache[k] = v
except AttributeError:
pass
try:
return _converters_cache[converter]
except KeyError:
raise UnknownConverter('Converter `%s` does not exist' % converter)
8 changes: 8 additions & 0 deletions ckan/plugins/toolkit.py
Expand Up @@ -41,10 +41,14 @@ 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
'check_access', # check logic function authorisation
'ObjectNotFound', # action not found exception
# (ckan.logic.NotFound)
'NotAuthorized', # action not authorized exception
'UnknownConverter', # convertor not found exception
'UnknownValidator', # validator not found exception
'ValidationError', # model update validation error
'CkanCommand', # class for providing cli interfaces

Expand Down Expand Up @@ -87,10 +91,14 @@ def _initialize(self):
t['literal'] = webhelpers.html.tags.literal

t['get_action'] = logic.get_action
t['get_converter'] = logic.get_converter
t['get_validator'] = logic.get_validator
t['check_access'] = logic.check_access
t['ObjectNotFound'] = logic.NotFound # Name change intentional
t['NotAuthorized'] = logic.NotAuthorized
t['ValidationError'] = logic.ValidationError
t['UnknownConverter'] = logic.UnknownConverter
t['UnknownValidator'] = logic.UnknownValidator

t['CkanCommand'] = cli.CkanCommand

Expand Down

0 comments on commit 9845416

Please sign in to comment.