From 75a37f8ce46ca6915cc704c1d09f1e5fb07ecc11 Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Wed, 11 Dec 2019 12:31:25 +0200 Subject: [PATCH] Split plugins --- .../spatial/{plugin.py => plugin/__init__.py} | 55 +++++-------------- ckanext/spatial/plugin/flask_plugin.py | 41 ++++++++++++++ ckanext/spatial/plugin/pylons_plugin.py | 41 ++++++++++++++ pip-requirements.txt | 1 + setup.py | 4 ++ 5 files changed, 101 insertions(+), 41 deletions(-) rename ckanext/spatial/{plugin.py => plugin/__init__.py} (90%) create mode 100644 ckanext/spatial/plugin/flask_plugin.py create mode 100644 ckanext/spatial/plugin/pylons_plugin.py diff --git a/ckanext/spatial/plugin.py b/ckanext/spatial/plugin/__init__.py similarity index 90% rename from ckanext/spatial/plugin.py rename to ckanext/spatial/plugin/__init__.py index e2d73df..9847c42 100644 --- a/ckanext/spatial/plugin.py +++ b/ckanext/spatial/plugin/__init__.py @@ -9,6 +9,14 @@ from ckan import plugins as p from ckan.lib.helpers import json +if p.toolkit.check_ckan_version(min_version="2.9"): + from ckanext.spatial.plugin.flask_plugin import ( + SpatialQueryMixin, HarvestMetadataApiMixin + ) +else: + from ckanext.spatial.plugin.pylons_plugin import ( + SpatialQueryMixin, HarvestMetadataApiMixin + ) def check_geoalchemy_requirement(): '''Checks if a suitable geoalchemy version installed @@ -77,9 +85,9 @@ class SpatialMetadata(p.SingletonPlugin): ''' Set up the resource library, public directory and template directory for all the spatial extensions ''' - p.toolkit.add_public_directory(config, 'public') - p.toolkit.add_template_directory(config, 'templates') - p.toolkit.add_resource('public', 'ckanext-spatial') + p.toolkit.add_public_directory(config, '../public') + p.toolkit.add_template_directory(config, '../templates') + p.toolkit.add_resource('../public', 'ckanext-spatial') # Add media types for common extensions not included in the mimetypes # module @@ -150,9 +158,8 @@ class SpatialMetadata(p.SingletonPlugin): 'get_common_map_config' : spatial_helpers.get_common_map_config, } -class SpatialQuery(p.SingletonPlugin): +class SpatialQuery(SpatialQueryMixin, p.SingletonPlugin): - p.implements(p.IRoutes, inherit=True) p.implements(p.IPackageController, inherit=True) p.implements(p.IConfigurable, inherit=True) @@ -166,13 +173,6 @@ class SpatialQuery(p.SingletonPlugin): 'Please upgrade CKAN or select the \'postgis\' backend.' raise p.toolkit.CkanVersionException(msg) - def before_map(self, map): - - map.connect('api_spatial_query', '/api/2/search/{register:dataset|package}/geo', - controller='ckanext.spatial.controllers.api:ApiController', - action='spatial_query') - return map - def before_index(self, pkg_dict): import shapely import shapely.geometry @@ -390,7 +390,7 @@ class SpatialQuery(p.SingletonPlugin): search_results['results'] = pkgs return search_results -class HarvestMetadataApi(p.SingletonPlugin): +class HarvestMetadataApi(HarvestMetadataApiMixin, p.SingletonPlugin): ''' Harvest Metadata API (previously called "InspireApi") @@ -398,31 +398,4 @@ class HarvestMetadataApi(p.SingletonPlugin): A way for a user to view the harvested metadata XML, either as a raw file or styled to view in a web browser. ''' - p.implements(p.IRoutes) - - def before_map(self, route_map): - controller = "ckanext.spatial.controllers.api:HarvestMetadataApiController" - - # Showing the harvest object content is an action of the default - # harvest plugin, so just redirect there - route_map.redirect('/api/2/rest/harvestobject/{id:.*}/xml', - '/harvest/object/{id}', - _redirect_code='301 Moved Permanently') - - route_map.connect('/harvest/object/{id}/original', controller=controller, - action='display_xml_original') - - route_map.connect('/harvest/object/{id}/html', controller=controller, - action='display_html') - route_map.connect('/harvest/object/{id}/html/original', controller=controller, - action='display_html_original') - - # Redirect old URL to a nicer and unversioned one - route_map.redirect('/api/2/rest/harvestobject/:id/html', - '/harvest/object/{id}/html', - _redirect_code='301 Moved Permanently') - - return route_map - - def after_map(self, route_map): - return route_map + pass diff --git a/ckanext/spatial/plugin/flask_plugin.py b/ckanext/spatial/plugin/flask_plugin.py new file mode 100644 index 0000000..cbd5006 --- /dev/null +++ b/ckanext/spatial/plugin/flask_plugin.py @@ -0,0 +1,41 @@ +import ckan.plugins as p + + + +class SpatialQueryMixin(p.SingletonPlugin): + p.implements(p.IRoutes, inherit=True) + + # IRoutes + def before_map(self, map): + + map.connect('api_spatial_query', '/api/2/search/{register:dataset|package}/geo', + controller='ckanext.spatial.controllers.api:ApiController', + action='spatial_query') + return map + +class HarvestMetadataApiMixin(p.SingletonPlugin): + p.implements(p.IRoutes, inherit=True) + + def before_map(self, route_map): + controller = "ckanext.spatial.controllers.api:HarvestMetadataApiController" + + # Showing the harvest object content is an action of the default + # harvest plugin, so just redirect there + route_map.redirect('/api/2/rest/harvestobject/{id:.*}/xml', + '/harvest/object/{id}', + _redirect_code='301 Moved Permanently') + + route_map.connect('/harvest/object/{id}/original', controller=controller, + action='display_xml_original') + + route_map.connect('/harvest/object/{id}/html', controller=controller, + action='display_html') + route_map.connect('/harvest/object/{id}/html/original', controller=controller, + action='display_html_original') + + # Redirect old URL to a nicer and unversioned one + route_map.redirect('/api/2/rest/harvestobject/:id/html', + '/harvest/object/{id}/html', + _redirect_code='301 Moved Permanently') + + return route_map diff --git a/ckanext/spatial/plugin/pylons_plugin.py b/ckanext/spatial/plugin/pylons_plugin.py new file mode 100644 index 0000000..cbd5006 --- /dev/null +++ b/ckanext/spatial/plugin/pylons_plugin.py @@ -0,0 +1,41 @@ +import ckan.plugins as p + + + +class SpatialQueryMixin(p.SingletonPlugin): + p.implements(p.IRoutes, inherit=True) + + # IRoutes + def before_map(self, map): + + map.connect('api_spatial_query', '/api/2/search/{register:dataset|package}/geo', + controller='ckanext.spatial.controllers.api:ApiController', + action='spatial_query') + return map + +class HarvestMetadataApiMixin(p.SingletonPlugin): + p.implements(p.IRoutes, inherit=True) + + def before_map(self, route_map): + controller = "ckanext.spatial.controllers.api:HarvestMetadataApiController" + + # Showing the harvest object content is an action of the default + # harvest plugin, so just redirect there + route_map.redirect('/api/2/rest/harvestobject/{id:.*}/xml', + '/harvest/object/{id}', + _redirect_code='301 Moved Permanently') + + route_map.connect('/harvest/object/{id}/original', controller=controller, + action='display_xml_original') + + route_map.connect('/harvest/object/{id}/html', controller=controller, + action='display_html') + route_map.connect('/harvest/object/{id}/html/original', controller=controller, + action='display_html_original') + + # Redirect old URL to a nicer and unversioned one + route_map.redirect('/api/2/rest/harvestobject/:id/html', + '/harvest/object/{id}/html', + _redirect_code='301 Moved Permanently') + + return route_map diff --git a/pip-requirements.txt b/pip-requirements.txt index 2251af8..e948c50 100644 --- a/pip-requirements.txt +++ b/pip-requirements.txt @@ -6,3 +6,4 @@ lxml>=2.3 argparse pyparsing>=2.1.10 requests>=1.1.0 +future>=0.18.2 diff --git a/setup.py b/setup.py index 294d144..b5820e9 100644 --- a/setup.py +++ b/setup.py @@ -46,5 +46,9 @@ setup( [ckan.test_plugins] test_spatial_plugin = ckanext.spatial.tests.test_plugin.plugin:TestSpatialPlugin + [console_scripts] + spatial = ckanext.spatial.cli:spatial + ckan-pycsw = ckanext.spatial.cli:ckan_pycsw + validation = ckanext.spatial.cli:validation """, )