Skip to content

Commit

Permalink
Merge pull request #2638 from ckan/cleanup-exception-deprecations
Browse files Browse the repository at this point in the history
Removes deprecation warning.
  • Loading branch information
wardi committed Jan 25, 2016
2 parents 412bd2d + 3219859 commit 0ca368c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
45 changes: 31 additions & 14 deletions ckan/logic/__init__.py
Expand Up @@ -25,7 +25,14 @@ class UsernamePasswordError(Exception):


class ActionError(Exception):
pass

def __init__(self, message=''):
self.message = message
super(ActionError, self).__init__(message)

def __str__(self):
return self.message


class NotFound(ActionError):
'''Exception raised by logic functions when a given object is not found.
Expand All @@ -44,7 +51,6 @@ class NotAuthorized(ActionError):
For example :py:func:`~ckan.logic.action.create.package_create` raises
:py:exc:`~ckan.plugins.toolkit.NotAuthorized` if the user is not authorized
to create packages.
'''
pass

Expand Down Expand Up @@ -90,8 +96,8 @@ def prettify(field_name):
elif key == 'extras':
errors_extras = []
for item in error:
if (item.get('key')
and item['key'][0] not in errors_extras):
if (item.get('key') and
item['key'][0] not in errors_extras):
errors_extras.append(item.get('key')[0])
summary[_('Extras')] = ', '.join(errors_extras)
elif key == 'extras_validation':
Expand Down Expand Up @@ -267,7 +273,7 @@ def check_access(action, context, data_dict=None):
user = context.get('user')

try:
if not 'auth_user_obj' in context:
if 'auth_user_obj' not in context:
context['auth_user_obj'] = None

if not context.get('ignore_auth'):
Expand All @@ -294,6 +300,8 @@ def check_access(action, context, data_dict=None):


_actions = {}


def clear_actions_cache():
_actions.clear()

Expand Down Expand Up @@ -343,7 +351,7 @@ def get_action(action):
'''

if _actions:
if not action in _actions:
if action not in _actions:
raise KeyError("Action '%s' not found" % action)
return _actions.get(action)
# Otherwise look in all the plugins to resolve all possible
Expand All @@ -360,9 +368,9 @@ def get_action(action):
if not k.startswith('_'):
# Only load functions from the action module or already
# replaced functions.
if (hasattr(v, '__call__')
and (v.__module__ == module_path
or hasattr(v, '__replaced'))):
if (hasattr(v, '__call__') and
(v.__module__ == module_path or
hasattr(v, '__replaced'))):
_actions[k] = v

# Whitelist all actions defined in logic/action/get.py as
Expand All @@ -371,7 +379,6 @@ def get_action(action):
not hasattr(v, 'side_effect_free'):
v.side_effect_free = True


# Then overwrite them with any specific ones in the plugins:
resolved_action_plugins = {}
fetched_actions = {}
Expand Down Expand Up @@ -421,7 +428,8 @@ def wrapped(context=None, data_dict=None, **kw):
log.debug('No auth function for %s' % action_name)
elif not getattr(_action, 'auth_audit_exempt', False):
raise Exception(
'Action function {0} did not call its auth function'
'Action function {0} did not call its '
'auth function'
.format(action_name))
# remove from audit stack
context['__auth_audit'].pop()
Expand Down Expand Up @@ -485,6 +493,7 @@ def get_or_bust(data_dict, keys):
return values[0]
return tuple(values)


def validate(schema_func, can_skip_validator=False):
''' A decorator that validates an action function against a given schema
'''
Expand All @@ -503,6 +512,7 @@ def wrapper(context, data_dict):
return wrapper
return action_decorator


def side_effect_free(action):
'''A decorator that marks the given action function as side-effect-free.
Expand Down Expand Up @@ -564,6 +574,7 @@ def wrapper(context, data_dict):
wrapper.auth_audit_exempt = True
return wrapper


def auth_allow_anonymous_access(action):
''' Flag an auth function as not requiring a logged in user
Expand All @@ -578,6 +589,7 @@ def wrapper(context, data_dict):
wrapper.auth_allow_anonymous_access = True
return wrapper


def auth_disallow_anonymous_access(action):
''' Flag an auth function as requiring a logged in user
Expand All @@ -591,6 +603,7 @@ def wrapper(context, data_dict):
wrapper.auth_allow_anonymous_access = False
return wrapper


class UnknownValidator(Exception):
'''Exception raised when a requested validator function cannot be found.
Expand All @@ -600,6 +613,7 @@ class UnknownValidator(Exception):

_validators_cache = {}


def clear_validators_cache():
_validators_cache.clear()

Expand All @@ -620,7 +634,7 @@ def get_validator(validator):
:rtype: ``types.FunctionType``
'''
if not _validators_cache:
if not _validators_cache:
validators = _import_module_functions('ckan.lib.navl.validators')
_validators_cache.update(validators)
validators = _import_module_functions('ckan.logic.validators')
Expand All @@ -635,7 +649,8 @@ def get_validator(validator):
raise NameConflict(
'The validator %r is already defined' % (name,)
)
log.debug('Validator function {0} from plugin {1} was inserted'.format(name, plugin.name))
log.debug('Validator function {0} from plugin {1} was inserted'
.format(name, plugin.name))
_validators_cache[name] = fn
try:
return _validators_cache[validator]
Expand All @@ -644,7 +659,8 @@ def get_validator(validator):


def model_name_to_class(model_module, model_name):
'''Return the class in model_module that has the same name as the received string.
'''Return the class in model_module that has the same name as the
received string.
Raises AttributeError if there's no model in model_module named model_name.
'''
Expand All @@ -654,6 +670,7 @@ def model_name_to_class(model_module, model_name):
except AttributeError:
raise ValidationError("%s isn't a valid model" % model_class_name)


def _import_module_functions(module_path):
'''Import a module and get the functions and return them in a dict'''
functions_dict = {}
Expand Down
1 change: 0 additions & 1 deletion ckan/tests/legacy/test_coding_standards.py
Expand Up @@ -409,7 +409,6 @@ class TestPep8(object):
'ckan/lib/search/index.py',
'ckan/lib/search/query.py',
'ckan/lib/search/sql.py',
'ckan/logic/__init__.py',
'ckan/logic/action/__init__.py',
'ckan/logic/action/delete.py',
'ckan/logic/action/get.py',
Expand Down

0 comments on commit 0ca368c

Please sign in to comment.