Merge pull request #255 from ckan/253-default-groups

[#253] Fix default_groups
This commit is contained in:
Adrià Mercader 2016-07-01 20:45:46 +02:00 committed by GitHub
commit f8cfc63885
2 changed files with 13 additions and 7 deletions

View File

@ -128,20 +128,24 @@ class CKANHarvester(HarvesterBase):
raise ValueError('default_groups must be a *list* of group' raise ValueError('default_groups must be a *list* of group'
' names/ids') ' names/ids')
if config_obj['default_groups'] and \ if config_obj['default_groups'] and \
not isinstance(config_obj['default_groups'][0], str): not isinstance(config_obj['default_groups'][0],
basestring):
raise ValueError('default_groups must be a list of group ' raise ValueError('default_groups must be a list of group '
'names/ids (i.e. strings)') 'names/ids (i.e. strings)')
# Check if default groups exist # Check if default groups exist
context = {'model': model, 'user': toolkit.c.user} context = {'model': model, 'user': toolkit.c.user}
self.default_group_dicts = [] config_obj['default_group_dicts'] = []
for group_name_or_id in config_obj['default_groups']: for group_name_or_id in config_obj['default_groups']:
try: try:
group = get_action('group_show')( group = get_action('group_show')(
context, {'id': group_name_or_id}) context, {'id': group_name_or_id})
self.default_group_dicts.append(group) # save the dict to the config object, as we'll need it
# in the import_stage of every dataset
config_obj['default_group_dicts'].append(group)
except NotFound, e: except NotFound, e:
raise ValueError('Default group not found') raise ValueError('Default group not found')
config = json.dumps(config_obj)
if 'default_extras' in config_obj: if 'default_extras' in config_obj:
if not isinstance(config_obj['default_extras'], dict): if not isinstance(config_obj['default_extras'], dict):
@ -496,7 +500,7 @@ class CKANHarvester(HarvesterBase):
package_dict['groups'] = [] package_dict['groups'] = []
existing_group_ids = [g['id'] for g in package_dict['groups']] existing_group_ids = [g['id'] for g in package_dict['groups']]
package_dict['groups'].extend( package_dict['groups'].extend(
[g for g in self.default_group_dicts [g for g in self.config['default_group_dicts']
if g['id'] not in existing_group_ids]) if g['id'] not in existing_group_ids])
# Set default extras if needed # Set default extras if needed

View File

@ -134,12 +134,14 @@ def harvest_source_config_validator(key, data, errors, context):
if info['name'] == harvester_type: if info['name'] == harvester_type:
if hasattr(harvester, 'validate_config'): if hasattr(harvester, 'validate_config'):
try: try:
return harvester.validate_config(data[key]) config = harvester.validate_config(data[key])
except Exception, e: except Exception, e:
raise Invalid('Error parsing the configuration options: %s' raise Invalid('Error parsing the configuration options: %s'
% e) % e)
else: if config is not None:
return data[key] # save an edited config, for use during the harvest
data[key] = config
# no value is returned for this sort of validator/converter
def keep_not_empty_extras(key, data, errors, context): def keep_not_empty_extras(key, data, errors, context):