Skip to content

Commit

Permalink
Adding validator and parser for default_groups and tags
Browse files Browse the repository at this point in the history
       - Work done by Tobias Schulmann <[email protected]>
       - Adding the validation that is done in ckanext-harvestor to here for consistencey
       - originally done  Wed Dec 14 09:46:10 2016 +1300
  • Loading branch information
dythya authored and ebuckley committed Oct 3, 2018
1 parent 2acf66b commit 8f7d23d
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions ckanext/spatial/harvesters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,20 @@ def validate_config(self, source_config):
if 'default_tags' in source_config_obj:
if not isinstance(source_config_obj['default_tags'],list):
raise ValueError('default_tags must be a list')
elif not isinstance(source_config_obj['default_tags'][0], dict):
# This check is taken from ckanext-harvest CKANHarvester to ensure consistency
# between harversters.
raise ValueError('default_tags must be a list of dicts like {"name": "name-of-tag"}')

if 'default_extras' in source_config_obj:
if not isinstance(source_config_obj['default_extras'],dict):
raise ValueError('default_extras must be a dictionary')

if 'default_groups' in source_config_obj:
# Also taken from CKANHarvester to ensure consistency.
if not isinstance(source_config_obj['default_groups'], list):
raise ValueError('default_groups must be a *list* of group names/ids')

for key in ('override_extras', 'clean_tags'):
if key in source_config_obj:
if not isinstance(source_config_obj[key],bool):
Expand Down Expand Up @@ -211,17 +220,29 @@ def get_package_dict(self, context, data_dict):
tags_val = [munge_tag(tag) if do_clean else tag[:100] for tag in iso_values['tags']]
tags = [{'name': tag} for tag in tags_val]

# Add default_tags from config
default_tags = self.source_config.get('default_tags', [])
if default_tags:
for tag in default_tags:
tags.append({'name': tag})

# Add default_tags from config.
# For consistency with CKANHarvester `default_tags` is now a list of
# dicts, which we can add to tags without further parsing.
tags.extend(self.source_config.get('default_tags', []))

# Adding default_groups from config. This was previously not supported
# by ckanext-spatial.
context = {'model': model, 'user': p.toolkit.c.user}
groups = []
for group_name_or_id in self.source_config.get('default_groups', []):
try:
group = p.toolkit.get_action('group_show')(context, {'id': group_name_or_id})
groups.append({'id': group['id'], 'name': group['name']})
except p.toolkit.ObjectNotFound, e:
logging.error('Default group %s not found, proceeding without.' % group_name_or_id)

package_dict = {
'title': iso_values['title'],
'notes': iso_values['abstract'],
'tags': tags,
'tags': dict((tag['name'], tag) for tag in tags).values(),
'resources': [],
'groups': dict((group['id'], group) for group in groups).values(),
}

# We need to get the owner organization (if any) from the harvest
Expand Down

0 comments on commit 8f7d23d

Please sign in to comment.