Commit 51cf906e authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[hooks] Prevent ckanpublish hooks from being ran upon setting of CKAN...

[hooks] Prevent ckanpublish hooks from being ran upon setting of CKAN identifier attributes on dataset/resource

This avoids those hooks to be called "recursively" as this sometimes causes
unexpected behaviors.

As a result, compute the modification_date before the ckan_resource_id
attribute is set in tests so that CKAN last_modified attribute still matches
with this date.

Closes #4869936.
parent 1c5b5774bec7
......@@ -81,6 +81,7 @@ class DeleteCKANDataSetHook(hook.Hook):
adaptable('ICKANDataset') &
score_entity(lambda x: x.ckan_dataset_id))
events = ('before_delete_entity', )
category = 'ckanpublish.dataset'
def __call__(self):
CKANDatasetOp.get_instance(self._cw).add_data(self.entity.eid)
......@@ -92,6 +93,7 @@ class AddOrUpdateCKANDataSetHook(hook.Hook):
__select__ = (hook.Hook.__select__ & ckan_instance_configured &
adaptable('ICKANDataset'))
events = ('after_add_entity', 'after_update_entity', )
category = 'ckanpublish.dataset'
def __call__(self):
CKANDatasetOp.get_instance(self._cw).add_data(self.entity.eid)
......@@ -117,9 +119,10 @@ class CKANDatasetOp(hook.DataOperationMixIn, hook.Operation):
data.keys(), datasetid)
else:
datasetid = create_dataset(config, eid, data)
self.cnx.execute(
'SET X ckan_dataset_id %(dsid)s WHERE X eid %(eid)s',
{'eid': eid, 'dsid': datasetid})
with self.cnx.allow_all_hooks_but('ckanpublish.dataset'):
self.cnx.execute(
'SET X ckan_dataset_id %(dsid)s WHERE X eid %(eid)s',
{'eid': eid, 'dsid': datasetid})
self.info('created CKAN dataset %s', datasetid)
......@@ -129,6 +132,7 @@ class DeleteCKANResourceHook(hook.Hook):
__select__ = (hook.Hook.__select__ & ckan_instance_configured &
adaptable('ICKANResource') &
score_entity(lambda x: x.ckan_resource_id))
category = 'ckanpublish.resource'
events = ('before_delete_entity', )
def __call__(self):
......@@ -160,6 +164,7 @@ class LinkResourceToDatasetHook(hook.Hook):
__select__ = (hook.Hook.__select__ & ckan_instance_configured &
partial_match_rtype())
__abstract__ = True
category = 'ckanpublish.resource'
events = ('after_add_relation', )
rtype = None # Use to fill the `expected` argument of match_rtype.
role = 'object'
......@@ -176,6 +181,7 @@ class UpdateCKANResourceHook(hook.Hook):
__regid__ = 'ckanpublish.update-ckan-resource'
__select__ = (hook.Hook.__select__ & ckan_instance_configured &
adaptable('ICKANResource'))
category = 'ckanpublish.resource'
events = ('after_update_entity', )
def __call__(self):
......@@ -206,9 +212,10 @@ class CKANResourceOp(hook.DataOperationMixIn, hook.Operation):
continue
resourceid = create_dataset_resource(
config, eid, dataset.ckan_dataset_id, metadata, data)
self.cnx.execute(
'SET X ckan_resource_id %(rid)s WHERE X eid %(eid)s',
{'eid': eid, 'rid': resourceid})
with self.cnx.allow_all_hooks_but('ckanpublish.resource'):
self.cnx.execute(
'SET X ckan_resource_id %(rid)s WHERE X eid %(eid)s',
{'eid': eid, 'rid': resourceid})
self.info('added resource %s', resourceid)
else:
update_dataset_resource(
......
......@@ -80,6 +80,10 @@ class CKANPublishHooksTC(CKANPublishTestMixin, CubicWebTC):
def _check_resource_creation(self, cnx, dataset, resource):
self.set_description('resource creation')
# Keep the modification_date before update of the File due to setting
# the ckan_resource_id attribute as this event will not be propagated
# to CKAN instance since the corresponding hook is disabled.
resource_md = resource.modification_date.isoformat()
dataset.cw_set(resources=resource)
cnx.commit()
resource.cw_clear_all_caches()
......@@ -92,8 +96,7 @@ class CKANPublishHooksTC(CKANPublishTestMixin, CubicWebTC):
{'id': resource.ckan_resource_id})
self.assertEqual(result['created'],
resource.creation_date.isoformat())
self.assertEqual(result['last_modified'],
resource.modification_date.isoformat())
self.assertEqual(result['last_modified'], resource_md)
self.assertEqual(result['name'], u'blurp')
self.assertEqual(result['mimetype'], 'text/plain')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment