From aa7f2b23499c259caa83b91481a668d85359e5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Tue, 26 Oct 2021 13:46:55 +0200 Subject: [PATCH] ref: mix IResourceController and IPackageController (partly https://github.com/DCOR-dev/DCOR-Aid/issues/28) --- ckanext/dcor_schemas/plugin.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ckanext/dcor_schemas/plugin.py b/ckanext/dcor_schemas/plugin.py index 999c2cc..f96e9fe 100644 --- a/ckanext/dcor_schemas/plugin.py +++ b/ckanext/dcor_schemas/plugin.py @@ -44,6 +44,7 @@ class DCORDatasetFormPlugin(plugins.SingletonPlugin, plugins.implements(plugins.IDatasetForm) plugins.implements(plugins.IPermissionLabels) plugins.implements(plugins.IResourceController, inherit=True) + plugins.implements(plugins.IPackageController, inherit=True) plugins.implements(plugins.ITemplateHelpers) # IActions @@ -267,9 +268,15 @@ def get_user_dataset_labels(self, user_obj): labels.extend(u'group-%s' % o['id'] for o in grps) return labels - # IResourceController + # IResourceController and IPackageController def after_create(self, context, resource): """Add custom jobs""" + if resource.get("package_id") is None: + # `resource` is a package dictionary, because we implemented + # both IResourceController and IPackageController. Stop here. + # https://github.com/ckan/ckan/issues/2949 + return + # It turns out that package_revise might not be as atomic as I # thought (because it also calls package_show). Therefore, we # make all the jobs that interact with the database sequential. @@ -322,7 +329,27 @@ def after_create(self, context, resource): "job_id": package_job_id + "sha256", "depends_on": copy.copy(depends_on)}) + def after_update(self, context, pkg_dict): + if pkg_dict.get("package_id") is not None: + # `pkg_dict` is a resource dictionary, because we implemented + # both IResourceController and IPackageController. Stop here. + # https://github.com/ckan/ckan/issues/2949 + return + + # On DCOR, "active" means that no changes can be made anymore. + # So we may proceed with running all jobs. + if pkg_dict["state"] == "active": + # Run all jobs that should be ran after resource creation. + # Note that some of the jobs are added twice, because rq + # does not do job uniqueness. But the implementation of the + # jobs is such that this should not be a problem. + for plugin in plugins.PluginImplementations( + plugins.IResourceController): + for resource in pkg_dict["resources"]: + plugin.after_create(context, resource) + def before_create(self, context, resource): + # IPackageController does not implement before_create if "upload" in resource: # set/override the filename upload = resource["upload"]