[ckan harvester] Support for default tags and groups
This commit is contained in:
parent
8ec05bc3e3
commit
2018d9e513
24
README.rst
24
README.rst
|
@ -113,6 +113,30 @@ to add the `ckan_harvester` plugin to your options file:
|
||||||
|
|
||||||
After adding it, a 'CKAN' option should appear in the 'New harvest source' form.
|
After adding it, a 'CKAN' option should appear in the 'New harvest source' form.
|
||||||
|
|
||||||
|
The CKAN harvesters support a number of configuration options to control their
|
||||||
|
behaviour. Those need to defined as a JSON object in the configuration form
|
||||||
|
field. The currently supported configuration options are:
|
||||||
|
|
||||||
|
* api_version: You can force the harvester to use eithoer version '1' or
|
||||||
|
'2' of the CKAN API. Default is '2'.
|
||||||
|
|
||||||
|
* default_tags: A list of tags that will be added to all harvested datasets.
|
||||||
|
Tags don't need to previously exist.
|
||||||
|
|
||||||
|
* default_groups: A list of groups to which the harvested datasets will be
|
||||||
|
added to. The groups must exist. Note that you must use ids or names to
|
||||||
|
define the groups according to the API version you defined (names for
|
||||||
|
version '1', ids for version '2')
|
||||||
|
|
||||||
|
Here is an example of a configuration object (the one that must be entered in
|
||||||
|
the configuration field)::
|
||||||
|
|
||||||
|
{
|
||||||
|
"api_version":"1",
|
||||||
|
"default_tags":["new-tag-1","new-tag-2"],
|
||||||
|
"default_groups":["my-own-group"]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
The harvesting interface
|
The harvesting interface
|
||||||
========================
|
========================
|
||||||
|
|
|
@ -106,11 +106,17 @@ class HarvesterBase(SingletonPlugin):
|
||||||
schema = default_package_schema()
|
schema = default_package_schema()
|
||||||
schema["id"] = [ignore_missing, unicode]
|
schema["id"] = [ignore_missing, unicode]
|
||||||
|
|
||||||
|
# Check API version
|
||||||
|
if self.config:
|
||||||
|
api_version = self.config.get('api_version','2')
|
||||||
|
else:
|
||||||
|
api_verion = '2'
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
'model': model,
|
'model': model,
|
||||||
'session': Session,
|
'session': Session,
|
||||||
'user': u'harvest',
|
'user': u'harvest',
|
||||||
'api_version':'2',
|
'api_version': api_version,
|
||||||
'schema': schema,
|
'schema': schema,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import urllib2
|
import urllib2
|
||||||
|
|
||||||
|
from ckan.lib.base import c
|
||||||
|
from ckan import model
|
||||||
from ckan.model import Session, Package
|
from ckan.model import Session, Package
|
||||||
from ckan.logic import ValidationError, NotFound
|
from ckan.logic import ValidationError, NotFound, get_action
|
||||||
from ckan.lib.helpers import json
|
from ckan.lib.helpers import json
|
||||||
|
|
||||||
from ckanext.harvest.model import HarvestJob, HarvestObject, HarvestGatherError, \
|
from ckanext.harvest.model import HarvestJob, HarvestObject, HarvestGatherError, \
|
||||||
|
@ -65,6 +67,16 @@ class CKANHarvester(HarvesterBase):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
config_obj = json.loads(config)
|
config_obj = json.loads(config)
|
||||||
|
|
||||||
|
if 'default_groups' in config_obj:
|
||||||
|
# Check if default groups exist
|
||||||
|
context = {'model':model,'user':c.user}
|
||||||
|
for group_name in config_obj['default_groups']:
|
||||||
|
try:
|
||||||
|
group = get_action('group_show')(context,{'id':group_name})
|
||||||
|
except NotFound,e:
|
||||||
|
raise ValueError('Default group not found')
|
||||||
|
|
||||||
except ValueError,e:
|
except ValueError,e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
@ -196,6 +208,24 @@ class CKANHarvester(HarvesterBase):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
package_dict = json.loads(harvest_object.content)
|
package_dict = json.loads(harvest_object.content)
|
||||||
|
|
||||||
|
# Set default tags if needed
|
||||||
|
default_tags = self.config.get('default_tags',[])
|
||||||
|
if default_tags:
|
||||||
|
if not 'tags' in package_dict:
|
||||||
|
package_dict['tags'] = []
|
||||||
|
package_dict['tags'].extend([t for t in default_tags if t not in package_dict['tags']])
|
||||||
|
|
||||||
|
# Ignore remote groups for the time being
|
||||||
|
del package_dict['groups']
|
||||||
|
|
||||||
|
# Set default groups if needed
|
||||||
|
default_groups = self.config.get('default_groups',[])
|
||||||
|
if default_groups:
|
||||||
|
if not 'groups' in package_dict:
|
||||||
|
package_dict['groups'] = []
|
||||||
|
package_dict['groups'].extend([g for g in default_groups if g not in package_dict['groups']])
|
||||||
|
|
||||||
return self._create_or_update_package(package_dict,harvest_object)
|
return self._create_or_update_package(package_dict,harvest_object)
|
||||||
except ValidationError,e:
|
except ValidationError,e:
|
||||||
self._save_object_error('Invalid package with GUID %s: %r' % (harvest_object.guid, e.error_dict),
|
self._save_object_error('Invalid package with GUID %s: %r' % (harvest_object.guid, e.error_dict),
|
||||||
|
|
Loading…
Reference in New Issue