--- a/ckanext/example/plugin.py +++ b/ckanext/example/plugin.py @@ -1,6 +1,8 @@ import os from logging import getLogger +from pylons import request +from genshi.input import HTML from genshi.filters.transform import Transformer from ckan.plugins import implements, SingletonPlugin @@ -21,9 +23,11 @@ found in the ``ini``-file. Here we use it to specify the site title, and to tell CKAN to look in this package for templates and resources that customise the core look and feel. + - ``IGenshiStreamFilter`` allows us to filter and transform the HTML stream just before it is rendered. In this case we use it to rename "frob" to "foobar" + - ``IRoutes`` allows us to add new URLs, or override existing URLs. In this example we use it to override the default ``/register`` behaviour with a custom controller @@ -47,8 +51,7 @@ our_public_dir = os.path.join(rootdir, 'ckanext', 'example', 'theme', 'public') template_dir = os.path.join(rootdir, 'ckanext', - 'example', 'theme', - 'templates') + 'example', 'theme', 'templates') # set our local template and resource overrides config['extra_public_paths'] = ','.join([our_public_dir, config.get('extra_public_paths', '')]) @@ -68,9 +71,26 @@ This example filter renames 'frob' to 'foobar' (this string is found in the custom ``home/index.html`` template provided as part of the package). + + It also adds the chosen JQuery plugin to the page if viewing the + dataset edit page (provides a better UX for working with tags with vocabularies) """ stream = stream | Transformer('//p[@id="examplething"]/text()')\ .substitute(r'frob', r'foobar') + + routes = request.environ.get('pylons.routes_dict') + if routes.get('controller') == 'package' \ + and routes.get('action') == 'edit': + stream = stream | Transformer('head').append(HTML( + '' + )) + stream = stream | Transformer('body').append(HTML( + ''' + ' + + ''' + )) + return stream def before_map(self, map): @@ -82,14 +102,18 @@ Note that we have also provided a custom register form template at ``theme/templates/user/register.html``. """ - # Note that when we set up the route, we must use the form - # that gives it a name (i.e. in this case, 'register'), so it - # works correctly with the url_for helper:: - # h.url_for('register') - map.connect('register', - '/user/register', + # Hook in our custom user controller at the points of creation + # and edition. + map.connect('/user/register', controller='ckanext.example.controller:CustomUserController', - action='custom_register') + action='register') + map.connect('/user/edit', + controller='ckanext.example.controller:CustomUserController', + action='edit') + map.connect('/user/edit/{id:.*}', + controller='ckanext.example.controller:CustomUserController', + action='edit') + map.connect('/package/new', controller='package_formalchemy', action='new') map.connect('/package/edit/{id}', controller='package_formalchemy', action='edit') return map