Commit ace1198b authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[entities] Do not push already pushed resources

Look for existing resources on CKAN side and compare with their url (download
url).
parent f0c22bd9116d
......@@ -64,6 +64,14 @@ class CKANPublishableAdapter(EntityAdapter):
# XXX CKAN uses Markdown syntax for notes field.
return self.entity.printable_value('description', format='text/plain')
def ckan_package_resources(self):
"""Return the list of resources URL for dataset"""
data = {'id': self.entity.ckan_dataset_id}
res = ckan_post(self._cw.vreg.config, 'package_show', data)
if res:
return [d['url'] for d in res['resources']]
return []
def ckan_get_organization_id(self, orgname):
"""Retrieve the ID of an organization given its name"""
data = {'organizations': [orgname],
......@@ -122,6 +130,7 @@ class CKANPublishableAdapter(EntityAdapter):
def ckan_resources(self):
"""Yield dicts of CKAN dataset resource info"""
resources_url = self.ckan_package_resources()
for entity in self.dataset_resources():
adapted = entity.cw_adapt_to('IDownloadable')
if adapted is None:
......@@ -129,7 +138,12 @@ class CKANPublishableAdapter(EntityAdapter):
'invalid resource %r, could not adapt to IDownloadable',
entity)
continue
yield {'url': adapted.download_url(),
url = adapted.download_url()
if url in resources_url:
self.info('skipping resource %s and it seems to be already '
'on CKAN side', entity)
continue
yield {'url': url,
'name': adapted.download_file_name(),
'mimetype': adapted.download_content_type(),
'created': str(entity.creation_date),
......
......@@ -89,6 +89,14 @@ class CKANPublishHooksTC(CubicWebTC):
r0 = resources[0]
iresource = resource.cw_adapt_to('IDownloadable')
self.assertEqual(r0['url'], iresource.download_url())
# Update the entity (should trigger another push to CKAN, but no new
# resource).
entity.cw_set(description=u'blurp')
cnx.commit()
result = ckan_post(self.ckan_config, 'package_show',
{'id': entity.ckan_dataset_id})
resources = result['resources']
self.assertEqual(len(resources), 1)
def _check_entity_delete(self, cnx, entity):
self.set_description('entity deletion')
......
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