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

JVickery-TBS:feature/validation support #113

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 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
eab54e4
Merge branch 'develop' into feature/validation-support
duttonw Oct 25, 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
18 changes: 15 additions & 3 deletions ckanext/xloader/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def xloader_submit(context, data_dict):
if errors:
raise p.toolkit.ValidationError(errors)

p.toolkit.check_access('xloader_submit', context, data_dict)

# If sync is set to True, the xloader callback will be executed right
# away, instead of a job being enqueued. It will also delete any existing jobs
# for the given resource. This is only controlled by sysadmins or the system.
sync = data_dict.pop('sync', False)

res_id = data_dict['resource_id']
try:
resource_dict = p.toolkit.get_action('resource_show')(context, {
Expand Down Expand Up @@ -166,15 +173,20 @@ def xloader_submit(context, data_dict):
job = enqueue_job(
jobs.xloader_data_into_datastore, [data], queue=custom_queue,
title="xloader_submit: package: {} resource: {}".format(resource_dict.get('package_id'), res_id),
rq_kwargs=dict(timeout=timeout)
rq_kwargs=dict(timeout=timeout, at_front=sync)
)
except Exception:
log.exception('Unable to enqueued xloader res_id=%s', res_id)
if sync:
log.exception('Unable to xloader res_id=%s', res_id)
else:
log.exception('Unable to enqueued xloader res_id=%s', res_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"to enqueued" -> "to enqueue"

return False
log.debug('Enqueued xloader job=%s res_id=%s', job.id, res_id)

value = json.dumps({'job_id': job.id})

if sync:
log.debug('Pushed xloader sync mode job=%s res_id=%s to front of queue', job.id, res_id)

task['value'] = value
task['state'] = 'pending'
task['last_updated'] = str(datetime.datetime.utcnow())
Expand Down
2 changes: 2 additions & 0 deletions ckanext/xloader/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ groups:
example: False
description: |
Resources are expected to have a Validation Schema, or use the default ones if not.

If this option is set to `False`, Resources that do not have
a Validation Schema will be treated like they do not require Validation.

See https://github.com/frictionlessdata/ckanext-validation?tab=readme-ov-file#data-schema
for more details.
- key: ckanext.xloader.clean_datastore_tables
Expand Down
16 changes: 12 additions & 4 deletions ckanext/xloader/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ckan.model.domain_object import DomainObjectOperation
from ckan.model.resource import Resource
from ckan.model.package import Package

from . import action, auth, helpers as xloader_helpers, utils
from ckanext.xloader.utils import XLoaderFormats
Expand Down Expand Up @@ -91,6 +92,7 @@ def receive_validation_report(self, validation_report):
sync = toolkit.asbool(toolkit.config.get(u'ckanext.validation.run_on_update_async', True))
self._submit_to_xloader(res_dict, sync=sync)


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flake8 generally prefers single blank lines inside a class.

# IDomainObjectModification

def notify(self, entity, operation):
Expand Down Expand Up @@ -183,7 +185,7 @@ def before_show(self, resource_dict):
def after_update(self, context, resource_dict):
self.after_resource_update(context, resource_dict)

def _submit_to_xloader(self, resource_dict):
def _submit_to_xloader(self, resource_dict, sync=False):
context = {"ignore_auth": True, "defer_commit": True}
resource_format = resource_dict.get("format")
if not XLoaderFormats.is_it_an_xloader_format(resource_format):
Expand All @@ -203,14 +205,20 @@ def _submit_to_xloader(self, resource_dict):
return

try:
log.debug(
"Submitting resource %s to be xloadered", resource_dict["id"]
)
if sync:
log.debug(
"xloadering resource %s in sync mode", resource_dict["id"]
)
else:
log.debug(
"Submitting resource %s to be xloadered", resource_dict["id"]
)
toolkit.get_action("xloader_submit")(
context,
{
"resource_id": resource_dict["id"],
"ignore_hash": self.ignore_hash,
"sync": sync,
},
)
except toolkit.ValidationError as e:
Expand Down
2 changes: 2 additions & 0 deletions ckanext/xloader/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
boolean_validator = get_validator('boolean_validator')
int_validator = get_validator('int_validator')
OneOf = get_validator('OneOf')
ignore_not_sysadmin = get_validator('ignore_not_sysadmin')

if p.toolkit.check_ckan_version('2.9'):
unicode_safe = get_validator('unicode_safe')
Expand All @@ -29,6 +30,7 @@ def xloader_submit_schema():
'id': [ignore_missing],
'set_url_type': [ignore_missing, boolean_validator],
'ignore_hash': [ignore_missing, boolean_validator],
'sync': [ignore_missing, boolean_validator, ignore_not_sysadmin],
'__junk': [empty],
'__before': [dsschema.rename('id', 'resource_id')]
}
Expand Down
8 changes: 5 additions & 3 deletions ckanext/xloader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import datetime
from rq import get_current_job

from six import text_type as str, binary_type

Expand All @@ -13,8 +14,6 @@
import ckan.plugins as p
from ckan.plugins.toolkit import config, h, _

from .job_exceptions import JobError

from logging import getLogger


Expand Down Expand Up @@ -58,11 +57,14 @@ def requires_successful_validation_report():


def awaiting_validation(res_dict):
# type: (dict) -> bool
"""
Checks the existence of a logic action from the ckanext-validation
plugin, thus supporting any extending of the Validation Plugin class.

Checks ckanext.xloader.validation.requires_successful_report config
option value.

Checks ckanext.xloader.validation.enforce_schema config
option value. Then checks the Resource's validation_status.
"""
Expand Down Expand Up @@ -273,7 +275,7 @@ def type_guess(rows, types=TYPES, strict=False):
at_least_one_value = []
for ri, row in enumerate(rows):
diff = len(row) - len(guesses)
for _ in range(diff):
for _i in range(diff):
typesdict = {}
for type in types:
typesdict[type] = 0
Expand Down