2011-04-19 15:54:59 +02:00
|
|
|
import urllib2
|
|
|
|
|
2011-11-18 14:20:41 +01:00
|
|
|
from ckan.lib.base import c
|
|
|
|
from ckan import model
|
2011-06-02 12:07:07 +02:00
|
|
|
from ckan.model import Session, Package
|
2011-11-18 14:20:41 +01:00
|
|
|
from ckan.logic import ValidationError, NotFound, get_action
|
2011-04-19 15:54:59 +02:00
|
|
|
from ckan.lib.helpers import json
|
2015-01-15 00:09:43 +01:00
|
|
|
from ckan.lib.munge import munge_name
|
2011-04-19 15:54:59 +02:00
|
|
|
|
2011-05-17 18:26:42 +02:00
|
|
|
from ckanext.harvest.model import HarvestJob, HarvestObject, HarvestGatherError, \
|
2011-04-19 15:54:59 +02:00
|
|
|
HarvestObjectError
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2011-06-02 12:07:07 +02:00
|
|
|
from base import HarvesterBase
|
2011-04-19 15:54:59 +02:00
|
|
|
|
2011-06-02 12:07:07 +02:00
|
|
|
class CKANHarvester(HarvesterBase):
|
2011-04-19 15:54:59 +02:00
|
|
|
'''
|
|
|
|
A Harvester for CKAN instances
|
|
|
|
'''
|
2011-06-07 14:35:11 +02:00
|
|
|
config = None
|
2011-05-17 18:26:42 +02:00
|
|
|
|
2013-05-22 16:46:14 +02:00
|
|
|
api_version = 2
|
2015-01-13 14:46:14 +01:00
|
|
|
action_api_version = 3
|
2011-05-17 18:26:42 +02:00
|
|
|
|
|
|
|
def _get_rest_api_offset(self):
|
2013-05-22 16:46:14 +02:00
|
|
|
return '/api/%d/rest' % self.api_version
|
2011-05-17 18:26:42 +02:00
|
|
|
|
2015-01-13 14:46:14 +01:00
|
|
|
def _get_action_api_offset(self):
|
|
|
|
return '/api/%d/action' % self.action_api_version
|
|
|
|
|
2011-05-17 18:26:42 +02:00
|
|
|
def _get_search_api_offset(self):
|
2013-05-22 16:46:14 +02:00
|
|
|
return '/api/%d/search' % self.api_version
|
2011-04-19 15:54:59 +02:00
|
|
|
|
|
|
|
def _get_content(self, url):
|
|
|
|
http_request = urllib2.Request(
|
|
|
|
url = url,
|
|
|
|
)
|
|
|
|
|
2013-02-28 20:06:21 +01:00
|
|
|
api_key = self.config.get('api_key',None)
|
|
|
|
if api_key:
|
|
|
|
http_request.add_header('Authorization',api_key)
|
2011-04-19 15:54:59 +02:00
|
|
|
|
2015-01-15 00:07:26 +01:00
|
|
|
try:
|
|
|
|
http_response = urllib2.urlopen(http_request)
|
2015-01-15 10:57:24 +01:00
|
|
|
except urllib2.URLError, e:
|
2015-01-15 00:07:26 +01:00
|
|
|
raise ContentFetchError(
|
2015-01-15 10:57:24 +01:00
|
|
|
'Could not fetch url: %s, error: %s' %
|
2015-01-15 11:36:15 +01:00
|
|
|
(url, str(e))
|
2015-01-15 00:07:26 +01:00
|
|
|
)
|
2013-02-28 20:06:21 +01:00
|
|
|
return http_response.read()
|
2011-04-19 15:54:59 +02:00
|
|
|
|
2013-05-24 17:55:05 +02:00
|
|
|
def _get_group(self, base_url, group_name):
|
2015-01-15 00:09:43 +01:00
|
|
|
url = base_url + self._get_rest_api_offset() + '/group/' + munge_name(group_name)
|
2013-05-24 17:55:05 +02:00
|
|
|
try:
|
|
|
|
content = self._get_content(url)
|
|
|
|
return json.loads(content)
|
2015-01-15 00:07:26 +01:00
|
|
|
except (ContentFetchError, ValueError):
|
|
|
|
log.debug('Could not fetch/decode remote group');
|
|
|
|
raise RemoteResourceError('Could not fetch/decode remote group')
|
2013-05-24 17:55:05 +02:00
|
|
|
|
2015-01-13 14:46:14 +01:00
|
|
|
def _get_organization(self, base_url, org_name):
|
|
|
|
url = base_url + self._get_action_api_offset() + '/organization_show?id=' + org_name
|
|
|
|
try:
|
|
|
|
content = self._get_content(url)
|
|
|
|
content_dict = json.loads(content)
|
|
|
|
return content_dict['result']
|
2015-01-15 00:07:26 +01:00
|
|
|
except (ContentFetchError, ValueError, KeyError):
|
|
|
|
log.debug('Could not fetch/decode remote group');
|
|
|
|
raise RemoteResourceError('Could not fetch/decode remote organization')
|
2015-01-13 14:46:14 +01:00
|
|
|
|
2011-06-07 14:35:11 +02:00
|
|
|
def _set_config(self,config_str):
|
|
|
|
if config_str:
|
|
|
|
self.config = json.loads(config_str)
|
2013-08-15 15:37:55 +02:00
|
|
|
if 'api_version' in self.config:
|
|
|
|
self.api_version = int(self.config['api_version'])
|
2011-07-18 18:35:32 +02:00
|
|
|
|
2011-06-07 14:35:11 +02:00
|
|
|
log.debug('Using config: %r', self.config)
|
|
|
|
else:
|
|
|
|
self.config = {}
|
|
|
|
|
2011-05-13 19:39:36 +02:00
|
|
|
def info(self):
|
|
|
|
return {
|
|
|
|
'name': 'ckan',
|
|
|
|
'title': 'CKAN',
|
2011-06-07 13:07:53 +02:00
|
|
|
'description': 'Harvests remote CKAN instances',
|
|
|
|
'form_config_interface':'Text'
|
2011-05-13 19:39:36 +02:00
|
|
|
}
|
2011-04-19 15:54:59 +02:00
|
|
|
|
2011-06-07 13:07:53 +02:00
|
|
|
def validate_config(self,config):
|
2011-06-28 16:04:40 +02:00
|
|
|
if not config:
|
|
|
|
return config
|
|
|
|
|
2011-06-07 13:07:53 +02:00
|
|
|
try:
|
|
|
|
config_obj = json.loads(config)
|
2011-11-18 14:20:41 +01:00
|
|
|
|
2013-05-31 18:23:40 +02:00
|
|
|
if 'api_version' in config_obj:
|
|
|
|
try:
|
|
|
|
int(config_obj['api_version'])
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError('api_version must be an integer')
|
|
|
|
|
2012-01-10 18:07:19 +01:00
|
|
|
if 'default_tags' in config_obj:
|
|
|
|
if not isinstance(config_obj['default_tags'],list):
|
|
|
|
raise ValueError('default_tags must be a list')
|
|
|
|
|
2011-11-18 14:20:41 +01:00
|
|
|
if 'default_groups' in config_obj:
|
2012-01-10 18:07:19 +01:00
|
|
|
if not isinstance(config_obj['default_groups'],list):
|
|
|
|
raise ValueError('default_groups must be a list')
|
|
|
|
|
2011-11-18 14:20:41 +01:00
|
|
|
# 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')
|
2011-11-18 15:12:30 +01:00
|
|
|
|
2012-01-10 18:07:19 +01:00
|
|
|
if 'default_extras' in config_obj:
|
|
|
|
if not isinstance(config_obj['default_extras'],dict):
|
|
|
|
raise ValueError('default_extras must be a dictionary')
|
|
|
|
|
2011-11-18 15:12:30 +01:00
|
|
|
if 'user' in config_obj:
|
|
|
|
# Check if user exists
|
|
|
|
context = {'model':model,'user':c.user}
|
|
|
|
try:
|
|
|
|
user = get_action('user_show')(context,{'id':config_obj.get('user')})
|
|
|
|
except NotFound,e:
|
|
|
|
raise ValueError('User not found')
|
2011-11-18 14:20:41 +01:00
|
|
|
|
2012-02-03 18:54:34 +01:00
|
|
|
for key in ('read_only','force_all'):
|
|
|
|
if key in config_obj:
|
|
|
|
if not isinstance(config_obj[key],bool):
|
|
|
|
raise ValueError('%s must be boolean' % key)
|
|
|
|
|
2011-06-07 13:07:53 +02:00
|
|
|
except ValueError,e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2011-04-19 15:54:59 +02:00
|
|
|
def gather_stage(self,harvest_job):
|
2011-05-17 18:26:42 +02:00
|
|
|
log.debug('In CKANHarvester gather_stage (%s)' % harvest_job.source.url)
|
|
|
|
get_all_packages = True
|
|
|
|
package_ids = []
|
|
|
|
|
2011-06-14 16:59:13 +02:00
|
|
|
self._set_config(harvest_job.source.config)
|
2011-06-07 14:35:11 +02:00
|
|
|
|
2011-05-17 18:26:42 +02:00
|
|
|
# Check if this source has been harvested before
|
|
|
|
previous_job = Session.query(HarvestJob) \
|
|
|
|
.filter(HarvestJob.source==harvest_job.source) \
|
|
|
|
.filter(HarvestJob.gather_finished!=None) \
|
|
|
|
.filter(HarvestJob.id!=harvest_job.id) \
|
|
|
|
.order_by(HarvestJob.gather_finished.desc()) \
|
|
|
|
.limit(1).first()
|
2011-04-19 15:54:59 +02:00
|
|
|
|
|
|
|
# Get source URL
|
2011-05-17 18:26:42 +02:00
|
|
|
base_url = harvest_job.source.url.rstrip('/')
|
|
|
|
base_rest_url = base_url + self._get_rest_api_offset()
|
|
|
|
base_search_url = base_url + self._get_search_api_offset()
|
2011-06-07 13:07:53 +02:00
|
|
|
|
2012-03-15 12:31:12 +01:00
|
|
|
if (previous_job and not previous_job.gather_errors and not len(previous_job.objects) == 0):
|
|
|
|
if not self.config.get('force_all',False):
|
|
|
|
get_all_packages = False
|
2011-04-19 15:54:59 +02:00
|
|
|
|
2012-03-15 12:31:12 +01:00
|
|
|
# Request only the packages modified since last harvest job
|
2013-02-28 20:06:21 +01:00
|
|
|
last_time = previous_job.gather_finished.isoformat()
|
2012-03-15 12:31:12 +01:00
|
|
|
url = base_search_url + '/revision?since_time=%s' % last_time
|
2011-05-17 18:26:42 +02:00
|
|
|
|
2012-03-15 12:31:12 +01:00
|
|
|
try:
|
|
|
|
content = self._get_content(url)
|
|
|
|
|
|
|
|
revision_ids = json.loads(content)
|
|
|
|
if len(revision_ids):
|
|
|
|
for revision_id in revision_ids:
|
|
|
|
url = base_rest_url + '/revision/%s' % revision_id
|
|
|
|
try:
|
|
|
|
content = self._get_content(url)
|
2015-01-15 00:49:11 +01:00
|
|
|
except ContentFetchError,e:
|
2012-03-15 12:31:12 +01:00
|
|
|
self._save_gather_error('Unable to get content for URL: %s: %s' % (url, str(e)),harvest_job)
|
|
|
|
continue
|
|
|
|
|
|
|
|
revision = json.loads(content)
|
2013-02-28 20:06:21 +01:00
|
|
|
for package_id in revision['packages']:
|
2012-03-15 12:31:12 +01:00
|
|
|
if not package_id in package_ids:
|
|
|
|
package_ids.append(package_id)
|
|
|
|
else:
|
|
|
|
log.info('No packages have been updated on the remote CKAN instance since the last harvest job')
|
|
|
|
return None
|
|
|
|
|
|
|
|
except urllib2.HTTPError,e:
|
|
|
|
if e.getcode() == 400:
|
|
|
|
log.info('CKAN instance %s does not suport revision filtering' % base_url)
|
|
|
|
get_all_packages = True
|
|
|
|
else:
|
|
|
|
self._save_gather_error('Unable to get content for URL: %s: %s' % (url, str(e)),harvest_job)
|
|
|
|
return None
|
2011-05-17 18:26:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if get_all_packages:
|
|
|
|
# Request all remote packages
|
|
|
|
url = base_rest_url + '/package'
|
|
|
|
try:
|
|
|
|
content = self._get_content(url)
|
2015-01-15 00:49:11 +01:00
|
|
|
except ContentFetchError,e:
|
2011-05-17 18:26:42 +02:00
|
|
|
self._save_gather_error('Unable to get content for URL: %s: %s' % (url, str(e)),harvest_job)
|
|
|
|
return None
|
2011-04-19 15:54:59 +02:00
|
|
|
|
|
|
|
package_ids = json.loads(content)
|
2011-05-17 18:26:42 +02:00
|
|
|
|
|
|
|
try:
|
2011-04-19 15:54:59 +02:00
|
|
|
object_ids = []
|
|
|
|
if len(package_ids):
|
|
|
|
for package_id in package_ids:
|
|
|
|
# Create a new HarvestObject for this identifier
|
|
|
|
obj = HarvestObject(guid = package_id, job = harvest_job)
|
|
|
|
obj.save()
|
|
|
|
object_ids.append(obj.id)
|
|
|
|
|
|
|
|
return object_ids
|
|
|
|
|
|
|
|
else:
|
2011-06-07 13:07:53 +02:00
|
|
|
self._save_gather_error('No packages received for URL: %s' % url,
|
2011-06-02 12:07:07 +02:00
|
|
|
harvest_job)
|
2011-04-19 15:54:59 +02:00
|
|
|
return None
|
|
|
|
except Exception, e:
|
|
|
|
self._save_gather_error('%r'%e.message,harvest_job)
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_stage(self,harvest_object):
|
|
|
|
log.debug('In CKANHarvester fetch_stage')
|
2011-06-07 14:35:11 +02:00
|
|
|
|
2011-06-14 16:59:13 +02:00
|
|
|
self._set_config(harvest_object.job.source.config)
|
2011-06-07 14:35:11 +02:00
|
|
|
|
2011-04-19 15:54:59 +02:00
|
|
|
# Get source URL
|
|
|
|
url = harvest_object.source.url.rstrip('/')
|
2011-05-17 18:26:42 +02:00
|
|
|
url = url + self._get_rest_api_offset() + '/package/' + harvest_object.guid
|
2011-04-19 15:54:59 +02:00
|
|
|
|
|
|
|
# Get contents
|
|
|
|
try:
|
|
|
|
content = self._get_content(url)
|
2015-01-15 00:49:11 +01:00
|
|
|
except ContentFetchError,e:
|
2011-04-19 15:54:59 +02:00
|
|
|
self._save_object_error('Unable to get content for package: %s: %r' % \
|
|
|
|
(url, e),harvest_object)
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Save the fetched contents in the HarvestObject
|
|
|
|
harvest_object.content = content
|
|
|
|
harvest_object.save()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def import_stage(self,harvest_object):
|
|
|
|
log.debug('In CKANHarvester import_stage')
|
|
|
|
if not harvest_object:
|
|
|
|
log.error('No harvest object received')
|
|
|
|
return False
|
|
|
|
|
|
|
|
if harvest_object.content is None:
|
2011-06-07 13:07:53 +02:00
|
|
|
self._save_object_error('Empty content for object %s' % harvest_object.id,
|
2011-06-02 12:07:07 +02:00
|
|
|
harvest_object, 'Import')
|
2011-04-19 15:54:59 +02:00
|
|
|
return False
|
|
|
|
|
2011-06-14 16:59:13 +02:00
|
|
|
self._set_config(harvest_object.job.source.config)
|
2011-06-07 14:35:11 +02:00
|
|
|
|
2011-06-02 12:07:07 +02:00
|
|
|
try:
|
2011-04-19 15:54:59 +02:00
|
|
|
package_dict = json.loads(harvest_object.content)
|
2011-11-18 14:20:41 +01:00
|
|
|
|
2013-08-15 15:38:33 +02:00
|
|
|
if package_dict.get('type') == 'harvest':
|
|
|
|
log.warn('Remote dataset is a harvest source, ignoring...')
|
2013-10-23 07:40:55 +02:00
|
|
|
return True
|
2013-08-15 15:38:33 +02:00
|
|
|
|
2011-11-18 14:20:41 +01:00
|
|
|
# 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']])
|
|
|
|
|
2013-05-24 17:55:05 +02:00
|
|
|
remote_groups = self.config.get('remote_groups', None)
|
2013-05-30 19:06:15 +02:00
|
|
|
if not remote_groups in ('only_local', 'create'):
|
|
|
|
# Ignore remote groups
|
|
|
|
package_dict.pop('groups', None)
|
|
|
|
else:
|
|
|
|
if not 'groups' in package_dict:
|
|
|
|
package_dict['groups'] = []
|
|
|
|
|
2013-05-24 17:55:05 +02:00
|
|
|
# check if remote groups exist locally, otherwise remove
|
|
|
|
validated_groups = []
|
|
|
|
context = {'model': model, 'session': Session, 'user': 'harvest'}
|
|
|
|
|
|
|
|
for group_name in package_dict['groups']:
|
|
|
|
try:
|
|
|
|
data_dict = {'id': group_name}
|
|
|
|
group = get_action('group_show')(context, data_dict)
|
2013-05-22 16:46:14 +02:00
|
|
|
if self.api_version == 1:
|
2013-05-24 17:55:05 +02:00
|
|
|
validated_groups.append(group['name'])
|
|
|
|
else:
|
|
|
|
validated_groups.append(group['id'])
|
|
|
|
except NotFound, e:
|
|
|
|
log.info('Group %s is not available' % group_name)
|
|
|
|
if remote_groups == 'create':
|
|
|
|
try:
|
|
|
|
group = self._get_group(harvest_object.source.url, group_name)
|
2015-01-15 00:07:26 +01:00
|
|
|
except RemoteResourceError:
|
2013-05-24 17:55:05 +02:00
|
|
|
log.error('Could not get remote group %s' % group_name)
|
|
|
|
continue
|
|
|
|
|
|
|
|
for key in ['packages', 'created', 'users', 'groups', 'tags', 'extras', 'display_name']:
|
|
|
|
group.pop(key, None)
|
|
|
|
get_action('group_create')(context, group)
|
|
|
|
log.info('Group %s has been newly created' % group_name)
|
2013-05-27 13:36:56 +02:00
|
|
|
if self.api_version == 1:
|
2013-05-24 17:55:05 +02:00
|
|
|
validated_groups.append(group['name'])
|
|
|
|
else:
|
|
|
|
validated_groups.append(group['id'])
|
|
|
|
|
|
|
|
package_dict['groups'] = validated_groups
|
2013-05-16 18:30:54 +02:00
|
|
|
|
2013-10-22 17:24:43 +02:00
|
|
|
context = {'model': model, 'session': Session, 'user': 'harvest'}
|
|
|
|
|
|
|
|
# Local harvest source organization
|
|
|
|
source_dataset = get_action('package_show')(context, {'id': harvest_object.source.id})
|
|
|
|
local_org = source_dataset.get('owner_org')
|
|
|
|
|
2013-10-04 15:34:22 +02:00
|
|
|
remote_orgs = self.config.get('remote_orgs', None)
|
2013-10-22 17:24:43 +02:00
|
|
|
|
2013-10-07 11:22:19 +02:00
|
|
|
if not remote_orgs in ('only_local', 'create'):
|
2013-10-22 17:24:43 +02:00
|
|
|
# Assign dataset to the source organization
|
|
|
|
package_dict['owner_org'] = local_org
|
2013-10-04 15:34:22 +02:00
|
|
|
else:
|
|
|
|
if not 'owner_org' in package_dict:
|
|
|
|
package_dict['owner_org'] = None
|
|
|
|
|
|
|
|
# check if remote org exist locally, otherwise remove
|
|
|
|
validated_org = None
|
|
|
|
remote_org = package_dict['owner_org']
|
2013-10-22 17:24:43 +02:00
|
|
|
|
2013-10-11 18:08:32 +02:00
|
|
|
if remote_org:
|
|
|
|
try:
|
|
|
|
data_dict = {'id': remote_org}
|
|
|
|
org = get_action('organization_show')(context, data_dict)
|
|
|
|
validated_org = org['id']
|
|
|
|
except NotFound, e:
|
|
|
|
log.info('Organization %s is not available' % remote_org)
|
|
|
|
if remote_orgs == 'create':
|
|
|
|
try:
|
2015-01-14 00:10:27 +01:00
|
|
|
try:
|
|
|
|
org = self._get_organization(harvest_object.source.url, remote_org)
|
2015-01-15 00:07:26 +01:00
|
|
|
except RemoteResourceError:
|
2015-01-14 00:10:27 +01:00
|
|
|
# fallback if remote CKAN exposes organizations as groups
|
|
|
|
# this especially targets older versions of CKAN
|
|
|
|
org = self._get_group(harvest_object.source.url, remote_org)
|
|
|
|
|
2013-10-11 18:08:32 +02:00
|
|
|
for key in ['packages', 'created', 'users', 'groups', 'tags', 'extras', 'display_name', 'type']:
|
|
|
|
org.pop(key, None)
|
|
|
|
get_action('organization_create')(context, org)
|
|
|
|
log.info('Organization %s has been newly created' % remote_org)
|
|
|
|
validated_org = org['id']
|
2015-01-15 00:07:26 +01:00
|
|
|
except (RemoteResourceError, ValidationError):
|
2013-10-11 18:08:32 +02:00
|
|
|
log.error('Could not get remote org %s' % remote_org)
|
2013-10-04 15:34:22 +02:00
|
|
|
|
2013-10-22 17:24:43 +02:00
|
|
|
package_dict['owner_org'] = validated_org or local_org
|
2011-11-18 14:20:41 +01:00
|
|
|
|
|
|
|
# Set default groups if needed
|
2013-05-24 17:55:05 +02:00
|
|
|
default_groups = self.config.get('default_groups', [])
|
2011-11-18 14:20:41 +01:00
|
|
|
if default_groups:
|
2014-02-10 14:16:58 +01:00
|
|
|
if not 'groups' in package_dict:
|
|
|
|
package_dict['groups'] = []
|
2011-11-18 14:20:41 +01:00
|
|
|
package_dict['groups'].extend([g for g in default_groups if g not in package_dict['groups']])
|
|
|
|
|
2013-05-31 20:35:06 +02:00
|
|
|
# Find any extras whose values are not strings and try to convert
|
|
|
|
# them to strings, as non-string extras are not allowed anymore in
|
|
|
|
# CKAN 2.0.
|
2013-05-31 14:56:53 +02:00
|
|
|
for key in package_dict['extras'].keys():
|
|
|
|
if not isinstance(package_dict['extras'][key], basestring):
|
2013-05-31 20:35:06 +02:00
|
|
|
try:
|
|
|
|
package_dict['extras'][key] = json.dumps(
|
|
|
|
package_dict['extras'][key])
|
|
|
|
except TypeError:
|
|
|
|
# If converting to a string fails, just delete it.
|
|
|
|
del package_dict['extras'][key]
|
2013-05-31 14:56:53 +02:00
|
|
|
|
2012-01-10 18:07:19 +01:00
|
|
|
# Set default extras if needed
|
|
|
|
default_extras = self.config.get('default_extras',{})
|
|
|
|
if default_extras:
|
|
|
|
override_extras = self.config.get('override_extras',False)
|
|
|
|
if not 'extras' in package_dict:
|
|
|
|
package_dict['extras'] = {}
|
|
|
|
for key,value in default_extras.iteritems():
|
|
|
|
if not key in package_dict['extras'] or override_extras:
|
|
|
|
# Look for replacement strings
|
|
|
|
if isinstance(value,basestring):
|
|
|
|
value = value.format(harvest_source_id=harvest_object.job.source.id,
|
|
|
|
harvest_source_url=harvest_object.job.source.url.strip('/'),
|
2012-03-13 13:38:14 +01:00
|
|
|
harvest_source_title=harvest_object.job.source.title,
|
2012-01-10 18:07:19 +01:00
|
|
|
harvest_job_id=harvest_object.job.id,
|
|
|
|
harvest_object_id=harvest_object.id,
|
|
|
|
dataset_id=package_dict['id'])
|
2012-03-13 13:38:14 +01:00
|
|
|
|
2012-01-10 18:07:19 +01:00
|
|
|
package_dict['extras'][key] = value
|
|
|
|
|
2014-02-11 18:27:19 +01:00
|
|
|
# Clear remote url_type for resources (eg datastore, upload) as we
|
|
|
|
# are only creating normal resources with links to the remote ones
|
|
|
|
for resource in package_dict.get('resources', []):
|
|
|
|
resource.pop('url_type', None)
|
|
|
|
|
2011-11-18 15:30:10 +01:00
|
|
|
result = self._create_or_update_package(package_dict,harvest_object)
|
|
|
|
|
|
|
|
if result and self.config.get('read_only',False) == True:
|
|
|
|
|
|
|
|
package = model.Package.get(package_dict['id'])
|
|
|
|
|
|
|
|
# Clear default permissions
|
|
|
|
model.clear_user_roles(package)
|
|
|
|
|
|
|
|
# Setup harvest user as admin
|
|
|
|
user_name = self.config.get('user',u'harvest')
|
|
|
|
user = model.User.get(user_name)
|
|
|
|
pkg_role = model.PackageRole(package=package, user=user, role=model.Role.ADMIN)
|
|
|
|
|
|
|
|
# Other users can only read
|
|
|
|
for user_name in (u'visitor',u'logged_in'):
|
|
|
|
user = model.User.get(user_name)
|
|
|
|
pkg_role = model.PackageRole(package=package, user=user, role=model.Role.READER)
|
|
|
|
|
|
|
|
|
2013-02-22 11:13:36 +01:00
|
|
|
return True
|
2011-04-19 15:54:59 +02:00
|
|
|
except ValidationError,e:
|
2011-06-07 13:07:53 +02:00
|
|
|
self._save_object_error('Invalid package with GUID %s: %r' % (harvest_object.guid, e.error_dict),
|
2011-06-02 12:07:07 +02:00
|
|
|
harvest_object, 'Import')
|
2011-04-19 15:54:59 +02:00
|
|
|
except Exception, e:
|
|
|
|
self._save_object_error('%r'%e,harvest_object,'Import')
|
|
|
|
|
2015-01-15 00:07:26 +01:00
|
|
|
class ContentFetchError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class RemoteResourceError(Exception):
|
|
|
|
pass
|