[refactoring] Use the common functions in the web interface.

Not yet implemented in create and edit harvest source as they use the DGU forms API.
Also TODO, think of what report info is needed in the listing and details page.
This commit is contained in:
Adrià Mercader 2011-04-05 13:39:23 +01:00
parent 3d32a18802
commit 4023bb7222
5 changed files with 86 additions and 102 deletions

View File

@ -8,6 +8,7 @@ from ckan.lib.base import BaseController, c, g, request, \
from ckan.model import Package
from ckanext.harvest.lib import *
class ViewController(BaseController):
@ -38,31 +39,11 @@ class ViewController(BaseController):
except urllib2.HTTPError as e:
raise
def index(self):
# Request all harvesting sources
sources_url = self.api_url + '/harvestsource'
try:
doc = self._do_request(sources_url).read()
sources_ids = json.loads(doc)
source_url = sources_url + '/%s'
sources = []
# For each source, request its details
for source_id in sources_ids:
doc = self._do_request(source_url % source_id).read()
sources.append(json.loads(doc))
c.sources = sources
except urllib2.HTTPError as e:
msg = 'An error occurred: [%s %s]' % (str(e.getcode()),e.msg)
h.flash_error(msg)
except urllib2.URLError as e:
msg = 'Could not find server %r: %r' % (sources_url, e)
h.flash_error(msg)
# Request all harvest sources
c.sources = get_harvest_sources()
#TODO: show source reports
return render('ckanext/harvest/index.html')
def create(self):
@ -104,24 +85,17 @@ class ViewController(BaseController):
redirect(h.url_for(controller='harvest', action='index'))
def show(self,id):
sources_url = self.api_url + '/harvestsource/%s' % id
try:
doc = self._do_request(sources_url).read()
c.source = json.loads(doc)
except urllib2.HTTPError as e:
msg = 'An error occurred: [%s %s]' % (str(e.getcode()),e.msg)
h.flash_error(msg)
c.source = get_harvest_source(id)
#TODO: show source reports
return render('ckanext/harvest/show.html')
def delete(self,id):
form_url = self.form_api_url + '/harvestsource/delete/%s' % id
try:
r = self._do_request(form_url)
delete_harvest_source(id)
h.flash_success('Harvesting source deleted successfully')
except urllib2.HTTPError as e:
msg = 'An error occurred: [%s %s]' % (str(e.getcode()),e.msg)
except Exception as e:
msg = 'An error occurred: [%s]' % e.message
h.flash_error(msg)
redirect(h.url_for(controller='harvest', action='index', id=None))
@ -156,25 +130,14 @@ class ViewController(BaseController):
redirect(h.url_for(controller='harvest', action='index', id=None))
def create_harvesting_job(self,id):
form_url = self.api_url + '/harvestingjob'
data = {
'source_id': id,
'user_ref': ''
}
data = json.dumps(data)
try:
r = self._do_request(form_url,data)
create_harvest_job(id)
h.flash_success('Refresh requested, harvesting will take place within 15 minutes.')
except urllib2.HTTPError as e:
msg = 'An error occurred: [%s %s]' % (str(e.getcode()),e.msg)
if e.getcode() == 400:
msg = msg + ' ' + e.read()
except Exception as e:
msg = 'An error occurred: [%s]' % e.message
h.flash_error(msg)
finally:
redirect(h.url_for(controller='harvest', action='index', id=None))
redirect(h.url_for(controller='harvest', action='index', id=None))
def map_view(self,id):
#check if package exists

View File

@ -18,6 +18,8 @@ def _source_as_dict(source):
for obj in source.objects:
out['objects'].append(obj.as_dict())
#TODO: Get some report data
return out
def _job_as_dict(job):
@ -95,7 +97,8 @@ def get_harvest_jobs(**kwds):
def create_harvest_job(source_id):
# Check if source exists
try:
source = get_harvest_source(source_id)
#We'll need the actual HarvestSource
source = HarvestSource.get(source_id)
except:
raise Exception('Source %s does not exist' % source_id)

View File

@ -20,10 +20,12 @@
<th></th>
<th></th>
<th>URL</th>
<th>Status</th>
<th>Type</th>
<th>Active</th>
<!-- <th>Status</th>
<th>Statistics</th>
<th>Next Harvest</th>
<th>Created Date</th>
<th>Next Harvest</th>-->
<th>Created</th>
</tr>
<tr py:for="source in c.sources">
@ -31,9 +33,11 @@
<td>${h.link_to('edit', 'harvest/' + source.id + '/edit')}</td>
<td>${h.link_to('refresh', 'harvest/' + source.id + '/refresh')}</td>
<td>${source.url}</td>
<td>${source.status.last_harvest_status}</td>
<td>${source.type}</td>
<td>${source.active}</td>
<!-- <td>${source.status.last_harvest_status}</td>
<td>${source.status.overall_statistics.added} pkgs ${source.status.overall_statistics.errors} errors</td>
<td>${source.status.next_harvest}</td>
<td>${source.status.next_harvest}</td>-->
<td>${source.created}</td>
</tr>
</table>

View File

@ -22,22 +22,35 @@
<th>URL</th>
<td>${c.source.url}</td>
</tr>
<tr>
<th>Type</th>
<td>${c.source.type}</td>
</tr>
<tr>
<th>Active</th>
<td>${c.source.active}</td>
</tr>
<tr>
<th>Description</th>
<td>${c.source.description}</td>
</tr>
<tr>
<th>User</th>
<td>${c.source.user_ref}</td>
<td>${c.source.user_id}</td>
</tr>
<tr>
<th>Publisher</th>
<td>${c.source.publisher_ref}</td>
<td>${c.source.publisher_id}</td>
</tr>
<tr>
<th>Created</th>
<td>${c.source.created}</td>
</tr>
<tr>
<th>Total jobs</th>
<td>${len(c.source.jobs)}</td>
</tr>
<!--
<tr>
<th>Status</th>
<td>
@ -74,6 +87,7 @@
</div>
</td>
</tr>
-->
</table>
</py:if>
</div>