2011-04-11 18:23:27 +02:00
|
|
|
import urllib2
|
|
|
|
|
2016-09-19 13:32:55 +02:00
|
|
|
from ckan.lib.base import BaseController, c, request, \
|
|
|
|
response, render, abort
|
2011-04-11 18:23:27 +02:00
|
|
|
|
|
|
|
from ckan.model import Package
|
|
|
|
|
2016-09-19 13:32:55 +02:00
|
|
|
|
2011-04-11 18:23:27 +02:00
|
|
|
class ViewController(BaseController):
|
|
|
|
|
2016-09-19 13:32:55 +02:00
|
|
|
def wms_preview(self, id):
|
|
|
|
# check if package exists
|
2011-04-11 18:23:27 +02:00
|
|
|
c.pkg = Package.get(id)
|
|
|
|
if c.pkg is None:
|
2011-10-28 14:03:31 +02:00
|
|
|
abort(404, 'Dataset not found')
|
2011-04-11 18:23:27 +02:00
|
|
|
|
|
|
|
for res in c.pkg.resources:
|
2012-08-13 18:07:59 +02:00
|
|
|
if res.format.lower() == 'wms':
|
2016-09-19 13:32:55 +02:00
|
|
|
c.wms_url = res.url \
|
|
|
|
if '?' not in res.url else res.url.split('?')[0]
|
2011-04-11 18:23:27 +02:00
|
|
|
break
|
2011-09-30 13:15:53 +02:00
|
|
|
if not c.wms_url:
|
2011-10-28 14:03:31 +02:00
|
|
|
abort(400, 'This dataset does not have a WMS resource')
|
2011-04-11 18:23:27 +02:00
|
|
|
|
|
|
|
return render('ckanext/spatial/wms_preview.html')
|
|
|
|
|
|
|
|
def proxy(self):
|
2016-09-19 13:32:55 +02:00
|
|
|
if 'url' not in request.params:
|
2011-04-11 18:23:27 +02:00
|
|
|
abort(400)
|
|
|
|
try:
|
|
|
|
server_response = urllib2.urlopen(request.params['url'])
|
|
|
|
headers = server_response.info()
|
|
|
|
if headers.get('Content-Type'):
|
|
|
|
response.content_type = headers.get('Content-Type')
|
|
|
|
return server_response.read()
|
|
|
|
except urllib2.HTTPError as e:
|
|
|
|
response.status_int = e.getcode()
|
|
|
|
return
|