Add controller to create a harvesting source
Before sending a create request to the forms API, an authz header is added, with the API key defined in the config file (ckan.harvesting.api_key). Also the data is formatted as the JSON object expected by the forms API.
This commit is contained in:
parent
1768dee13e
commit
ad239b93e8
|
@ -1,63 +1,86 @@
|
||||||
import simplejson as json
|
from ckan.lib.helpers import json
|
||||||
import urllib2
|
import urllib2
|
||||||
import ckan.lib.helpers as h
|
import ckan.lib.helpers as h
|
||||||
from ckan.lib.base import BaseController, c, g, request, \
|
from ckan.lib.base import BaseController, c, g, request, \
|
||||||
response, session, render, config, abort
|
response, session, render, config, abort, redirect
|
||||||
#from ..dictization import *
|
#from ..dictization import *
|
||||||
|
|
||||||
class ViewController(BaseController):
|
class ViewController(BaseController):
|
||||||
|
|
||||||
api_url = config.get('ckan.api_url', '/').rstrip('/')+'/api/2/rest'
|
api_url = config.get('ckan.api_url', 'http://localhost:5000').rstrip('/')+'/api/2/rest'
|
||||||
|
|
||||||
def _request_document(self,url,data = False):
|
|
||||||
try:
|
|
||||||
doc = urllib2.urlopen(url).read()
|
|
||||||
return doc
|
|
||||||
except urllib2.URLError:
|
|
||||||
#TODO: log?
|
|
||||||
abort(500)
|
|
||||||
|
|
||||||
def index(self):
|
def index(self):
|
||||||
"""
|
try:
|
||||||
TODO: error handling
|
# Request all harvesting sources
|
||||||
"""
|
sources_url = self.api_url + '/harvestsource'
|
||||||
# Request all harvesting sources
|
|
||||||
sources_url = self.api_url + '/harvestsource'
|
doc = urllib2.urlopen(sources_url).read()
|
||||||
|
sources_ids = json.loads(doc)
|
||||||
doc = self._request_document(sources_url)
|
|
||||||
sources_ids = json.loads(doc)
|
|
||||||
|
|
||||||
source_url = sources_url + '/%s'
|
source_url = sources_url + '/%s'
|
||||||
sources = []
|
sources = []
|
||||||
for source_id in sources_ids:
|
|
||||||
doc = self._request_document(source_url % source_id)
|
# For each source, request its details
|
||||||
sources.append(json.loads(doc))
|
for source_id in sources_ids:
|
||||||
|
doc = urllib2.urlopen(source_url % source_id).read()
|
||||||
c.sources = sources
|
sources.append(json.loads(doc))
|
||||||
return render('ckanext/harvest/index.html')
|
|
||||||
|
|
||||||
|
c.sources = sources
|
||||||
|
return render('ckanext/harvest/index.html')
|
||||||
|
except urllib2.HTTPError as e:
|
||||||
|
raise Exception('The forms API returned an error:' + str(e.getcode()) + ' ' + e.msg )
|
||||||
|
|
||||||
def create(self):
|
def create(self):
|
||||||
|
|
||||||
# This is the DGU form API, so we don't use self.api_url
|
# This is the DGU form API, so we don't use self.api_url
|
||||||
form_url = config.get('ckan.api_url', '/').rstrip('/') + \
|
form_url = config.get('ckan.api_url', '/').rstrip('/') + \
|
||||||
'/api/2/form/harvestsource/create'
|
'/api/2/form/harvestsource/create'
|
||||||
|
|
||||||
|
# Create a Request object to define the Authz header
|
||||||
|
http_request = urllib2.Request(
|
||||||
|
url = form_url,
|
||||||
|
headers = {'Authorization' : config.get('ckan.harvesting.api_key')}
|
||||||
|
)
|
||||||
|
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
c.form = self._request_document(form_url)
|
# Request the fields
|
||||||
|
c.form = urllib2.urlopen(http_request).read()
|
||||||
|
|
||||||
return render('ckanext/harvest/create.html')
|
return render('ckanext/harvest/create.html')
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
"""
|
# Build an object like the one expected by the DGU form API
|
||||||
TODO: Authz
|
data = {
|
||||||
"""
|
'form_data':
|
||||||
|
{'HarvestSource--url': request.POST['HarvestSource--url'],
|
||||||
|
'HarvestSource--description': request.POST['HarvestSource--description']},
|
||||||
|
'user_ref':'',
|
||||||
|
'publisher_ref':''
|
||||||
|
}
|
||||||
|
data = json.dumps(data)
|
||||||
|
http_request.add_data(data)
|
||||||
|
|
||||||
#raw_post_data = request.environ['wsgi.input'].read(int(request.environ['CONTENT_LENGTH']))
|
try:
|
||||||
raw_post_data = request.environ['wsgi.input'].read()
|
r = urllib2.urlopen(http_request)
|
||||||
r = urllib2.urlopen(form_url,raw_post_data)
|
|
||||||
|
h.flash_success('Harvesting source added successfully')
|
||||||
return str(r.getcode())
|
redirect(h.url_for(controller='harvest', action='index'))
|
||||||
|
except urllib2.HTTPError as e:
|
||||||
|
h.flash_error('An error occurred: ' + str(e.getcode()) + ' ' + e.msg)
|
||||||
|
redirect(h.url_for(controller='harvest', action='create'))
|
||||||
|
"""
|
||||||
|
if e.getcode() == 403:
|
||||||
|
abort(403)
|
||||||
|
else:
|
||||||
|
raise Exception('The forms API returned an error:' + str(e.getcode()) + ' ' + e.msg )
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def show(self,id):
|
def show(self,id):
|
||||||
sources_url = self.api_url + '/harvestsource/%s' % id
|
sources_url = self.api_url + '/harvestsource/%s' % id
|
||||||
doc = self._request_document(sources_url)
|
try:
|
||||||
|
doc = urllib2.urlopen(sources_url).read()
|
||||||
|
except urllib2.HTTPError as e:
|
||||||
|
raise Exception('The forms API returned an error:' + str(e.getcode()) + ' ' + e.msg )
|
||||||
c.source = json.loads(doc)
|
c.source = json.loads(doc)
|
||||||
|
|
||||||
return render('ckanext/harvest/show.html')
|
return render('ckanext/harvest/show.html')
|
||||||
|
|
|
@ -11,29 +11,13 @@
|
||||||
|
|
||||||
<div py:match="content">
|
<div py:match="content">
|
||||||
<div class="harvest-content">
|
<div class="harvest-content">
|
||||||
<h1>Add harvesting source</h1>
|
<h1>Add harvesting source</h1>
|
||||||
${Markup(c.form)}
|
|
||||||
<div class="submit">
|
<form action="create" method="POST">
|
||||||
<input id="save" name="save" value="Save" type="button" />
|
${Markup(c.form)}
|
||||||
|
<input id="save" name="save" value="Save" type="submit" />
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<script type="text/javascript">
|
|
||||||
//<![CDATA[
|
|
||||||
$(document).ready(function() {
|
|
||||||
$('#save').click(function(){
|
|
||||||
$.post(
|
|
||||||
'http://localhost:5000/harvest/create',
|
|
||||||
{'HarvestSource--url':$('#HarvestSource--url').val(),
|
|
||||||
'HarvestSource--description':$('#HarvestSource--description').val()},
|
|
||||||
function(data,status,xhr){
|
|
||||||
alert(status);
|
|
||||||
})
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//]]>
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<xi:include href="../../layout.html" />
|
<xi:include href="../../layout.html" />
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue