From 467fb7bb8fb27e6a943e007799f4f224085044c6 Mon Sep 17 00:00:00 2001 From: amercader Date: Mon, 13 Jan 2014 10:30:52 +0000 Subject: [PATCH] Fix resource updating for harvested datasets Starting from 2.2, resource_update calls package_show before updating the resource via a package_update call. The dict passed had the harvest extras (eg harvest_object_id) added which made the update call fails due to duplicated extra keys. To fix it we now remove any harvest extras on after_show if there is a 'for_edit' property on the context. --- ckanext/harvest/plugin.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ckanext/harvest/plugin.py b/ckanext/harvest/plugin.py index abfb66b..6b36bd8 100644 --- a/ckanext/harvest/plugin.py +++ b/ckanext/harvest/plugin.py @@ -95,7 +95,21 @@ class Harvest(p.SingletonPlugin, DefaultDatasetForm): .filter(HarvestObject.current==True) \ .first() - # validate is false is passed only on indexing. + if context.get('for_edit'): + # If we are editing the dataset, check if the harvest extras + # are there, and if so, remove them. This can happen eg when + # doing resource_update, which calls package_show + data_dict['extras'][:] = [e for e in data_dict.get('extras', []) + if not e['key'] + in ('harvest_object_id', 'harvest_source_id', 'harvest_source_title',)] + + + # We only want to add these extras at index time so they are part + # of the cached data_dict used to display, search results etc. We + # don't want them added when editing the dataset, otherwise we get + # duplicated key errors. + # The only way to detect indexing right now is checking that + # validate is set to False. if harvest_object and not context.get('validate', True): for key, value in [ ('harvest_object_id', harvest_object.id),