diff --git a/ckanext/validation/helpers.py b/ckanext/validation/helpers.py index edd2f314..cd171427 100644 --- a/ckanext/validation/helpers.py +++ b/ckanext/validation/helpers.py @@ -3,10 +3,11 @@ from six.moves.urllib.parse import urlparse from six import string_types -from ckantoolkit import url_for, _, config, asbool, literal, h +from ckantoolkit import url_for, _, config, asbool, literal from ckanext.validation.utils import get_default_schema + def get_helpers(): validators = ( get_validation_badge, diff --git a/ckanext/validation/jobs.py b/ckanext/validation/jobs.py index 2c9ca40e..0cc3828e 100644 --- a/ckanext/validation/jobs.py +++ b/ckanext/validation/jobs.py @@ -140,8 +140,8 @@ def run_validation_job(resource): t.get_action('resource_patch')( {'ignore_auth': True, - 'user': t.get_action('get_site_user')({'ignore_auth': True})['name'], - '_validation_performed': True}, + 'user': t.get_action('get_site_user')({'ignore_auth': True})['name'], + '_validation_performed': True}, data_dict) utils.send_validation_report(utils.validation_dictize(validation)) diff --git a/ckanext/validation/logic/action.py b/ckanext/validation/logic/action.py index ee2b57eb..de9a5aae 100644 --- a/ckanext/validation/logic/action.py +++ b/ckanext/validation/logic/action.py @@ -33,11 +33,7 @@ def get_actions(): return {"{}".format(func.__name__): func for func in validators} - - # Actions - - def resource_validation_run(context, data_dict): u''' Start a validation job against a resource. @@ -67,7 +63,7 @@ def resource_validation_run(context, data_dict): # Ensure format is supported if not resource.get(u'format', u'').lower() in settings.get_supported_formats(): raise t.ValidationError( - {u'format': u'Unsupported resource format.' + + {u'format': u'Unsupported resource format.' u'Must be one of {}'.format( u','.join(settings.get_supported_formats()))}) @@ -108,6 +104,7 @@ def resource_validation_run(context, data_dict): def enqueue_job(*args, **kwargs): return t.enqueue_job(*args, **kwargs) + @t.side_effect_free def resource_validation_show(context, data_dict): u''' @@ -285,9 +282,9 @@ def resource_validation_run_batch(context, data_dict): except t.ValidationError as e: log.warning( - u'Could not run validation for resource %s ' + + u'Could not run validation for resource %s ' u'from dataset %s: %s', - resource['id'], dataset['name'], e) + resource['id'], dataset['name'], e) if len(query['results']) < page_size: break @@ -363,8 +360,8 @@ def _update_search_params(search_data_dict, user_search_params=None): else: search_data_dict['fq'] = user_search_params['fq'] - if (user_search_params.get('fq_list') and - isinstance(user_search_params['fq_list'], list)): + if (user_search_params.get('fq_list') + and isinstance(user_search_params['fq_list'], list)): search_data_dict['fq_list'].extend(user_search_params['fq_list']) diff --git a/ckanext/validation/plugin.py b/ckanext/validation/plugin.py index 8edb4a2d..8f484445 100644 --- a/ckanext/validation/plugin.py +++ b/ckanext/validation/plugin.py @@ -3,12 +3,10 @@ import json import logging - - import ckan.plugins as p import ckantoolkit as tk -from . import settings as s, utils, validators, utils +from . import settings as s, utils, validators from .helpers import get_helpers from .logic import action, auth from .model import tables_exist @@ -16,7 +14,6 @@ from ckanext.validation.interfaces import IDataValidation from ckanext.validation import views, cli - log = logging.getLogger(__name__) @@ -82,14 +79,12 @@ def get_validators(self): resources_to_validate = {} packages_to_skip = {} - # CKAN < 2.10 def before_create(self, context, data_dict): return self.before_resource_create(context, data_dict) # CKAN >= 2.10 def before_resource_create(self, context, data_dict): - is_dataset = self._data_dict_is_dataset(data_dict) if not is_dataset: context["_resource_create_call"] = True @@ -104,7 +99,6 @@ def after_create(self, context, data_dict): # CKAN >= 2.10 def after_resource_create(self, context, data_dict): - is_dataset = self._data_dict_is_dataset(data_dict) if not s.get_create_mode_from_config() == u'async': @@ -130,14 +124,13 @@ def _handle_validation_for_resource(self, context, resource): needs_validation = False if (( # File uploaded - resource.get(u'url_type') == u'upload' or + resource.get(u'url_type') == u'upload' # URL defined - resource.get(u'url') - ) and ( + or resource.get(u'url') + ) and ( # Make sure format is supported - resource.get(u'format', u'').lower() in - s.get_supported_formats() - )): + resource.get(u'format', u'').lower() in s.get_supported_formats() + )): needs_validation = True if needs_validation: @@ -173,20 +166,17 @@ def before_resource_update(self, context, current_resource, updated_resource): needs_validation = False if (( # New file uploaded - updated_resource.get(u'upload') or + updated_resource.get(u'upload') # External URL changed - updated_resource.get(u'url') != current_resource.get(u'url') or + or updated_resource.get(u'url') != current_resource.get(u'url') # Schema changed - (updated_resource.get(u'schema') != - current_resource.get(u'schema')) or + or updated_resource.get(u'schema') != current_resource.get(u'schema') # Format changed - (updated_resource.get(u'format', u'').lower() != - current_resource.get(u'format', u'').lower()) - ) and ( + or updated_resource.get(u'format', u'').lower() != current_resource.get(u'format', u'').lower() + ) and ( # Make sure format is supported - updated_resource.get(u'format', u'').lower() in - s.get_supported_formats() - )): + updated_resource.get(u'format', u'').lower() in s.get_supported_formats() + )): needs_validation = True if needs_validation: diff --git a/ckanext/validation/settings.py b/ckanext/validation/settings.py index 4bf59945..1072c476 100644 --- a/ckanext/validation/settings.py +++ b/ckanext/validation/settings.py @@ -64,7 +64,6 @@ def get_supported_formats(): return supported_formats or DEFAULT_SUPPORTED_FORMATS - def get_update_mode_from_config(): if asbool( config.get(u'ckanext.validation.run_on_update_sync', False)): @@ -84,4 +83,4 @@ def get_create_mode_from_config(): config.get(u'ckanext.validation.run_on_create_async', True)): return u'async' else: - return None \ No newline at end of file + return None diff --git a/ckanext/validation/tests/test_helpers.py b/ckanext/validation/tests/test_helpers.py index 2d94a903..99dd9626 100644 --- a/ckanext/validation/tests/test_helpers.py +++ b/ckanext/validation/tests/test_helpers.py @@ -96,7 +96,6 @@ def test_get_validation_badge_error(self): assert 'dataerror' in out assert 'title="Error during validation {}"'.format(resource["validation_timestamp"]) in out - def test_get_validation_badge_other(self): resource = factories.Resource( diff --git a/ckanext/validation/utils.py b/ckanext/validation/utils.py index d50f0d8e..e451bf5d 100644 --- a/ckanext/validation/utils.py +++ b/ckanext/validation/utils.py @@ -3,18 +3,15 @@ from six import string_types, ensure_str -import ckan.plugins as plugins +import ckan.plugins as p +import ckantoolkit as t import ckan.lib.uploader as uploader from ckan import model - -from ckanext.validation.interfaces import IPipeValidation +from .interfaces import IPipeValidation +from . import settings log = logging.getLogger(__name__) -from . import settings -import ckan.plugins as p -import ckantoolkit as t - def process_schema_fields(data_dict): u''' @@ -41,8 +38,8 @@ def process_schema_fields(data_dict): data_dict["schema"] = data_dict["schema"].decode() elif schema_url: - if (not isinstance(schema_url, string_types) or - not schema_url.lower()[:4] == u'http'): + if (not isinstance(schema_url, string_types) + or not schema_url.lower()[:4] == u'http'): raise t.ValidationError({u'schema_url': 'Must be a valid URL'}) data_dict[u'schema'] = schema_url elif schema_json: @@ -50,6 +47,7 @@ def process_schema_fields(data_dict): return data_dict + def run_async_validation(resource_id): try: @@ -60,8 +58,7 @@ def run_async_validation(resource_id): except t.ValidationError as e: log.warning( u'Could not run validation for resource %s: %s', - resource_id, e) - + resource_id, e) def get_default_schema(package_id): @@ -81,9 +78,9 @@ def should_remove_unsupported_resource_validation_reports(res_dict): return False return (not res_dict.get('format', u'').lower() in settings.get_supported_formats() and (res_dict.get('url_type') == 'upload' - or not res_dict.get('url_type')) + or not res_dict.get('url_type')) and (t.h.asbool(res_dict.get('validation_status', False)) - or t.h.asbool(res_dict.get('extras', {}).get('validation_status', False)))) + or t.h.asbool(res_dict.get('extras', {}).get('validation_status', False)))) def remove_unsupported_resource_validation_reports(resource_id): @@ -102,7 +99,7 @@ def remove_unsupported_resource_validation_reports(resource_id): if should_remove_unsupported_resource_validation_reports(res): log.info('Unsupported resource format "%s". Deleting validation reports for resource %s', - res.get(u'format', u''), res['id']) + res.get(u'format', u''), res['id']) try: p.toolkit.get_action('resource_validation_delete')(context, { "resource_id": res['id']}) @@ -111,8 +108,6 @@ def remove_unsupported_resource_validation_reports(resource_id): log.error('Validation reports for resource %s do not exist', res['id']) - - def get_local_upload_path(resource_id): u''' Returns the local path to an uploaded file give an id @@ -173,7 +168,7 @@ def validation_dictize(validation): def send_validation_report(validation_report): - for observer in plugins.PluginImplementations(IPipeValidation): + for observer in p.PluginImplementations(IPipeValidation): try: observer.receive_validation_report(validation_report) except Exception as ex: