2015-04-02 12:28:06 +02:00
|
|
|
# TODO: Move these to ckanext-geoviews
|
|
|
|
|
2014-03-07 17:26:50 +01:00
|
|
|
import mimetypes
|
2011-10-28 14:03:31 +02:00
|
|
|
from logging import getLogger
|
|
|
|
|
2013-01-14 14:59:15 +01:00
|
|
|
from ckan import plugins as p
|
2011-10-28 14:03:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
log = getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-04-01 12:02:31 +02:00
|
|
|
class DataViewBase(p.SingletonPlugin):
|
|
|
|
'''This base class is for view extensions. '''
|
2015-04-02 12:28:06 +02:00
|
|
|
if p.toolkit.check_ckan_version(min_version='2.3'):
|
2015-04-01 12:02:31 +02:00
|
|
|
p.implements(p.IResourceView, inherit=True)
|
|
|
|
else:
|
|
|
|
p.implements(p.IResourcePreview, inherit=True)
|
2013-01-14 14:59:15 +01:00
|
|
|
p.implements(p.IConfigurer, inherit=True)
|
2015-04-01 12:02:31 +02:00
|
|
|
p.implements(p.IConfigurable, inherit=True)
|
2011-10-28 14:03:31 +02:00
|
|
|
|
2015-04-01 12:02:31 +02:00
|
|
|
proxy_is_enabled = False
|
|
|
|
same_domain = False
|
2013-07-19 12:35:35 +02:00
|
|
|
|
2013-01-14 14:59:15 +01:00
|
|
|
def update_config(self, config):
|
2013-06-13 22:51:01 +02:00
|
|
|
p.toolkit.add_public_directory(config, 'public')
|
|
|
|
p.toolkit.add_template_directory(config, 'templates')
|
|
|
|
p.toolkit.add_resource('public', 'ckanext-spatial')
|
|
|
|
|
2015-04-01 12:02:31 +02:00
|
|
|
config['ckan.resource_proxy_enabled'] = p.plugin_loaded('resource_proxy')
|
|
|
|
|
|
|
|
def configure(self, config):
|
|
|
|
enabled = config.get('ckan.resource_proxy_enabled', False)
|
|
|
|
self.proxy_is_enabled = enabled
|
2011-10-28 14:03:31 +02:00
|
|
|
|
2013-01-14 14:59:15 +01:00
|
|
|
def setup_template_variables(self, context, data_dict):
|
|
|
|
import ckanext.resourceproxy.plugin as proxy
|
2015-04-02 12:28:06 +02:00
|
|
|
self.same_domain = data_dict['resource'].get('on_same_domain')
|
2015-04-01 12:02:31 +02:00
|
|
|
if self.proxy_is_enabled and not self.same_domain:
|
|
|
|
data_dict['resource']['original_url'] = data_dict['resource']['url']
|
|
|
|
data_dict['resource']['url'] = proxy.get_proxified_resource_url(data_dict)
|
2011-10-28 14:03:31 +02:00
|
|
|
|
2013-07-19 23:04:49 +02:00
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
|
2015-04-01 12:02:31 +02:00
|
|
|
class WMSView(DataViewBase):
|
|
|
|
WMS = ['wms']
|
2013-07-19 12:35:35 +02:00
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
# IResourceView (CKAN >=2.3)
|
|
|
|
def info(self):
|
|
|
|
return {'name': 'wms_view',
|
|
|
|
'title': 'WMS',
|
|
|
|
'icon': 'map-marker',
|
|
|
|
'iframed': True,
|
|
|
|
'default_title': p.toolkit._('WMS'),
|
|
|
|
}
|
|
|
|
|
|
|
|
def can_view(self, data_dict):
|
|
|
|
resource = data_dict['resource']
|
|
|
|
format_lower = resource['format'].lower()
|
|
|
|
|
|
|
|
if format_lower in self.WMS:
|
|
|
|
return self.same_domain or self.proxy_is_enabled
|
|
|
|
return False
|
|
|
|
|
|
|
|
def view_template(self, context, data_dict):
|
|
|
|
return 'dataviewer/wms.html'
|
|
|
|
|
|
|
|
# IResourcePreview (CKAN < 2.3)
|
|
|
|
|
|
|
|
def can_preview(self, data_dict):
|
|
|
|
format_lower = data_dict['resource']['format'].lower()
|
|
|
|
|
|
|
|
correct_format = format_lower in self.WMS
|
|
|
|
can_preview_from_domain = self.proxy_is_enabled or data_dict['resource']['on_same_domain']
|
|
|
|
quality = 2
|
|
|
|
|
|
|
|
if p.toolkit.check_ckan_version('2.1'):
|
|
|
|
if correct_format:
|
|
|
|
if can_preview_from_domain:
|
|
|
|
return {'can_preview': True, 'quality': quality}
|
2013-07-19 12:35:35 +02:00
|
|
|
else:
|
2015-04-02 12:28:06 +02:00
|
|
|
return {'can_preview': False,
|
|
|
|
'fixable': 'Enable resource_proxy',
|
|
|
|
'quality': quality}
|
|
|
|
else:
|
|
|
|
return {'can_preview': False, 'quality': quality}
|
2013-07-19 12:35:35 +02:00
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
return correct_format and can_preview_from_domain
|
2011-10-28 14:03:31 +02:00
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
def preview_template(self, context, data_dict):
|
|
|
|
return 'dataviewer/wms.html'
|
2013-06-13 22:51:01 +02:00
|
|
|
|
2015-04-01 12:02:31 +02:00
|
|
|
def setup_template_variables(self, context, data_dict):
|
|
|
|
import ckanext.resourceproxy.plugin as proxy
|
2015-04-02 12:28:06 +02:00
|
|
|
self.same_domain = data_dict['resource'].get('on_same_domain')
|
2015-04-01 12:02:31 +02:00
|
|
|
if self.proxy_is_enabled and not self.same_domain:
|
|
|
|
data_dict['resource']['proxy_url'] = proxy.get_proxified_resource_url(data_dict)
|
2013-06-13 22:51:01 +02:00
|
|
|
|
2015-04-01 12:02:31 +02:00
|
|
|
else:
|
|
|
|
data_dict['resource']['proxy_url'] = data_dict['resource']['url']
|
|
|
|
|
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
class WMSPreview(WMSView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-04-01 12:02:31 +02:00
|
|
|
class GeoJSONView(DataViewBase):
|
2013-10-07 00:18:59 +02:00
|
|
|
p.implements(p.ITemplateHelpers, inherit=True)
|
2013-06-13 22:51:01 +02:00
|
|
|
|
|
|
|
GeoJSON = ['gjson', 'geojson']
|
|
|
|
|
|
|
|
def update_config(self, config):
|
|
|
|
''' Set up the resource library, public directory and
|
|
|
|
template directory for the preview
|
|
|
|
'''
|
|
|
|
|
2014-03-07 17:26:50 +01:00
|
|
|
mimetypes.add_type('application/json', '.geojson')
|
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
# IResourceView (CKAN >=2.3)
|
|
|
|
def info(self):
|
|
|
|
return {'name': 'geojson_view',
|
|
|
|
'title': 'GeoJSON',
|
|
|
|
'icon': 'map-marker',
|
|
|
|
'iframed': True,
|
|
|
|
'default_title': p.toolkit._('GeoJSON'),
|
|
|
|
}
|
|
|
|
|
|
|
|
def can_view(self, data_dict):
|
|
|
|
resource = data_dict['resource']
|
|
|
|
format_lower = resource['format'].lower()
|
|
|
|
|
|
|
|
if format_lower in self.GeoJSON:
|
|
|
|
return self.same_domain or self.proxy_is_enabled
|
|
|
|
return False
|
|
|
|
|
|
|
|
def view_template(self, context, data_dict):
|
|
|
|
return 'dataviewer/geojson.html'
|
|
|
|
|
|
|
|
# IResourcePreview (CKAN < 2.3)
|
|
|
|
|
|
|
|
def can_preview(self, data_dict):
|
|
|
|
format_lower = data_dict['resource']['format'].lower()
|
|
|
|
|
|
|
|
correct_format = format_lower in self.GeoJSON
|
|
|
|
can_preview_from_domain = self.proxy_is_enabled or data_dict['resource']['on_same_domain']
|
|
|
|
quality = 2
|
|
|
|
|
|
|
|
if p.toolkit.check_ckan_version('2.1'):
|
|
|
|
if correct_format:
|
|
|
|
if can_preview_from_domain:
|
|
|
|
return {'can_preview': True, 'quality': quality}
|
2013-07-19 12:35:35 +02:00
|
|
|
else:
|
2015-04-02 12:28:06 +02:00
|
|
|
return {'can_preview': False,
|
|
|
|
'fixable': 'Enable resource_proxy',
|
|
|
|
'quality': quality}
|
|
|
|
else:
|
|
|
|
return {'can_preview': False, 'quality': quality}
|
2013-07-19 12:35:35 +02:00
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
return correct_format and can_preview_from_domain
|
2013-06-13 22:51:01 +02:00
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
def preview_template(self, context, data_dict):
|
|
|
|
return 'dataviewer/geojson.html'
|
2013-10-07 00:18:59 +02:00
|
|
|
|
2015-04-02 12:28:06 +02:00
|
|
|
# ITemplateHelpers
|
2013-10-07 00:18:59 +02:00
|
|
|
|
|
|
|
def get_helpers(self):
|
|
|
|
from ckanext.spatial import helpers as spatial_helpers
|
|
|
|
|
|
|
|
# CKAN does not allow to define two helpers with the same name
|
|
|
|
# As this plugin can be loaded independently of the main spatial one
|
|
|
|
# We define a different helper pointing to the same function
|
|
|
|
return {
|
|
|
|
'get_common_map_config_geojson' : spatial_helpers.get_common_map_config,
|
|
|
|
}
|
2015-04-02 12:28:06 +02:00
|
|
|
|
|
|
|
class GeoJSONPreview(GeoJSONView):
|
|
|
|
pass
|