Skip to content

Commit

Permalink
chore: alignment, move logic into logic folder and split auth and act…
Browse files Browse the repository at this point in the history
…ions apart, move plugin from folder to root plugin.py
  • Loading branch information
duttonw committed Dec 10, 2024
1 parent 68a02d8 commit 8ec2043
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ ENV/

# mypy
.mypy_cache/
.idea
Empty file.
42 changes: 13 additions & 29 deletions ckanext/validation/logic.py → ckanext/validation/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
log = logging.getLogger(__name__)


def get_actions():
validators = (
resource_validation_run,
resource_validation_show,
resource_validation_delete,
resource_validation_run_batch,
resource_create,
resource_update,
)

return {"{}".format(func.__name__): func for func in validators}


def enqueue_job(*args, **kwargs):
try:
return t.enqueue_job(*args, **kwargs)
Expand All @@ -34,35 +47,6 @@ def enqueue_job(*args, **kwargs):
return enqueue_job_legacy(*args, **kwargs)


# Auth

def auth_resource_validation_run(context, data_dict):
if t.check_access(
u'resource_update', context, {u'id': data_dict[u'resource_id']}):
return {u'success': True}
return {u'success': False}


def auth_resource_validation_delete(context, data_dict):
if t.check_access(
u'resource_update', context, {u'id': data_dict[u'resource_id']}):
return {u'success': True}
return {u'success': False}


@t.auth_allow_anonymous_access
def auth_resource_validation_show(context, data_dict):
if t.check_access(
u'resource_show', context, {u'id': data_dict[u'resource_id']}):
return {u'success': True}
return {u'success': False}


def auth_resource_validation_run_batch(context, data_dict):
'''u Sysadmins only'''
return {u'success': False}


# Actions


Expand Down
39 changes: 39 additions & 0 deletions ckanext/validation/logic/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import ckan.plugins.toolkit as tk


def get_auth_functions():
validators = (
resource_validation_run,
resource_validation_delete,
resource_validation_show,
resource_validation_run_batch,
)

return {"{}".format(func.__name__): func for func in validators}


def resource_validation_run(context, data_dict):
if tk.check_access(u'resource_update', context,
{u'id': data_dict[u'resource_id']}):
return {u'success': True}
return {u'success': False}


def resource_validation_delete(context, data_dict):
if tk.check_access(u'resource_update', context,
{u'id': data_dict[u'resource_id']}):
return {u'success': True}
return {u'success': False}


@tk.auth_allow_anonymous_access
def resource_validation_show(context, data_dict):
if tk.check_access(u'resource_show', context,
{u'id': data_dict[u'resource_id']}):
return {u'success': True}
return {u'success': False}


def resource_validation_run_batch(context, data_dict):
'''u Sysadmins only'''
return {u'success': False}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@

