diff --git a/ckanext/dcat/harvesters/_json.py b/ckanext/dcat/harvesters/_json.py index b77f4f7b..d4a025ef 100644 --- a/ckanext/dcat/harvesters/_json.py +++ b/ckanext/dcat/harvesters/_json.py @@ -230,6 +230,7 @@ def import_stage(self, harvest_object): existing_dataset = self._get_existing_dataset(harvest_object.guid) if existing_dataset: copy_across_resource_ids(existing_dataset, package_dict) + copy_across_dataset_groups(existing_dataset, package_dict) # Allow custom harvesters to modify the package dict before creating # or updating the package @@ -343,3 +344,12 @@ def copy_across_resource_ids(existing_dataset, harvested_dataset): matching_existing_resource) if not existing_resources_still_to_match: break + + +def copy_across_dataset_groups(existing_dataset, harvested_dataset): + '''Copies the groups any existing datasets + are in to the newly harvested datasets''' + existing_groups = existing_dataset.get('groups') + + if existing_groups: + harvested_dataset['groups'] = existing_groups diff --git a/ckanext/dcat/tests/test_json_harvester.py b/ckanext/dcat/tests/test_json_harvester.py index 0ee1f1a5..e8cfd71c 100644 --- a/ckanext/dcat/tests/test_json_harvester.py +++ b/ckanext/dcat/tests/test_json_harvester.py @@ -8,7 +8,9 @@ import ckan.tests.factories as factories -from ckanext.dcat.harvesters._json import copy_across_resource_ids, DCATJSONHarvester +from ckanext.dcat.harvesters._json import (copy_across_resource_ids, + DCATJSONHarvester, + copy_across_dataset_groups) from test_harvester import FunctionalHarvestTest eq_ = nose.tools.eq_ @@ -323,3 +325,31 @@ def test_import_invalid_tags( assert 'Error importing dataset Invalid tags: ValidationError(None,)' in args[0] assert '{\'tags\': [{}, u\'Tag "test\\\'s" must be alphanumeric characters or symbols: -_.\', u\'Tag "invalid & wrong" must be alphanumeric characters or symbols: -_.\']}' in args[0] + + +class TestCopyAcrossDatasetGroups: + def test_groups_copied(self): + harvested_dataset = {} + existing_dataset = {'groups': [{ + 'display_name': u'education', + u'description': u'', + u'title': u'education', + 'image_display_url': u'', + u'id': u'ed944b74-6338-4cbd-83df-0927ff8ae8ab', + u'name': u'education' + }, { + 'display_name': u'other', + u'description': u'', + u'title': u'other', + 'image_display_url': u'', + u'id': u'95291d78-7c33-48e6-bd06-5751254e5bf7', + u'name': u'other'}] + } + + copy_across_dataset_groups( + existing_dataset, + harvested_dataset, + ) + + eq_(harvested_dataset['groups'][0].get('name'), + existing_dataset['groups'][0].get('name')) \ No newline at end of file