Skip to content

Instantly share code, notes, and snippets.

@seanh
Created April 10, 2012 16:45
Show Gist options
  • Save seanh/2352758 to your computer and use it in GitHub Desktop.
Save seanh/2352758 to your computer and use it in GitHub Desktop.
Example CKAN extension with custom package controller
from ckan.plugins import IRoutes, implements, SingletonPlugin
from ckan.controllers.package import PackageController
from ckan.config.routing import SubMapper
class TestPackageControllerPlugin(SingletonPlugin):
implements(IRoutes, inherit=True)
def before_map(self, map):
# Default mappings copied from ckan/config/routing.py that we want to preserve.
with SubMapper(map, controller='package') as m:
m.connect('/dataset/{action}',
requirements=dict(action='|'.join([
'list',
'new',
'autocomplete',
'search'
]))
)
m.connect('/dataset/{action}/{id}/{revision}', action='read_ajax',
requirements=dict(action='|'.join([
'read',
'edit',
'authz',
'history',
]))
)
m.connect('/dataset/{action}/{id}',
requirements=dict(action='|'.join([
'edit',
'editresources',
'authz',
'history',
'read_ajax',
'history_ajax',
]))
)
m.connect('/dataset/{id}.{format}', action='read')
m.connect('/dataset/{id}/resource/{resource_id}', action='resource_read')
# Our new custom mapping.
map.connect('/dataset/{id}',
controller='ckanext.testpackagecontroller.plugin:TestPackageController',
action='read',
)
return map
class TestPackageController(PackageController):
def read(self, id):
return 'foobar'
@LingboTang
Copy link

Do you know what is the correct template to replace /dataset/? Is it /{dataset_type}/ or /{type}/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment