Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Extension Support #200

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f0a470a
feat(dev): adds validation extension support;
JVickery-TBS Nov 24, 2023
a97f4ad
feat(dev): logging;
JVickery-TBS Nov 24, 2023
028e42b
fix(dev): 2.9 support;
JVickery-TBS Nov 24, 2023
77d762e
fix(dev): better conditioning;
JVickery-TBS Nov 27, 2023
aec22b9
feat(comments): added comments;
JVickery-TBS Nov 27, 2023
cd3c7cc
Merge branch 'master' into feature/validation-support
JVickery-TBS Jan 29, 2024
7e99aa7
fix(dev): misc feedback;
JVickery-TBS Jan 29, 2024
25ea76e
fix(dev): misc fixes;
JVickery-TBS Jan 29, 2024
d2720fc
fix(syntax): flake8;
JVickery-TBS Jan 31, 2024
e888153
feat(dev): logic and schema config option;
JVickery-TBS Feb 2, 2024
5c07ba4
Merge branch 'master' into feature/validation-support
JVickery-TBS Feb 2, 2024
4612484
feat(dev): better logic and tests;
JVickery-TBS Feb 2, 2024
8ac8db5
fix(logic): fixed some logic;
JVickery-TBS Feb 2, 2024
d9bb56c
Merge branch 'master' into feature/validation-support
JVickery-TBS Feb 5, 2024
1ac8090
fix(syntax): made better;
JVickery-TBS Feb 5, 2024
1761ed5
fix(comments): fixed inline comments;
JVickery-TBS Feb 5, 2024
e182eb7
feat(dev): started doing sync mode;
JVickery-TBS Feb 6, 2024
b386e0e
feat(dev): sync mode cont.;
JVickery-TBS Feb 7, 2024
3200483
feat(dev): sync mode cont.;
JVickery-TBS Feb 7, 2024
d225801
Merge branch 'master' into feature/validation-support
JVickery-TBS May 16, 2024
4fbdb0d
feat(dev): IPipeValidation implementation;
JVickery-TBS May 16, 2024
27d98cf
fix(tests): validation req tests;
JVickery-TBS May 16, 2024
378f69f
fix(misc): comments and messages;
JVickery-TBS Jul 12, 2024
6070740
fix(logic): ignore not sysadmin;
JVickery-TBS Aug 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ckanext/xloader/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def xloader_submit(context, data_dict):
p.toolkit.check_access('xloader_submit', context, data_dict)

sync = data_dict.pop('sync', False)

res_id = data_dict['resource_id']
try:
resource_dict = p.toolkit.get_action('resource_show')(context, {
Expand Down
8 changes: 0 additions & 8 deletions ckanext/xloader/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ groups:

See https://github.com/frictionlessdata/ckanext-validation?tab=readme-ov-file#data-schema
for more details.
- key: ckanext.xloader.validation.chain_xloader
default: True
example: False
description: |
Resources that pass Validation will immediately get XLoadered instead of having
a job enqueued for it.

If this option is set to `False`, jobs will be enqueued like normal.
- key: ckanext.xloader.clean_datastore_tables
default: False
example: True
Expand Down
44 changes: 27 additions & 17 deletions ckanext/xloader/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
from . import action, auth, helpers as xloader_helpers, utils
from ckanext.xloader.utils import XLoaderFormats

try:
from ckanext.validation.interfaces import IPipeValidation
HAS_IPIPE_VALIDATION = True
except ImportError:
HAS_IPIPE_VALIDATION = False

try:
config_declarations = toolkit.blanket.config_declarations
except AttributeError:
Expand All @@ -34,6 +40,8 @@ class xloaderPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IResourceController, inherit=True)
plugins.implements(plugins.IClick)
plugins.implements(plugins.IBlueprint)
if HAS_IPIPE_VALIDATION:
plugins.implements(IPipeValidation)

# IClick
def get_commands(self):
Expand Down Expand Up @@ -68,6 +76,23 @@ def configure(self, config_):
)
)

# IPipeValidation

def receive_validation_report(self, validation_report):
if utils.requires_successful_validation_report():
res_dict = toolkit.get_action('resource_show')({'ignore_auth': True},
{'id': validation_report.get('resource_id')})
if (toolkit.asbool(toolkit.config.get('ckanext.xloader.validation.enforce_schema', True))
or res_dict.get('schema', None)) and validation_report.get('status') != 'success':
# either validation.enforce_schema is turned on or it is off and there is a schema,
JVickery-TBS marked this conversation as resolved.
Show resolved Hide resolved
# we then explicitly check for the `validation_status` report to be `success`
return
# if validation is running in async mode, it is running from the redis workers.
# thus we need to do sync=True to have Xloader put the job at the front of the queue.
sync = toolkit.asbool(toolkit.config.get(u'ckanext.validation.run_on_update_async', True))
self._submit_to_xloader(res_dict, sync=sync)


# IDomainObjectModification

def notify(self, entity, operation):
Expand Down Expand Up @@ -95,21 +120,10 @@ def notify(self, entity, operation):
if _should_remove_unsupported_resource_from_datastore(resource_dict):
toolkit.enqueue_job(fn=_remove_unsupported_resource_from_datastore, args=[entity.id])

if utils.awaiting_validation(resource_dict):
# If the resource requires validation, stop here if validation
# has not been performed or did not succeed. The Validation
# 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 utils.requires_successful_validation_report():
log.debug("Skipping xloading resource %s because the "
"resource did not pass validation yet.", entity.id)
return
elif utils.do_chain_after_validation(resource_dict):
# At this point, the Resource has passed validation requirements,
# and chaining is turned on. We will execute XLoader right away,
# inside of the Validation job, instead of enqueueing a job.
self._submit_to_xloader(resource_dict, sync=True)
return
elif not getattr(entity, 'url_changed', False):
# do not submit to xloader if the url has not changed.
return
Expand All @@ -119,15 +133,11 @@ def notify(self, entity, operation):
# IResourceController

def after_resource_create(self, context, resource_dict):
if utils.awaiting_validation(resource_dict):
if utils.requires_successful_validation_report():
log.debug("Skipping xloading resource %s because the "
"resource did not pass validation yet.", resource_dict.get('id'))
return

if utils.do_chain_after_validation(resource_dict):
self._submit_to_xloader(resource_dict, sync=True)
return

self._submit_to_xloader(resource_dict)

def before_resource_show(self, resource_dict):
Expand Down
37 changes: 5 additions & 32 deletions ckanext/xloader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def is_it_an_xloader_format(cls, format_):
return format_.lower() in cls._formats


def requires_successful_validation_report():
return p.toolkit.asbool(config.get('ckanext.xloader.validation.requires_successful_report', False))


def awaiting_validation(res_dict):
# type: (dict) -> bool
"""
Expand All @@ -60,7 +64,7 @@ def awaiting_validation(res_dict):
Checks ckanext.xloader.validation.enforce_schema config
option value. Then checks the Resource's validation_status.
"""
if not p.toolkit.asbool(config.get('ckanext.xloader.validation.requires_successful_report', False)):
if not requires_successful_validation_report():
# validation.requires_successful_report is turned off, return right away
return False

Expand Down Expand Up @@ -90,37 +94,6 @@ def awaiting_validation(res_dict):
return False


def do_chain_after_validation(res_dict):
# type: (dict) -> bool

current_job = get_current_job()

if not p.toolkit.asbool(config.get('ckanext.xloader.validation.requires_successful_report', False)) \
or not p.toolkit.asbool(config.get('ckanext.xloader.validation.chain_xloader', True)) \
or not current_job:

# we are not requiring resources to pass validation
# OR we are not chaining validation to xloader
# OR we are outside of the job context, thus not running a job
return False

if current_job.func_name != 'ckanext.validation.jobs.run_validation_job':
# the current running job is not the ckanext-validation validate job
#FIXME: how to do a better check for the caller in the stack??
return False

try:
job_rid = current_job.args[0].get('id', None)
except (KeyError):
job_rid = None
if res_dict.get('id', None) != job_rid:
# the current running job's Resource ID is not
# the same as the passed Resource's ID
return False

return True


def resource_data(id, resource_id, rows=None):

if p.toolkit.request.method == "POST":
Expand Down
Loading