Merge branch '18-delete-sources-for-2.0' into release-v2.0

This commit is contained in:
amercader 2013-03-12 15:36:04 +00:00
commit 2ed7966f72
4 changed files with 86 additions and 31 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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

View File

@ -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' %}
<div class="control-group">
<label for="field-state" class="control-label">{{ _('State') }}</label>
<div class="controls">
<select id="field-state" name="state">
<option value="active" {% if data.get('state', 'none') == 'active' %} selected="selected" {% endif %}>{{ _('Active') }}</option>
<option value="deleted" {% if data.get('state', 'none') == 'deleted' %} selected="selected" {% endif %}>{{ _('Deleted') }}</option>
</select>
</div>
</div>
{% endif %}
<p class="form-actions">
{% 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?')}) %}
<a class="btn btn-danger pull-left" href="{% url_for 'harvest_delete', id=data.name %}" data-module="confirm-action" data-module-i18n="{{ locale }}">{% block delete_button_text %}{{ _('Delete') }}{% endblock %}</a>
{% endif %}
{% endblock %}
<input id="save" name="save" value="Save" type="submit" class="btn btn-primary">
</p>