[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 ckan.model import Package
from ckanext.harvest.lib import *
class ViewController(BaseController): class ViewController(BaseController):
@ -38,31 +39,11 @@ class ViewController(BaseController):
except urllib2.HTTPError as e: except urllib2.HTTPError as e:
raise raise
def index(self): def index(self):
# Request all harvesting sources # Request all harvest sources
sources_url = self.api_url + '/harvestsource' c.sources = get_harvest_sources()
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)
#TODO: show source reports
return render('ckanext/harvest/index.html') return render('ckanext/harvest/index.html')
def create(self): def create(self):
@ -104,24 +85,17 @@ class ViewController(BaseController):
redirect(h.url_for(controller='harvest', action='index')) redirect(h.url_for(controller='harvest', action='index'))
def show(self,id): def show(self,id):
sources_url = self.api_url + '/harvestsource/%s' % id c.source = get_harvest_source(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)
#TODO: show source reports
return render('ckanext/harvest/show.html') return render('ckanext/harvest/show.html')
def delete(self,id): def delete(self,id):
form_url = self.form_api_url + '/harvestsource/delete/%s' % id
try: try:
r = self._do_request(form_url) delete_harvest_source(id)
h.flash_success('Harvesting source deleted successfully') h.flash_success('Harvesting source deleted successfully')
except urllib2.HTTPError as e: except Exception as e:
msg = 'An error occurred: [%s %s]' % (str(e.getcode()),e.msg) msg = 'An error occurred: [%s]' % e.message
h.flash_error(msg) h.flash_error(msg)
redirect(h.url_for(controller='harvest', action='index', id=None)) 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)) redirect(h.url_for(controller='harvest', action='index', id=None))
def create_harvesting_job(self,id): def create_harvesting_job(self,id):
form_url = self.api_url + '/harvestingjob'
data = {
'source_id': id,
'user_ref': ''
}
data = json.dumps(data)
try: try:
r = self._do_request(form_url,data) create_harvest_job(id)
h.flash_success('Refresh requested, harvesting will take place within 15 minutes.') h.flash_success('Refresh requested, harvesting will take place within 15 minutes.')
except urllib2.HTTPError as e: except Exception as e:
msg = 'An error occurred: [%s %s]' % (str(e.getcode()),e.msg) msg = 'An error occurred: [%s]' % e.message
if e.getcode() == 400:
msg = msg + ' ' + e.read()
h.flash_error(msg) 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): def map_view(self,id):
#check if package exists #check if package exists

View File

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

View File

@ -20,10 +20,12 @@
<th></th> <th></th>
<th></th> <th></th>
<th>URL</th> <th>URL</th>
<th>Status</th> <th>Type</th>
<th>Active</th>
<!-- <th>Status</th>
<th>Statistics</th> <th>Statistics</th>
<th>Next Harvest</th> <th>Next Harvest</th>-->
<th>Created Date</th> <th>Created</th>
</tr> </tr>
<tr py:for="source in c.sources"> <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('edit', 'harvest/' + source.id + '/edit')}</td>
<td>${h.link_to('refresh', 'harvest/' + source.id + '/refresh')}</td> <td>${h.link_to('refresh', 'harvest/' + source.id + '/refresh')}</td>
<td>${source.url}</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.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> <td>${source.created}</td>
</tr> </tr>
</table> </table>

View File

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