From c291e30a0e306966d6297d47f57283f4f0c28ac4 Mon Sep 17 00:00:00 2001 From: Sol Lee Date: Wed, 1 Apr 2015 18:02:31 +0800 Subject: [PATCH 1/2] make wms and geojson viewer compatible with CKAN 2.3 --- ckanext/spatial/nongeos_plugin.py | 173 +++++++++++++++++++----------- setup.py | 6 +- 2 files changed, 116 insertions(+), 63 deletions(-) diff --git a/ckanext/spatial/nongeos_plugin.py b/ckanext/spatial/nongeos_plugin.py index 1597e01..421187b 100644 --- a/ckanext/spatial/nongeos_plugin.py +++ b/ckanext/spatial/nongeos_plugin.py @@ -2,60 +2,106 @@ import mimetypes from logging import getLogger from ckan import plugins as p +import ckan.lib.datapreview as datapreview log = getLogger(__name__) -class WMSPreview(p.SingletonPlugin): - +class DataViewBase(p.SingletonPlugin): + '''This base class is for view extensions. ''' + if p.toolkit.check_ckan_version('2.3'): + p.implements(p.IResourceView, inherit=True) + else: + p.implements(p.IResourcePreview, inherit=True) p.implements(p.IConfigurer, inherit=True) - p.implements(p.IResourcePreview, inherit=True) + p.implements(p.IConfigurable, inherit=True) - WMS = ['wms'] + proxy_is_enabled = False + same_domain = False 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')) + 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 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) + if p.toolkit.check_ckan_version('2.3'): + self.same_domain = datapreview.on_same_domain(data_dict) else: - p.toolkit.c.resource['proxy_url'] = data_dict['resource']['url'] + self.same_domain = datapreview._on_same_domain(data_dict) + 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) - 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 +class WMSView(DataViewBase): + WMS = ['wms'] - if p.toolkit.check_ckan_version('2.1'): - if correct_format: - if can_preview_from_domain: - return {'can_preview': True, 'quality': quality} + if p.toolkit.check_ckan_version('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' + else: + 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} + else: + return {'can_preview': False, + 'fixable': 'Enable resource_proxy', + 'quality': quality} else: - return {'can_preview': False, - 'fixable': 'Enable resource_proxy', - 'quality': quality} - else: - return {'can_preview': False, 'quality': quality} + return {'can_preview': False, 'quality': quality} - return correct_format and can_preview_from_domain + return correct_format and can_preview_from_domain - def preview_template(self, context, data_dict): - return 'dataviewer/wms.html' + def preview_template(self, context, data_dict): + return 'dataviewer/wms.html' + + def setup_template_variables(self, context, data_dict): + import ckanext.resourceproxy.plugin as proxy + if p.toolkit.check_ckan_version('2.3'): + self.same_domain = datapreview.on_same_domain(data_dict) + else: + self.same_domain = datapreview._on_same_domain(data_dict) + if self.proxy_is_enabled and not self.same_domain: + data_dict['resource']['proxy_url'] = proxy.get_proxified_resource_url(data_dict) + + else: + data_dict['resource']['proxy_url'] = data_dict['resource']['url'] -class GeoJSONPreview(p.SingletonPlugin): - p.implements(p.IConfigurer, inherit=True) - p.implements(p.IResourcePreview, inherit=True) +class GeoJSONView(DataViewBase): p.implements(p.ITemplateHelpers, inherit=True) GeoJSON = ['gjson', 'geojson'] @@ -65,45 +111,50 @@ class GeoJSONPreview(p.SingletonPlugin): 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) - mimetypes.add_type('application/json', '.geojson') - def can_preview(self, data_dict): - format_lower = data_dict['resource']['format'].lower() + if p.toolkit.check_ckan_version('2.3'): + def info(self): + return {'name': 'geojson_view', + 'title': 'GeoJSON', + 'icon': 'map-marker', + 'iframed': True, + 'default_title': p.toolkit._('GeoJSON'), + } - correct_format = format_lower in self.GeoJSON - can_preview_from_domain = self.proxy_enabled or data_dict['resource']['on_same_domain'] - quality = 2 + def can_view(self, data_dict): + resource = data_dict['resource'] + format_lower = resource['format'].lower() - if p.toolkit.check_ckan_version('2.1'): - if correct_format: - if can_preview_from_domain: - return {'can_preview': True, 'quality': quality} + 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' + else: + 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} + else: + return {'can_preview': False, + 'fixable': 'Enable resource_proxy', + 'quality': quality} else: - return {'can_preview': False, - 'fixable': 'Enable resource_proxy', - 'quality': quality} - else: - return {'can_preview': False, 'quality': quality} + return {'can_preview': False, 'quality': quality} - return correct_format and can_preview_from_domain + 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' + def preview_template(self, context, data_dict): + return 'dataviewer/geojson.html' ## ITemplateHelpers diff --git a/setup.py b/setup.py index 1822fb1..d408422 100644 --- a/setup.py +++ b/setup.py @@ -30,8 +30,10 @@ setup( [ckan.plugins] spatial_metadata=ckanext.spatial.plugin:SpatialMetadata spatial_query=ckanext.spatial.plugin:SpatialQuery - wms_preview=ckanext.spatial.nongeos_plugin:WMSPreview - geojson_preview=ckanext.spatial.nongeos_plugin:GeoJSONPreview + wms_view=ckanext.spatial.nongeos_plugin:WMSView + geojson_view=ckanext.spatial.nongeos_plugin:GeoJSONView + wms_preview=ckanext.spatial.nongeos_plugin:WMSView + geojson_preview=ckanext.spatial.nongeos_plugin:GeoJSONView cswserver=ckanext.spatial.plugin:CatalogueServiceWeb spatial_harvest_metadata_api=ckanext.spatial.plugin:HarvestMetadataApi From 4a142e837745b80adb9ed1aeef693899aae99889 Mon Sep 17 00:00:00 2001 From: amercader Date: Thu, 2 Apr 2015 11:28:06 +0100 Subject: [PATCH 2/2] [#98] Minor tweaks to @u10313335 PR * Keep old entry points for preview plugins so people does not need to do python setup.py develop * Version checks should be done including the min_version parameter, otherwise they will fail for 2.4, 2.5... * No need to define different methods for different interfaces. If running eg 2.3, the IResourcePreview interface methods will simply be ignored. * Simplified same domain checking * Add note about these being eventually migrated to ckanext-geoviews * Added docs --- ckanext/spatial/nongeos_plugin.py | 165 ++++++++++++++++-------------- doc/previews.rst | 11 +- setup.py | 4 +- 3 files changed, 95 insertions(+), 85 deletions(-) diff --git a/ckanext/spatial/nongeos_plugin.py b/ckanext/spatial/nongeos_plugin.py index 421187b..02fb126 100644 --- a/ckanext/spatial/nongeos_plugin.py +++ b/ckanext/spatial/nongeos_plugin.py @@ -1,8 +1,9 @@ +# TODO: Move these to ckanext-geoviews + import mimetypes from logging import getLogger from ckan import plugins as p -import ckan.lib.datapreview as datapreview log = getLogger(__name__) @@ -10,7 +11,7 @@ log = getLogger(__name__) class DataViewBase(p.SingletonPlugin): '''This base class is for view extensions. ''' - if p.toolkit.check_ckan_version('2.3'): + if p.toolkit.check_ckan_version(min_version='2.3'): p.implements(p.IResourceView, inherit=True) else: p.implements(p.IResourcePreview, inherit=True) @@ -33,67 +34,64 @@ class DataViewBase(p.SingletonPlugin): def setup_template_variables(self, context, data_dict): import ckanext.resourceproxy.plugin as proxy - if p.toolkit.check_ckan_version('2.3'): - self.same_domain = datapreview.on_same_domain(data_dict) - else: - self.same_domain = datapreview._on_same_domain(data_dict) + self.same_domain = data_dict['resource'].get('on_same_domain') 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) + class WMSView(DataViewBase): WMS = ['wms'] - if p.toolkit.check_ckan_version('2.3'): - def info(self): - return {'name': 'wms_view', - 'title': 'wms', - 'icon': 'map-marker', - 'iframed': True, - 'default_title': p.toolkit._('WMS'), - } + # 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() + 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 + 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' - else: - def can_preview(self, data_dict): - format_lower = data_dict['resource']['format'].lower() + def view_template(self, context, data_dict): + return 'dataviewer/wms.html' - correct_format = format_lower in self.WMS - can_preview_from_domain = self.proxy_is_enabled or data_dict['resource']['on_same_domain'] - quality = 2 + # IResourcePreview (CKAN < 2.3) - 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} + 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} else: - return {'can_preview': False, 'quality': quality} + 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 + return correct_format and can_preview_from_domain - def preview_template(self, context, data_dict): - return 'dataviewer/wms.html' + def preview_template(self, context, data_dict): + return 'dataviewer/wms.html' def setup_template_variables(self, context, data_dict): import ckanext.resourceproxy.plugin as proxy - if p.toolkit.check_ckan_version('2.3'): - self.same_domain = datapreview.on_same_domain(data_dict) - else: - self.same_domain = datapreview._on_same_domain(data_dict) + self.same_domain = data_dict['resource'].get('on_same_domain') if self.proxy_is_enabled and not self.same_domain: data_dict['resource']['proxy_url'] = proxy.get_proxified_resource_url(data_dict) @@ -101,6 +99,10 @@ class WMSView(DataViewBase): data_dict['resource']['proxy_url'] = data_dict['resource']['url'] +class WMSPreview(WMSView): + pass + + class GeoJSONView(DataViewBase): p.implements(p.ITemplateHelpers, inherit=True) @@ -113,50 +115,52 @@ class GeoJSONView(DataViewBase): mimetypes.add_type('application/json', '.geojson') - if p.toolkit.check_ckan_version('2.3'): - def info(self): - return {'name': 'geojson_view', - 'title': 'GeoJSON', - 'icon': 'map-marker', - 'iframed': True, - 'default_title': p.toolkit._('GeoJSON'), - } + # 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() + 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 + 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' - else: - def can_preview(self, data_dict): - format_lower = data_dict['resource']['format'].lower() + def view_template(self, context, data_dict): + return 'dataviewer/geojson.html' - correct_format = format_lower in self.GeoJSON - can_preview_from_domain = self.proxy_is_enabled or data_dict['resource']['on_same_domain'] - quality = 2 + # IResourcePreview (CKAN < 2.3) - 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} + 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} else: - return {'can_preview': False, 'quality': quality} + 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 + return correct_format and can_preview_from_domain - def preview_template(self, context, data_dict): - return 'dataviewer/geojson.html' + def preview_template(self, context, data_dict): + return 'dataviewer/geojson.html' - ## ITemplateHelpers + # ITemplateHelpers def get_helpers(self): from ckanext.spatial import helpers as spatial_helpers @@ -167,3 +171,6 @@ class GeoJSONView(DataViewBase): return { 'get_common_map_config_geojson' : spatial_helpers.get_common_map_config, } + +class GeoJSONPreview(GeoJSONView): + pass diff --git a/doc/previews.rst b/doc/previews.rst index cf1b562..71a31cc 100644 --- a/doc/previews.rst +++ b/doc/previews.rst @@ -17,13 +17,14 @@ The GeoJSON previewer is based on Leaflet_. It will render GeoJSON_ files on a map and add a popup showing the features properties, for those resources that have a format of ``geojson`` or ``gjson``. -To enable the GeoJSON previewer you need to add the ``geojson_preview`` plugin +To enable the GeoJSON previewer you need to add the ``geojson_view`` plugin to your ini file. This plugin also requires the `resource_proxy`_ plugin (Make sure you load the ``resource_proxy`` plugin before any other from the spatial extension):: - ckan.plugins = resource_proxy geojson_preview + ckan.plugins = resource_proxy geojson_view +.. note:: If using CKAN < 2.3, use `geojson_preview` WMS Preview ----------- @@ -39,12 +40,14 @@ just the main WMS service endpoint, for example: http://vmap0.tiles.osgeo.org/wms/vmap0?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1 -To enable the WMS previewer you need to add the ``wms_preview`` plugin to your +To enable the WMS previewer you need to add the ``wms_view`` plugin to your ini file. This plugin also requires the `resource_proxy`_ plugin (Make sure you load the ``resource_proxy`` plugin before any other from the spatial extension:: - ckan.plugins = resource_proxy wms_preview + ckan.plugins = resource_proxy wms_view + +.. note:: If using CKAN < 2.3, use `geojson_preview` .. note:: Please note that the WMS previewer included in ckanext-spatial is just a proof of concept and has important limitations, and is diff --git a/setup.py b/setup.py index f3e8d37..f402b0e 100644 --- a/setup.py +++ b/setup.py @@ -32,8 +32,8 @@ setup( spatial_query=ckanext.spatial.plugin:SpatialQuery wms_view=ckanext.spatial.nongeos_plugin:WMSView geojson_view=ckanext.spatial.nongeos_plugin:GeoJSONView - wms_preview=ckanext.spatial.nongeos_plugin:WMSView - geojson_preview=ckanext.spatial.nongeos_plugin:GeoJSONView + wms_preview=ckanext.spatial.nongeos_plugin:WMSPreview + geojson_preview=ckanext.spatial.nongeos_plugin:GeoJSONPreview cswserver=ckanext.spatial.plugin:CatalogueServiceWeb spatial_harvest_metadata_api=ckanext.spatial.plugin:HarvestMetadataApi