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 3810b48
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 56 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

0 comments on commit 3810b48

Please sign in to comment.