From 25ea76ebcd451502339724f64c54d08da71e771a Mon Sep 17 00:00:00 2001 From: Jesse Vickery Date: Mon, 29 Jan 2024 21:02:37 +0000 Subject: [PATCH] fix(dev): misc fixes; - Prevent POST to upload to DS if validation is required. - flake8 if wrap indents. --- ckanext/xloader/plugin.py | 26 +++++++------------------- ckanext/xloader/utils.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/ckanext/xloader/plugin.py b/ckanext/xloader/plugin.py index d15afeca..edaa34bd 100644 --- a/ckanext/xloader/plugin.py +++ b/ckanext/xloader/plugin.py @@ -115,10 +115,10 @@ def notify(self, entity, operation): # extension will call resource_patch and this method should # be called again. However, url_changed will not be in the entity # once Validation does the patch. - if _is_validation_plugin_loaded() and \ - toolkit.asbool(toolkit.config.get('ckanext.xloader.requires_validation')): + if utils.is_validation_plugin_loaded() and \ + toolkit.asbool(toolkit.config.get('ckanext.xloader.requires_validation')): if entity.__dict__.get('extras', {}).get('validation_status', None) != 'success': - log.debug("Skipping xloading resource %s because " + log.debug("Skipping xloading resource %s because the " "resource did not pass validation yet.", entity.id) return elif not getattr(entity, 'url_changed', False): @@ -138,10 +138,10 @@ def notify(self, entity, operation): # IResourceController def after_resource_create(self, context, resource_dict): - if _is_validation_plugin_loaded() and \ - toolkit.asbool(toolkit.config.get('ckanext.xloader.requires_validation')) and \ - resource_dict.get('validation_status', None) != 'success': - log.debug("Skipping xloading resource %s because " + if utils.is_validation_plugin_loaded() and \ + toolkit.asbool(toolkit.config.get('ckanext.xloader.requires_validation')) and \ + resource_dict.get('validation_status', None) != 'success': + log.debug("Skipping xloading resource %s because the " "resource did not pass validation yet.", resource_dict.get('id')) return self._submit_to_xloader(resource_dict) @@ -245,15 +245,3 @@ def get_helpers(self): "xloader_status": xloader_helpers.xloader_status, "xloader_status_description": xloader_helpers.xloader_status_description, } - - -def _is_validation_plugin_loaded(): - """ - Checks the existence of a logic action from the ckanext-validation - plugin, thus supporting any extending of the Validation Plugin class. - """ - try: - toolkit.get_action('resource_validation_show') - return True - except KeyError: - return False diff --git a/ckanext/xloader/utils.py b/ckanext/xloader/utils.py index ec8e4bbd..3bb20a98 100644 --- a/ckanext/xloader/utils.py +++ b/ckanext/xloader/utils.py @@ -9,11 +9,29 @@ from decimal import Decimal import ckan.plugins as p +from ckan.plugins.toolkit import config, h, _ def resource_data(id, resource_id, rows=None): if p.toolkit.request.method == "POST": + if is_validation_plugin_loaded() and \ + p.toolkit.asbool(p.toolkit.config.get('ckanext.xloader.requires_validation')): + context = { + "ignore_auth": True, + } + resource_dict = p.toolkit.get_action("resource_show")( + context, + { + "id": resource_id, + }, + ) + if resource_dict.get('validation_status', None) != 'success': + h.flash_error(_("Cannot upload resource %s to the DataStore " + "because the resource did not pass validation yet.") % resource_id) + return p.toolkit.redirect_to( + "xloader.resource_data", id=id, resource_id=resource_id + ) try: p.toolkit.get_action("xloader_submit")( None, @@ -215,3 +233,15 @@ def type_guess(rows, types=TYPES, strict=False): guesses_tuples = [(t, guess[t]) for t in types if t in guess] _columns.append(max(guesses_tuples, key=lambda t_n: t_n[1])[0]) return _columns + + +def is_validation_plugin_loaded(): + """ + Checks the existence of a logic action from the ckanext-validation + plugin, thus supporting any extending of the Validation Plugin class. + """ + try: + p.toolkit.get_action('resource_validation_show') + return True + except KeyError: + return False