From 3810b48fb2fb6088bbacea9706668ffb783de1ba Mon Sep 17 00:00:00 2001 From: William Dutton Date: Wed, 11 Dec 2024 07:29:38 +1000 Subject: [PATCH] chore: alignment, move logic into logic folder and split auth and actions apart, move plugin from folder to root plugin.py --- .gitignore | 1 + ckanext/validation/logic/__init__.py | 0 .../validation/{logic.py => logic/action.py} | 42 ++++++------------- ckanext/validation/logic/auth.py | 39 +++++++++++++++++ .../{plugin/__init__.py => plugin.py} | 33 +++------------ 5 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 ckanext/validation/logic/__init__.py rename ckanext/validation/{logic.py => logic/action.py} (96%) create mode 100644 ckanext/validation/logic/auth.py rename ckanext/validation/{plugin/__init__.py => plugin.py} (90%) diff --git a/.gitignore b/.gitignore index 7bbc71c0..265a4a55 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,4 @@ ENV/ # mypy .mypy_cache/ +.idea diff --git a/ckanext/validation/logic/__init__.py b/ckanext/validation/logic/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ckanext/validation/logic.py b/ckanext/validation/logic/action.py similarity index 96% rename from ckanext/validation/logic.py rename to ckanext/validation/logic/action.py index 66ced821..0d4f8ec8 100644 --- a/ckanext/validation/logic.py +++ b/ckanext/validation/logic/action.py @@ -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) @@ -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 diff --git a/ckanext/validation/logic/auth.py b/ckanext/validation/logic/auth.py new file mode 100644 index 00000000..2139ac99 --- /dev/null +++ b/ckanext/validation/logic/auth.py @@ -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} diff --git a/ckanext/validation/plugin/__init__.py b/ckanext/validation/plugin.py similarity index 90% rename from ckanext/validation/plugin/__init__.py rename to ckanext/validation/plugin.py index c3e8b467..ddda1ab4 100644 --- a/ckanext/validation/plugin/__init__.py +++ b/ckanext/validation/plugin.py @@ -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, @@ -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