Skip to content

Commit

Permalink
[#2673] Deleting routes. Test started, not complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Read committed Oct 6, 2015
1 parent 85108d8 commit afb174e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
58 changes: 57 additions & 1 deletion ckan/plugins/toolkit.py
Expand Up @@ -365,9 +365,65 @@ def _requires_ckan_version(cls, min_version, max_version=None):
error = 'Requires ckan version %s or higher' % min_version
else:
error = 'Requires ckan version between %s and %s' % \
(min_version, max_version)
(min_version, max_version)
raise CkanVersionException(error)

@classmethod
def __delete_routes_by_path_func(cls, routes_map, match_func):

This comment has been minimized.

Copy link
@wardi

wardi Oct 8, 2015

Contributor

please no double-underscore methods. Python's name munging is nasty when you do this

matches_to_delete = []
for match in routes_map.matchlist:
if match_func(match.routepath):
matches_to_delete.append(match)
for match in matches_to_delete:
routes_map.matchlist.remove(match)

@classmethod
def _delete_routes_by_path(cls, routes_map, paths):
'''Deletes routes from a map. It selects the routes whose path equals
the given strings.
:param routes_map: the existing routes, from which some will be deleted
in-place.
:type routes_map: Routes map object, as provided by the IRoutes
interface
:param paths: the paths to match the paths by for deletion.
:type paths: string or list of strings
'''
if isinstance(paths, basestring):
paths = [paths]
cls.__delete_route_by_func(lambda path: path in paths)

@classmethod
def _delete_routes_by_path_startswith(cls, routes_map, path_startswith):
'''Deletes routes from a map. It selects the routes whose path starts
with the given string.
:param routes_map: the existing routes, from which some will be deleted
in-place.
:type routes_map: Routes map object, as provided by the IRoutes
interface
:param path_startswith: the paths to match the routes by for deletion.
:type path_startswith: string
'''
cls.__delete_route_by_func(lambda path:
path.startswith(path_startswith))

@classmethod
def _delete_routes_by_name(cls, routes_map, route_names):
'''Deletes routes from a map. It selects the routes by name.
:param routes_map: the existing routes, from which some will be deleted
in-place.
:type routes_map: Routes map object, as provided by the IRoutes
interface
:param route_names: the names to match the routes by for deletion.
:type route_names: string or list of strings
'''
if isinstance(route_names, basestring):
route_names = [route_names]
for route_name in route_names:
del routes_map._routenames[route_name]

def __getattr__(self, name):
''' return the function/object requested '''
if not self._toolkit:
Expand Down
1 change: 1 addition & 0 deletions ckanext/example_iroutes/__init__.py
@@ -0,0 +1 @@

9 changes: 9 additions & 0 deletions ckanext/example_iroutes/controller.py
@@ -0,0 +1,9 @@
import ckan.lib.base as base

render = base.render


class DashboardController(base.BaseController):

def main(self):
return 'Main Dashboard'
34 changes: 34 additions & 0 deletions ckanext/example_iroutes/plugin.py
@@ -0,0 +1,34 @@
from routes.mapper import SubMapper

import ckan.plugins as plugins
import ckan.plugins.toolkit as tk


class ExampleIRoutesPlugin(plugins.SingletonPlugin):

'''
An example IRoutes plugin that shows:
* Adding a route
* Changing the path of a route
'''

plugins.implements(plugins.IRoutes, inherit=True)

# IRoutes

def before_map(self, map):
controller = 'ckanext.example_iroutes.controller:DashboardController'

# Adding a route
with SubMapper(map, controller=controller) as m:
m.connect('main_dash',
'/dashboard/main', action='main',
ckan_icon='dashboard'),
m.connect('test1',
'/dashboard/main', action='test', testid='1'),

# Changing the path of a route
tk._delete_routes_by_name(map, 'test1')

return map
1 change: 1 addition & 0 deletions ckanext/example_iroutes/tests/__init__.py
@@ -0,0 +1 @@

44 changes: 44 additions & 0 deletions ckanext/example_iroutes/tests/test_iroutes.py
@@ -0,0 +1,44 @@
from nose.tools import assert_equal, assert_in

import ckan.tests.helpers as helpers
import ckan.plugins as plugins
from plugins import toolkit as tk

CONTROLLER = 'ckanext.example_iroutes.controller:DashboardController'


class TestIRoutes(object):

@classmethod
def setup_class(cls):
super(TestIRoutes, cls).setup_class()
plugins.load('example_iroutes')

@classmethod
def teardown_class(cls):
plugins.unload('example_iroutes')
super(TestIRoutes, cls).teardown_class()

def test_added_route(self):
assert_equal(tk.url_for('main_dash'), '/dashboard/main')
assert_equal(tk.url_for(controller=CONTROLLER, action='main'),
'/dashboard/main')


class TestIRoutesFunctional(helpers.FunctionalTestBase):

@classmethod
def setup_class(cls):
super(TestIRoutesFunctional, cls).setup_class()
plugins.load('example_iroutes')

@classmethod
def teardown_class(cls):
plugins.unload('example_iroutes')
super(TestIRoutesFunctional, cls).teardown_class()

def test_added_route(self):
app = self._get_test_app()
response = app.get('/ckan-admin/myext_config_one', status=200)
assert_in('Main Dashboard', response.body)

0 comments on commit afb174e

Please sign in to comment.