diff --git a/ckanext/datapusher_plus/datastore_utils.py b/ckanext/datapusher_plus/datastore_utils.py index 8500f47..1851505 100644 --- a/ckanext/datapusher_plus/datastore_utils.py +++ b/ckanext/datapusher_plus/datastore_utils.py @@ -25,11 +25,23 @@ def default(self, obj: object) -> object: return json.JSONEncoder.default(self, obj) +def _site_user_context(): + """A context that names the site user. + + From CKAN 2.11 the activity plugin requires context["user"] on every + action that records an activity (datastore_create and datastore_delete + both patch the resource; resource_delete records one too), and raises + ValidationError({"user_id": ["User not found"]}) otherwise. + """ + site_user = tk.get_action("get_site_user")({"ignore_auth": True}, {}) + return {"ignore_auth": True, "user": site_user["name"]} + + def delete_datastore_resource(resource_id: str) -> None: """Delete a resource from datastore.""" try: tk.get_action("datastore_delete")( - {"ignore_auth": True}, {"resource_id": resource_id, "force": True} + _site_user_context(), {"resource_id": resource_id, "force": True} ) except tk.ObjectNotFound: raise utils.JobError("Deleting existing datastore failed.") @@ -39,7 +51,7 @@ def delete_resource(resource_id: str) -> None: """Delete a resource from CKAN.""" try: tk.get_action("resource_delete")( - {"ignore_auth": True}, {"id": resource_id, "force": True} + _site_user_context(), {"id": resource_id, "force": True} ) except tk.ObjectNotFound: raise utils.JobError("Deleting existing resource failed.") @@ -160,7 +172,7 @@ def send_resource_to_datastore( } try: resource_dict = tk.get_action("datastore_create")( - {"ignore_auth": True}, request + _site_user_context(), request ) return resource_dict except Exception as e: