spatial-d4science/ckanext/spatial/nongeos_plugin.py

116 lines
4.1 KiB
Python

from logging import getLogger
from ckan import plugins as p
log = getLogger(__name__)
class WMSPreview(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourcePreview, inherit=True)
WMS = ['wms']
def update_config(self, config):
p.toolkit.add_public_directory(config, 'public')
p.toolkit.add_template_directory(config, 'templates')
p.toolkit.add_resource('public', 'ckanext-spatial')
self.proxy_enabled = p.toolkit.asbool(config.get('ckan.resource_proxy_enabled', 'False'))
def setup_template_variables(self, context, data_dict):
import ckanext.resourceproxy.plugin as proxy
if self.proxy_enabled and not data_dict['resource']['on_same_domain']:
p.toolkit.c.resource['proxy_url'] = proxy.get_proxified_resource_url(data_dict)
else:
p.toolkit.c.resource['proxy_url'] = data_dict['resource']['url']
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_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}
else:
return {'can_preview': False,
'fixable': 'Enable resource_proxy',
'quality': quality}
else:
return {'can_preview': False, 'quality': quality}
return correct_format and can_preview_from_domain
def preview_template(self, context, data_dict):
return 'dataviewer/wms.html'
class GeoJSONPreview(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourcePreview, inherit=True)
p.implements(p.ITemplateHelpers, inherit=True)
GeoJSON = ['gjson', 'geojson']
def update_config(self, config):
''' Set up the resource library, public directory and
template directory for the preview
'''
p.toolkit.add_public_directory(config, 'public')
p.toolkit.add_template_directory(config, 'templates')
p.toolkit.add_resource('public', 'ckanext-spatial')
self.proxy_enabled = config.get(
'ckan.resource_proxy_enabled', False)
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_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}
else:
return {'can_preview': False,
'fixable': 'Enable resource_proxy',
'quality': quality}
else:
return {'can_preview': False, 'quality': quality}
return correct_format and can_preview_from_domain
def setup_template_variables(self, context, data_dict):
import ckanext.resourceproxy.plugin as proxy
if (self.proxy_enabled
and not data_dict['resource']['on_same_domain']):
p.toolkit.c.resource['original_url'] = p.toolkit.c.resource['url']
p.toolkit.c.resource['url'] = proxy.get_proxified_resource_url(
data_dict)
def preview_template(self, context, data_dict):
return 'dataviewer/geojson.html'
## ITemplateHelpers
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,
}