From 6c55aad2236fb9a829e67b738ee3bf9760a0bba4 Mon Sep 17 00:00:00 2001 From: amercader Date: Tue, 13 May 2014 18:03:12 +0100 Subject: [PATCH] [#63] Add extra stuff to the get_package_dict extension point Moved the call to get_site_user higher on base.py so it's available to extensions. Also added the parsed XML etree so it does not need to be parsed from the string again. --- ckanext/spatial/harvesters/base.py | 18 +++++++++++------- ckanext/spatial/interfaces.py | 6 +++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/ckanext/spatial/harvesters/base.py b/ckanext/spatial/harvesters/base.py index 017d722..f91f42c 100644 --- a/ckanext/spatial/harvesters/base.py +++ b/ckanext/spatial/harvesters/base.py @@ -111,6 +111,8 @@ class SpatialHarvester(HarvesterBase): _user_name = None + _site_user = None + source_config = {} force_import = False @@ -376,6 +378,7 @@ class SpatialHarvester(HarvesterBase): context = { 'model': model, 'session': model.Session, + 'user': self._get_user_name(), } log = logging.getLogger(__name__ + '.import') @@ -402,7 +405,6 @@ class SpatialHarvester(HarvesterBase): # Delete package context.update({ 'ignore_auth': True, - 'user': self._get_user_name(), }) p.toolkit.get_action('package_delete')(context, {'id': harvest_object.package_id}) log.info('Deleted package {0} with guid {1}'.format(harvest_object.package_id, harvest_object.guid)) @@ -445,7 +447,9 @@ class SpatialHarvester(HarvesterBase): # Parse ISO document try: - iso_values = ISODocument(harvest_object.content).read_values() + + iso_parser = ISODocument(harvest_object.content) + iso_values = iso_parser.read_values() except Exception, e: self._save_object_error('Error parsing ISO document for object {0}: {1}'.format(harvest_object.id, str(e)), harvest_object, 'Import') @@ -498,6 +502,7 @@ class SpatialHarvester(HarvesterBase): package_dict = harvester.get_package_dict(context, { 'package_dict': package_dict, 'iso_values': iso_values, + 'xml_tree': iso_parser.xml_tree, 'harvest_object': harvest_object, }) if not package_dict: @@ -506,12 +511,11 @@ class SpatialHarvester(HarvesterBase): # Create / update the package context.update({ - 'user': self._get_user_name(), - 'extras_as_string': True, - 'api_version': '2', - 'return_id_only': True}) + 'extras_as_string': True, + 'api_version': '2', + 'return_id_only': True}) - if context['user'] == self._site_user['name']: + if self._site_user and context['user'] == self._site_user['name']: context['ignore_auth'] = True diff --git a/ckanext/spatial/interfaces.py b/ckanext/spatial/interfaces.py index a40908d..375a664 100644 --- a/ckanext/spatial/interfaces.py +++ b/ckanext/spatial/interfaces.py @@ -23,7 +23,8 @@ class ISpatialHarvester(Interface): :param context: Contains a reference to the model, eg to - perform DB queries. + perform DB queries, and the user name used for + authorization. :type context: dict :param data_dict: Available data. Contains three keys: @@ -33,6 +34,9 @@ class ISpatialHarvester(Interface): * `iso_values` The parsed ISO XML document values. These contain more fields that are not added by default to the ``package_dict``. + * `xml_tree` + The full XML etree object. If some values not present in + ``iso_values`` are needed, these can be extracted via xpath. * `harvest_object` A ``HarvestObject`` domain object which contains a reference to the original metadata document (``harvest_object.content``)