Merge branch 'release-v2.0' of https://github.com/okfn/ckanext-harvest into release-v2.0

This commit is contained in:
kindly 2013-03-12 23:31:58 +00:00
commit cb5e06119e
4 changed files with 90 additions and 35 deletions

View File

@ -31,7 +31,8 @@ class ViewController(BaseController):
p.toolkit.get_action('harvest_source_delete')(context, {'id':id}) p.toolkit.get_action('harvest_source_delete')(context, {'id':id})
h.flash_success(_('Harvesting source successfully inactivated')) 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: except p.toolkit.ObjectNotFound:
abort(404,_('Harvest source not found')) abort(404,_('Harvest source not found'))
except p.toolkit.NotAuthorized: except p.toolkit.NotAuthorized:

View File

@ -1,32 +1,27 @@
import logging 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__) 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) log.info('Deleting harvest source: %r', data_dict)
check_access('harvest_source_delete',context,data_dict)
source_id = data_dict.get('id') p.toolkit.check_access('harvest_source_delete', context, data_dict)
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)
# Don't actually delete the record, just flag it as inactive p.toolkit.get_action('package_delete')(context, data_dict)
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

View File

@ -39,12 +39,17 @@ class Harvest(p.SingletonPlugin, DefaultDatasetForm):
def after_create(self, context, data_dict): def after_create(self, context, data_dict):
if 'type' in data_dict and data_dict['type'] == DATASET_TYPE_NAME and not self.startup: if 'type' in data_dict and data_dict['type'] == DATASET_TYPE_NAME and not self.startup:
# Create an actual HarvestSource object # Create an actual HarvestSource object
_create_harvest_source_object(data_dict) _create_harvest_source_object(context, data_dict)
def after_update(self, context, data_dict): def after_update(self, context, data_dict):
if 'type' in data_dict and data_dict['type'] == DATASET_TYPE_NAME: if 'type' in data_dict and data_dict['type'] == DATASET_TYPE_NAME:
# Edit the actual HarvestSource object # Edit the actual HarvestSource object
_update_harvest_source_object(data_dict) _update_harvest_source_object(context, 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(context, data_dict)
def after_show(self, context, data_dict): def after_show(self, context, data_dict):
@ -161,7 +166,6 @@ class Harvest(p.SingletonPlugin, DefaultDatasetForm):
'state', 'owner_org', 'frequency', 'config', 'state', 'owner_org', 'frequency', 'config',
'organization'] 'organization']
#TODO: state and delete
if not schema: if not schema:
schema = self.form_to_db_schema() schema = self.form_to_db_schema()
schema_keys = schema.keys() schema_keys = schema.keys()
@ -284,7 +288,7 @@ def _get_logic_functions(module_root, logic_functions = {}):
return logic_functions return logic_functions
def _create_harvest_source_object(data_dict): def _create_harvest_source_object(context, data_dict):
''' '''
Creates an actual HarvestSource object with the data dict Creates an actual HarvestSource object with the data dict
of the harvest_source dataset. All validation and authorization of the harvest_source dataset. All validation and authorization
@ -314,9 +318,7 @@ def _create_harvest_source_object(data_dict):
if o in data_dict and data_dict[o] is not None: if o in data_dict and data_dict[o] is not None:
source.__setattr__(o,data_dict[o]) source.__setattr__(o,data_dict[o])
#TODO: state / deleted source.active = data_dict.get('state', None) == 'active'
if 'active' in data_dict:
source.active = data_dict['active']
# Don't commit yet, let package_create do it # Don't commit yet, let package_create do it
source.add() source.add()
@ -324,7 +326,7 @@ def _create_harvest_source_object(data_dict):
return source return source
def _update_harvest_source_object(data_dict): def _update_harvest_source_object(context, data_dict):
''' '''
Updates an actual HarvestSource object with the data dict Updates an actual HarvestSource object with the data dict
of the harvest_source dataset. All validation and authorization of the harvest_source dataset. All validation and authorization
@ -358,12 +360,11 @@ def _update_harvest_source_object(data_dict):
if 'source_type' in data_dict: if 'source_type' in data_dict:
source.type = data_dict['source_type'] source.type = data_dict['source_type']
if 'active' in data_dict:
source.active = data_dict['active']
if 'config' in data_dict: if 'config' in data_dict:
source.config = data_dict['config'] source.config = data_dict['config']
source.active = data_dict.get('state', None) == 'active'
# Don't commit yet, let package_create do it # Don't commit yet, let package_create do it
source.add() source.add()
@ -377,3 +378,42 @@ def _update_harvest_source_object(data_dict):
job.add() job.add()
return source return source
def _delete_harvest_source_object(context, 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 %}
{% 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"> <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"> <input id="save" name="save" value="Save" type="submit" class="btn btn-primary">
</p> </p>