2011-03-25 18:01:26 +01:00
|
|
|
from pylons.i18n import _
|
|
|
|
|
|
|
|
import ckan.lib.helpers as h, json
|
2011-03-09 19:56:55 +01:00
|
|
|
from ckan.lib.base import BaseController, c, g, request, \
|
2011-03-10 15:02:21 +01:00
|
|
|
response, session, render, config, abort, redirect
|
2011-03-22 18:33:58 +01:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
from ckan.lib.navl.dictization_functions import DataError
|
|
|
|
from ckan.logic import NotFound, ValidationError
|
|
|
|
from ckanext.harvest.logic.schema import harvest_source_form_schema
|
|
|
|
from ckanext.harvest.lib import create_harvest_source, edit_harvest_source, \
|
|
|
|
get_harvest_source, get_harvest_sources, \
|
|
|
|
create_harvest_job, get_registered_harvesters_types
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2011-03-09 19:56:55 +01:00
|
|
|
|
|
|
|
class ViewController(BaseController):
|
|
|
|
|
2011-03-25 18:01:26 +01:00
|
|
|
def __before__(self, action, **env):
|
|
|
|
super(ViewController, self).__before__(action, **env)
|
|
|
|
# All calls to this controller must be with a sysadmin key
|
|
|
|
if not self.authorizer.is_sysadmin(c.user):
|
|
|
|
response_msg = _('Not authorized to see this page')
|
|
|
|
status = 401
|
|
|
|
abort(status, response_msg)
|
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
def index(self):
|
|
|
|
# Request all harvest sources
|
|
|
|
c.sources = get_harvest_sources()
|
2011-03-10 10:45:16 +01:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
return render('index.html')
|
2011-03-10 16:32:51 +01:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
def new(self,data = None,errors = None, error_summary = None):
|
2011-04-05 14:39:23 +02:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
if ('save' in request.params) and not data:
|
|
|
|
return self._save_new()
|
|
|
|
|
|
|
|
data = data or {}
|
|
|
|
errors = errors or {}
|
|
|
|
error_summary = error_summary or {}
|
|
|
|
#TODO: Use new description interface to build the types select and descriptions
|
|
|
|
vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'types': get_registered_harvesters_types()}
|
|
|
|
|
|
|
|
c.form = render('source/new_source_form.html', extra_vars=vars)
|
|
|
|
return render('source/new.html')
|
|
|
|
|
|
|
|
def _save_new(self):
|
2011-03-10 16:32:51 +01:00
|
|
|
try:
|
2011-05-13 15:17:58 +02:00
|
|
|
data_dict = dict(request.params)
|
|
|
|
self._check_data_dict(data_dict)
|
2011-03-10 16:32:51 +01:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
source = create_harvest_source(data_dict)
|
2011-03-10 16:32:51 +01:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
# Create a harvest job for the new source
|
|
|
|
create_harvest_job(source['id'])
|
2011-04-05 14:39:23 +02:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
h.flash_success(_('New harvest source added successfully.'
|
|
|
|
'A new harvest job for the source has also been created.'))
|
|
|
|
redirect(h.url_for('harvest'))
|
|
|
|
except DataError,e:
|
|
|
|
abort(400, 'Integrity Error')
|
|
|
|
except ValidationError,e:
|
|
|
|
errors = e.error_dict
|
|
|
|
error_summary = e.error_summary if 'error_summary' in e else None
|
|
|
|
return self.new(data_dict, errors, error_summary)
|
2011-03-10 15:02:21 +01:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
def edit(self, id, data = None,errors = None, error_summary = None):
|
|
|
|
|
|
|
|
if ('save' in request.params) and not data:
|
|
|
|
return self._save_edit(id)
|
|
|
|
|
|
|
|
if not data:
|
2011-03-11 13:35:27 +01:00
|
|
|
try:
|
2011-05-13 15:17:58 +02:00
|
|
|
old_data = get_harvest_source(id)
|
|
|
|
except NotFound:
|
|
|
|
abort(404, _('Harvest Source not found'))
|
|
|
|
|
|
|
|
data = data or old_data
|
|
|
|
errors = errors or {}
|
|
|
|
error_summary = error_summary or {}
|
|
|
|
#TODO: Use new description interface to build the types select and descriptions
|
|
|
|
vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'types': get_registered_harvesters_types()}
|
|
|
|
|
|
|
|
c.form = render('source/new_source_form.html', extra_vars=vars)
|
|
|
|
return render('source/edit.html')
|
|
|
|
|
|
|
|
def _save_edit(self,id):
|
|
|
|
try:
|
|
|
|
data_dict = dict(request.params)
|
|
|
|
self._check_data_dict(data_dict)
|
|
|
|
|
|
|
|
source = edit_harvest_source(id,data_dict)
|
|
|
|
|
|
|
|
h.flash_success(_('Harvest source edited successfully.'))
|
|
|
|
redirect(h.url_for('harvest'))
|
|
|
|
except DataError,e:
|
|
|
|
abort(400, _('Integrity Error'))
|
|
|
|
except NotFound, e:
|
|
|
|
abort(404, _('Harvest Source not found'))
|
|
|
|
except ValidationError,e:
|
|
|
|
errors = e.error_dict
|
|
|
|
error_summary = e.error_summary if 'error_summary' in e else None
|
|
|
|
return self.edit(id,data_dict, errors, error_summary)
|
|
|
|
|
|
|
|
def _check_data_dict(self, data_dict):
|
|
|
|
'''Check if the return data is correct'''
|
|
|
|
surplus_keys_schema = ['id','publisher_id','user_id','active','save']
|
|
|
|
|
|
|
|
schema_keys = harvest_source_form_schema().keys()
|
|
|
|
keys_in_schema = set(schema_keys) - set(surplus_keys_schema)
|
|
|
|
|
|
|
|
if keys_in_schema - set(data_dict.keys()):
|
|
|
|
log.info(_('Incorrect form fields posted'))
|
|
|
|
raise DataError(data_dict)
|
|
|
|
|
|
|
|
def read(self,id):
|
2011-04-11 17:30:56 +02:00
|
|
|
try:
|
|
|
|
c.source = get_harvest_source(id)
|
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
return render('source/read.html')
|
|
|
|
except NotFound:
|
|
|
|
abort(404,_('Harvest source not found'))
|
2011-03-23 18:02:02 +01:00
|
|
|
|
2011-03-11 13:41:13 +01:00
|
|
|
|
|
|
|
def delete(self,id):
|
2011-03-23 18:02:02 +01:00
|
|
|
try:
|
2011-04-05 14:39:23 +02:00
|
|
|
delete_harvest_source(id)
|
2011-03-23 18:02:02 +01:00
|
|
|
|
2011-05-13 15:17:58 +02:00
|
|
|
h.flash_success(_('Harvesting source deleted successfully'))
|
|
|
|
redirect(h.url_for('harvest'))
|
|
|
|
except NotFound:
|
|
|
|
abort(404,_('Harvest source not found'))
|
2011-04-05 14:39:23 +02:00
|
|
|
|
|
|
|
|
2011-03-10 18:24:23 +01:00
|
|
|
def create_harvesting_job(self,id):
|
2011-03-11 13:35:27 +01:00
|
|
|
try:
|
2011-04-05 14:39:23 +02:00
|
|
|
create_harvest_job(id)
|
2011-05-13 15:17:58 +02:00
|
|
|
h.flash_success(_('Refresh requested, harvesting will take place within 15 minutes.'))
|
|
|
|
redirect(h.url_for('harvest'))
|
|
|
|
except NotFound:
|
|
|
|
abort(404,_('Harvest source not found'))
|
2011-04-05 14:39:23 +02:00
|
|
|
except Exception as e:
|
|
|
|
msg = 'An error occurred: [%s]' % e.message
|
|
|
|
h.flash_error(msg)
|
2011-05-13 15:17:58 +02:00
|
|
|
redirect(h.url_for('harvest'))
|
2011-03-22 18:33:58 +01:00
|
|
|
|