diff --git a/ckanext/odsh/collection/controller.py b/ckanext/odsh/collection/controller.py deleted file mode 100644 index 511460d70b1976a4daef43b4a3987357675ea458..0000000000000000000000000000000000000000 --- a/ckanext/odsh/collection/controller.py +++ /dev/null @@ -1,41 +0,0 @@ -from ckan.lib.helpers import is_url, url_for -import ckan.plugins.toolkit as toolkit -from ckan.controllers.package import PackageController -from .helpers import get_latest_resources_for_format, get_latest_dataset - - -class LatestDatasetController(PackageController): - - def latest_dataset(self, id): - latest_dataset = get_latest_dataset(id) - if latest_dataset is None: - toolkit.abort(404) - toolkit.redirect_to(controller='dataset', - action='read', id=latest_dataset) - - -class LatestRecourcesController(PackageController): - - def latest_resource(self, id, resource_format): - latest_resources = get_latest_resources_for_format(id, resource_format) - if latest_resources is None: - toolkit.abort(404) - url_type = latest_resources.get('url_type') - if url_type is None: - resource_url = latest_resources.get('url') - toolkit.redirect_to(resource_url) - if url_type == 'upload': - download_package_id = latest_resources.get('package_id') - download_resource_id = latest_resources.get('id') - pre_resource_url = latest_resources.get('url') - if is_url(pre_resource_url): - url_resource = pre_resource_url - else: - url_resource = url_for(controller='dataset', - action='resource_download', - id=download_package_id, - resource_id=download_resource_id, - filename=pre_resource_url, - qualified=True) - toolkit.redirect_to(url_resource) - toolkit.abort(404) diff --git a/ckanext/odsh/collection/helpers.py b/ckanext/odsh/collection/helpers.py index cefd9f034ff25be5be6bf2dc292c5e2c9912f92a..ce519e6886ee88b9bf8fba51053cac8e3e4771bb 100644 --- a/ckanext/odsh/collection/helpers.py +++ b/ckanext/odsh/collection/helpers.py @@ -154,12 +154,7 @@ def url_from_id(package_id): return helpers.url_for(controller='dataset', action='read', id=package_id) def url_last_member(name_collection): - return helpers.url_for( - controller='ckanext.odsh.collection.controller:LatestDatasetController', - action='latest', - id=name_collection - ) - + return toolkit.url_for('odsh_collection.latest_dataset', id=name_collection) def get_latest_dataset(collection_name): collection_info = get_collection_info(collection_name) diff --git a/ckanext/odsh/collection/plugin.py b/ckanext/odsh/collection/plugin.py index 021ac971b8fdd044aa8714f04484ef1f817df48f..7ac1d0f4461155075a071ac3a0f6493ae2ab32b3 100644 --- a/ckanext/odsh/collection/plugin.py +++ b/ckanext/odsh/collection/plugin.py @@ -1,14 +1,13 @@ - from ckan.lib.plugins import DefaultTranslation, DefaultDatasetForm import ckan.plugins as plugins import ckan.plugins.toolkit as toolkit from . import helpers as collection_helpers -from routes.mapper import SubMapper +from flask import Blueprint class CollectionsPlugin(plugins.SingletonPlugin, DefaultDatasetForm): plugins.implements(plugins.IDatasetForm, inherit=True) - plugins.implements(plugins.IRoutes, inherit=True) plugins.implements(plugins.ITemplateHelpers) + plugins.implements(plugins.IBlueprint) # IDataSetForm @@ -22,34 +21,15 @@ class CollectionsPlugin(plugins.SingletonPlugin, DefaultDatasetForm): return 'package/collection_read.html' - # IRoutes - def before_map(self, map): - - map.connect( - '/collection/{id}/aktuell', - controller='ckanext.odsh.collection.controller:LatestDatasetController', - action='latest_dataset' - ) + # IBlueprint + def get_blueprint(self): + blueprint = Blueprint('odsh_collection', self.__module__) - map.connect( - '/collection/{id}/aktuell.{resource_format}', - controller='ckanext.odsh.collection.controller:LatestRecourcesController', - action='latest_resource' - ) + blueprint.add_url_rule('/collection/<id>/aktuell', 'latest_dataset', self.latest_dataset) + blueprint.add_url_rule('/collection/<id>/aktuell.<resource_format>', 'latest_resource', self.latest_resource) + return blueprint - with SubMapper( - map, - controller='ckanext.odsh.collection.controller:LatestDatasetController', - path_prefix='/collection/' - ) as m: - m.connect('latest', '{id}/aktuell', action='latest') - m.connect('latest_resource', '{id}/aktuell.{resource_format}', action='latest_resource') - return map - def after_map(self, map): - return map - - # ITemplateHelpers def get_helpers(self): # Template helper function names should begin with the name of the @@ -61,4 +41,35 @@ class CollectionsPlugin(plugins.SingletonPlugin, DefaultDatasetForm): 'get_collection_info': collection_helpers.get_collection_info, 'url_from_id': collection_helpers.url_from_id, } - \ No newline at end of file + + def latest_dataset(self, id): + latest_dataset = collection_helpers.get_latest_dataset(id) + if latest_dataset is None: + toolkit.abort(404) + return toolkit.redirect_to('dataset.read', id=latest_dataset) + + + def latest_resource(self, id, resource_format): + latest_resources = collection_helpers.get_latest_resources_for_format(id, resource_format) + if latest_resources is None: + toolkit.abort(404) + url_type = latest_resources.get('url_type') + if url_type is None: + resource_url = latest_resources.get('url') + toolkit.redirect_to(resource_url) + if url_type == 'upload': + download_package_id = latest_resources.get('package_id') + download_resource_id = latest_resources.get('id') + pre_resource_url = latest_resources.get('url') + if toolkit.is_url(pre_resource_url): + url_resource = pre_resource_url + else: + url_resource = toolkit.url_for(controller='dataset', + action='resource_download', + id=download_package_id, + resource_id=download_resource_id, + filename=pre_resource_url) + toolkit.redirect_to(url_resource) + else: + toolkit.abort(404) + diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py index 3621dd5dc8fd53af82c761589f95a8ecef646862..0ee35bf262e896f7816b7caa6748ee7ec9aebbf6 100644 --- a/ckanext/odsh/plugin.py +++ b/ckanext/odsh/plugin.py @@ -412,6 +412,3 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm if score > 0: dict_pkg['openness'] = OdshPlugin.scores[score-1] - - -