from ckanext.validation import settings
from ckanext.validation.model import tables_exist
from ckanext.validation.logic import (
resource_validation_run, resource_validation_show,
resource_validation_delete, resource_validation_run_batch,
auth_resource_validation_run, auth_resource_validation_show,
auth_resource_validation_delete, auth_resource_validation_run_batch,
resource_create as custom_resource_create,
resource_update as custom_resource_update,
)
from .logic import action, auth
from ckanext.validation.helpers import (
get_validation_badge,
validation_extract_report_from_errors,
Expand Down Expand Up @@ -75,33 +68,19 @@ def update_config(self, config_):
else:
log.debug(u'Validation tables exist')

t.add_template_directory(config_, u'../templates')
t.add_public_directory(config_, u'../public')
t.add_resource(u'../webassets', 'ckanext-validation')
t.add_template_directory(config_, u'templates')
t.add_public_directory(config_, u'public')
t.add_resource(u'webassets', 'ckanext-validation')

# IActions

def get_actions(self):
new_actions = {
u'resource_validation_run': resource_validation_run,
u'resource_validation_show': resource_validation_show,
u'resource_validation_delete': resource_validation_delete,
u'resource_validation_run_batch': resource_validation_run_batch,
u'resource_create': custom_resource_create,
u'resource_update': custom_resource_update,
}

return new_actions
return action.get_actions()

# IAuthFunctions

def get_auth_functions(self):
return {
u'resource_validation_run': auth_resource_validation_run,
u'resource_validation_show': auth_resource_validation_show,
u'resource_validation_delete': auth_resource_validation_delete,
u'resource_validation_run_batch': auth_resource_validation_run_batch,
}
return auth.get_auth_functions()

# ITemplateHelpers

Expand Down
8 changes: 4 additions & 4 deletions ckanext/validation/tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_can_validate_called_on_update_sync_no_validation(self, mock_validation)
class TestInterfaceAsync():

@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', True)
@mock.patch('ckanext.validation.logic.enqueue_job')
@mock.patch('ckanext.validation.logic.action.enqueue_job')
def test_can_validate_called_on_create_async(self, mock_validation):

dataset = factories.Dataset()
Expand All @@ -152,7 +152,7 @@ def test_can_validate_called_on_create_async(self, mock_validation):
assert mock_validation.called

@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', True)
@mock.patch('ckanext.validation.logic.enqueue_job')
@mock.patch('ckanext.validation.logic.action.enqueue_job')
def test_can_validate_called_on_create_async_no_validation(self, mock_validation):

dataset = factories.Dataset()
Expand All @@ -169,7 +169,7 @@ def test_can_validate_called_on_create_async_no_validation(self, mock_validation

@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_async', True)
@mock.patch('ckanext.validation.logic.enqueue_job')
@mock.patch('ckanext.validation.logic.action.enqueue_job')
def test_can_validate_called_on_update_async(self, mock_validation):

dataset = factories.Dataset()
Expand All @@ -187,7 +187,7 @@ def test_can_validate_called_on_update_async(self, mock_validation):

@pytest.mark.ckan_config('ckanext.validation.run_on_create_async', False)
@pytest.mark.ckan_config('ckanext.validation.run_on_update_async', True)
@mock.patch('ckanext.validation.logic.enqueue_job')
@mock.patch('ckanext.validation.logic.action.enqueue_job')
def test_can_validate_called_on_update_async_no_validation(self, mock_validation):

dataset = factories.Dataset()
Expand Down
12 changes: 6 additions & 6 deletions ckanext/validation/tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def test_resource_validation_no_url_or_upload(self):

assert "Resource must have a valid URL" in str(e)

@mock.patch("ckanext.validation.logic.enqueue_job")
@mock.patch("ckanext.validation.logic.action.enqueue_job")
def test_resource_validation_with_url(self, mock_enqueue_job):

resource = factories.Resource(url="http://example.com", format="csv")

call_action("resource_validation_run", resource_id=resource["id"])

@mock.patch("ckanext.validation.logic.enqueue_job")
@mock.patch("ckanext.validation.logic.action.enqueue_job")
def test_resource_validation_with_upload(self, mock_enqueue_job):

resource = factories.Resource(url="", url_type="upload", format="csv")
Expand All @@ -85,7 +85,7 @@ def test_resource_validation_run_starts_job(self):

assert len(jobs_after) == len(jobs) + 1

@mock.patch("ckanext.validation.logic.enqueue_job")
@mock.patch("ckanext.validation.logic.action.enqueue_job")
def test_resource_validation_creates_validation_object(self, mock_enqueue_job):

resource = factories.Resource(format="csv")
Expand All @@ -106,7 +106,7 @@ def test_resource_validation_creates_validation_object(self, mock_enqueue_job):
assert validation.error is None

@pytest.mark.ckan_config("ckanext.validation.run_on_create_async", False)
@mock.patch("ckanext.validation.logic.enqueue_job")
@mock.patch("ckanext.validation.logic.action.enqueue_job")
def test_resource_validation_resets_existing_validation_object(
self, mock_enqueue_job
):
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_resource_validation_resets_existing_validation_object(
assert validation.report is None
assert validation.error is None

@mock.patch("ckanext.validation.logic.enqueue_job")
@mock.patch("ckanext.validation.logic.action.enqueue_job")
def test_resource_validation_only_called_on_resource_created(
self, mock_enqueue_job
):
Expand All @@ -170,7 +170,7 @@ def test_resource_validation_only_called_on_resource_created(
assert mock_enqueue_job.call_count == 1
assert mock_enqueue_job.call_args[0][1][0]["id"] == resource2["id"]

@mock.patch("ckanext.validation.logic.enqueue_job")
@mock.patch("ckanext.validation.logic.action.enqueue_job")
def test_resource_validation_only_called_on_resource_updated(
self, mock_enqueue_job
):
Expand Down
Loading

0 comments on commit 8ec2043

Please sign in to comment.