From 43f1d082554635d611f226b273a0f60b5743b253 Mon Sep 17 00:00:00 2001 From: amercader Date: Wed, 30 Apr 2014 17:45:07 +0100 Subject: [PATCH] [#97] Persitent endpoint for datasets harvest objects Contrary to `/harvest/object/xxx`, this endpoint is passed the dataset id, thus it not depends on a particular object but the most recent one. --- ckanext/harvest/controllers/view.py | 11 ++++++---- ckanext/harvest/logic/action/get.py | 31 +++++++++++++++++++++++------ ckanext/harvest/plugin.py | 1 + 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/ckanext/harvest/controllers/view.py b/ckanext/harvest/controllers/view.py index b3d0f94..68b80cf 100644 --- a/ckanext/harvest/controllers/view.py +++ b/ckanext/harvest/controllers/view.py @@ -88,11 +88,14 @@ class ViewController(BaseController): redirect(h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id)) - def show_object(self,id): + def show_object(self, id, ref_type='object'): try: context = {'model':model, 'user':c.user} - obj = p.toolkit.get_action('harvest_object_show')(context, {'id':id}) + if ref_type == 'object': + obj = p.toolkit.get_action('harvest_object_show')(context, {'id': id}) + elif ref_type == 'dataset': + obj = p.toolkit.get_action('harvest_object_show')(context, {'dataset_id': id}) # Check content type. It will probably be either XML or JSON try: @@ -119,8 +122,8 @@ class ViewController(BaseController): response.headers['Content-Length'] = len(content) return content.encode('utf-8') - except p.toolkit.ObjectNotFound: - abort(404,_('Harvest object not found')) + except p.toolkit.ObjectNotFound, e: + abort(404,_(str(e))) except p.toolkit.NotAuthorized: abort(401,self.not_auth_message) except Exception, e: diff --git a/ckanext/harvest/logic/action/get.py b/ckanext/harvest/logic/action/get.py index d293e29..8282ff5 100644 --- a/ckanext/harvest/logic/action/get.py +++ b/ckanext/harvest/logic/action/get.py @@ -249,15 +249,34 @@ def harvest_job_list(context,data_dict): @side_effect_free def harvest_object_show(context,data_dict): - check_access('harvest_object_show',context,data_dict) + p.toolkit.check_access('harvest_object_show', context, data_dict) id = data_dict.get('id') - attr = data_dict.get('attr',None) - obj = HarvestObject.get(id,attr=attr) - if not obj: - raise NotFound + dataset_id = data_dict.get('dataset_id') - return harvest_object_dictize(obj,context) + if id: + attr = data_dict.get('attr',None) + obj = HarvestObject.get(id,attr=attr) + elif dataset_id: + model = context['model'] + + pkg = model.Package.get(dataset_id) + if not pkg: + raise p.toolkit.ObjectNotFound('Dataset not found') + + obj = model.Session.query(HarvestObject) \ + .filter(HarvestObject.package_id == pkg.id) \ + .filter(HarvestObject.current == True) \ + .first() + else: + raise p.toolkit.ValidationError( + 'Please provide either an "id" or a "dataset_id" parameter') + + if not obj: + raise p.toolkit.ObjectNotFound('Harvest object not found') + + + return harvest_object_dictize(obj, context) @side_effect_free def harvest_object_list(context,data_dict): diff --git a/ckanext/harvest/plugin.py b/ckanext/harvest/plugin.py index 5b04d9c..d2e7432 100644 --- a/ckanext/harvest/plugin.py +++ b/ckanext/harvest/plugin.py @@ -210,6 +210,7 @@ class Harvest(p.SingletonPlugin, DefaultDatasetForm): map.connect('harvest_job_show', '/' + DATASET_TYPE_NAME + '/{source}/job/{id}', controller=controller, action='show_job') map.connect('harvest_object_show', '/' + DATASET_TYPE_NAME + '/object/:id', controller=controller, action='show_object') + map.connect('harvest_object_for_dataset_show', '/dataset/harvest_object/:id', controller=controller, action='show_object', ref_type='dataset') org_controller = 'ckanext.harvest.controllers.organization:OrganizationController' map.connect('{0}_org_list'.format(DATASET_TYPE_NAME), '/organization/' + DATASET_TYPE_NAME + '/' + '{id}', controller=org_controller, action='source_list')