Skip to content

Commit

Permalink
fix(dev): misc fixes;
Browse files Browse the repository at this point in the history
- Prevent POST to upload to DS if validation is required.
- flake8 if wrap indents.
  • Loading branch information
JVickery-TBS committed Jan 29, 2024
1 parent 7e99aa7 commit 25ea76e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
26 changes: 7 additions & 19 deletions ckanext/xloader/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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
30 changes: 30 additions & 0 deletions ckanext/xloader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit 25ea76e

Please sign in to comment.