diff --git a/ckanext/harvest/controllers/view.py b/ckanext/harvest/controllers/view.py index ea01a5f..b3567b2 100644 --- a/ckanext/harvest/controllers/view.py +++ b/ckanext/harvest/controllers/view.py @@ -31,7 +31,8 @@ class ViewController(BaseController): p.toolkit.get_action('harvest_source_delete')(context, {'id':id}) h.flash_success(_('Harvesting source successfully inactivated')) - redirect(h.url_for('harvest')) + + redirect(h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id)) except p.toolkit.ObjectNotFound: abort(404,_('Harvest source not found')) except p.toolkit.NotAuthorized: diff --git a/ckanext/harvest/logic/action/delete.py b/ckanext/harvest/logic/action/delete.py index 84b29b1..dd13e24 100644 --- a/ckanext/harvest/logic/action/delete.py +++ b/ckanext/harvest/logic/action/delete.py @@ -1,32 +1,27 @@ import logging -from ckan.logic import NotFound, check_access +from ckan import plugins as p -from ckanext.harvest.model import (HarvestSource, HarvestJob) log = logging.getLogger(__name__) -def harvest_source_delete(context,data_dict): +def harvest_source_delete(context, data_dict): + ''' + Deletes an existing harvest source + + This method just proxies the request to package_delete, + which will delete the actual harvest type dataset and the + HarvestSource object (via the after_delete extension point). + + :param id: the name or id of the harvest source to delete + :type id: string + + :returns: the newly created harvest source + :rtype: dictionary + + ''' log.info('Deleting harvest source: %r', data_dict) - check_access('harvest_source_delete',context,data_dict) - source_id = data_dict.get('id') - source = HarvestSource.get(source_id) - if not source: - log.warn('Harvest source %s does not exist', source_id) - raise NotFound('Harvest source %s does not exist' % source_id) + p.toolkit.check_access('harvest_source_delete', context, data_dict) - # Don't actually delete the record, just flag it as inactive - source.active = False - source.save() - - # Abort any pending jobs - jobs = HarvestJob.filter(source=source,status=u'New') - if jobs: - log.info('Aborting %i jobs due to deleted harvest source', jobs.count()) - for job in jobs: - job.status = u'Aborted' - job.save() - - log.info('Harvest source %s deleted', source_id) - return True + p.toolkit.get_action('package_delete')(context, data_dict) diff --git a/ckanext/harvest/plugin.py b/ckanext/harvest/plugin.py index 8933989..9297f2a 100644 --- a/ckanext/harvest/plugin.py +++ b/ckanext/harvest/plugin.py @@ -43,6 +43,11 @@ class Harvest(p.SingletonPlugin, DefaultDatasetForm): # Edit the actual HarvestSource object _update_harvest_source_object(data_dict) + def after_delete(self, context, data_dict): + if 'type' in data_dict and data_dict['type'] == DATASET_TYPE_NAME: + # Delete the actual HarvestSource object + _delete_harvest_source_object(data_dict) + def after_show(self, context, data_dict): def add_extra(data_dict, key, value): @@ -158,7 +163,6 @@ class Harvest(p.SingletonPlugin, DefaultDatasetForm): 'state', 'owner_org', 'frequency', 'config', 'organization'] - #TODO: state and delete if not schema: schema = self.form_to_db_schema() schema_keys = schema.keys() @@ -294,9 +298,7 @@ def _create_harvest_source_object(data_dict): if o in data_dict and data_dict[o] is not None: source.__setattr__(o,data_dict[o]) - #TODO: state / deleted - if 'active' in data_dict: - source.active = data_dict['active'] + source.active = data_dict.get('state', None) == 'active' # Don't commit yet, let package_create do it source.add() @@ -338,12 +340,11 @@ def _update_harvest_source_object(data_dict): if 'source_type' in data_dict: source.type = data_dict['source_type'] - if 'active' in data_dict: - source.active = data_dict['active'] - if 'config' in data_dict: source.config = data_dict['config'] + source.active = data_dict.get('state', None) == 'active' + # Don't commit yet, let package_create do it source.add() @@ -357,3 +358,42 @@ def _update_harvest_source_object(data_dict): job.add() return source + +def _delete_harvest_source_object(data_dict): + ''' + Deletes an actual HarvestSource object with the id provided on the + data dict of the harvest_source dataset. Similarly to the datasets, + the source object is not actually deleted, just flagged as inactive. + All validation and authorization checks should be used by now, so + this function is not to be used directly to delete harvest sources. + + :param data_dict: A standard package data_dict + + :returns: The deleted HarvestSource object + :rtype: HarvestSource object + ''' + + source_id = data_dict.get('id') + + log.info('Deleting harvest source: %s', source_id) + + source = HarvestSource.get(source_id) + if not source: + log.warn('Harvest source %s does not exist', source_id) + raise p.toolkit.ObjectNotFound('Harvest source %s does not exist' % source_id) + + # Don't actually delete the record, just flag it as inactive + source.active = False + source.save() + + # Abort any pending jobs + jobs = HarvestJob.filter(source=source, status=u'New') + if jobs: + log.info('Aborting %i jobs due to deleted harvest source', jobs.count()) + for job in jobs: + job.status = u'Aborted' + job.save() + + log.debug('Harvest source %s deleted', source_id) + + return source diff --git a/ckanext/harvest/templates_new/source/new_source_form.html b/ckanext/harvest/templates_new/source/new_source_form.html index 330a9c3..f9e8e5f 100644 --- a/ckanext/harvest/templates_new/source/new_source_form.html +++ b/ckanext/harvest/templates_new/source/new_source_form.html @@ -70,7 +70,26 @@ {% endif %} {% endif %} + {% if data.get('id', None) and h.check_access('harvest_source_delete', {'id': data.id}) and data.get('state', 'none') == 'deleted' %} +
+ {% block delete_button %} + {% if data.get('id', None) and h.check_access('harvest_source_delete', {'id': data.id}) and not data.get('state', 'none') == 'deleted' %} + {% set locale = h.dump_json({'content': _('Are you sure you want to delete this harvest source?')}) %} + {% block delete_button_text %}{{ _('Delete') }}{% endblock %} + {% endif %} + {% endblock %} +