[ckan-dev] implimenting stream filters

Koebrick, Andrew (MNIT) andrew.koebrick at state.mn.us
Wed Oct 9 14:55:23 UTC 2013


I did manage to accomplish what I needed to do, via a solution I am not 100% happy about it.  I ended up injecting some jquery  and altering via the DOM.  Here is the explanation I sent to my other developers, which I will send along in case anyone else wants to use this technique:

1)      Define a "resource" in the extension plugin (/usr/local/ckan/ckanext-geodatamn/ckanext/geodatamn/plugin.py):
plugins.toolkit.add_resource('public/js','js')

Resources are static pages which can be pulled into ckan snippets and templates. They are generally .css or .js files.  You can read about them here: http://docs.ckan.org/en/latest/resources.html

2)      In the template / snippet itself, call the new resource like so:
{% resource 'js/transform_keys.js' %}
Where the first "js" refers to the second value in the resource definition from step 1.

3)      Create a JavaScript function at the path location defined in step 1 to do what you want.  In this case I did it with jquery:
$( document ).ready(function() {
        var field = {
                "dsPurpose" : "Purpose",
                "dsOnlineLink" : "Online link",
                "dsAccessConst" : "Access constraints",
                "dsMetadataUrl" : "Metadata URL",
                "dsOriginator" : "Originating organization",
                "spatial" : "Spatial extent (Geojson)",
                "gdrsDsGuid" : "Geospatial Data Repository System ID",
        };
        for (var key in field){
                $(".dataset-label:contains('"+ key +"')").text(field[key]);
        }
});
Ideally I would have liked to do the transformations server side, but as a kludge, this was good enough.

>> If you want to help marking the IGenshiStreamFilter interface a PR would be most welcomed :).

I am very new to GitHub, and do not know what a PR is (Pull Request?) or how I would go about helping to update the documentation / sample extension.  But if anyone wants to walk me through the process (off list) I would be happy to contribute.  I will be digging deeper and deeper into this code as Minnesota builds out its CKAN instance and it would be great to be able to help improve the documentation as I find problems.

Cheers,

Andrew




From: ckan-dev-bounces at lists.okfn.org [mailto:ckan-dev-bounces at lists.okfn.org] On Behalf Of Adrià Mercader
Sent: Friday, October 04, 2013 5:32 AM
To: CKAN Development Discussions
Subject: Re: [ckan-dev] implimenting stream filters

Hi,

Make sure to use the "latest" version of the CKAN docs (not sure how you got to the "ckan-docs-theme" one). Notice the version on the URL. There are links to docs to the different versions of CKAN in the lower right hand corner

http://docs.ckan.org/en/latest/extensions/index.html

If you want to help marking the IGenshiStreamFilter interface a PR would be most welcomed :). A notice in this docstring should suffice:

https://github.com/okfn/ckan/blob/master/ckan/plugins/interfaces.py#L54


Looking at the example looks like you already figured out how to fix it, but just in case, a custom template + a helper function is the way I would go.

Hope this helps,

Adrià


On 3 October 2013 14:49, Stefan Oderbolz <stefan.oderbolz at liip.ch<mailto:stefan.oderbolz at liip.ch>> wrote:
No, I think this is the most up-to-date documentation. I'd suggest you have a look at the documentation about "Theming" if you want to change the frontend of CKAN (http://docs.ckan.org/en/ckan-docs-theme/theming.html).

On Thu, Oct 3, 2013 at 3:13 PM, Koebrick, Andrew (MNIT) <andrew.koebrick at state.mn.us<mailto:andrew.koebrick at state.mn.us>> wrote:
Stefan,

Thanks for the tips, I will play around.  I had thought that the Genshi was deprecated, but then saw it documented here:
http://docs.ckan.org/en/ckan-docs-theme/writing-extensions.html#example-extension which is marked as "CKAN Documentation 2.2a documentation".

As I continue to get into the guts of the code, is there an accurate up-to-date location I can look to for documentation?

Andrew

From: ckan-dev-bounces at lists.okfn.org<mailto:ckan-dev-bounces at lists.okfn.org> [mailto:ckan-dev-bounces at lists.okfn.org<mailto:ckan-dev-bounces at lists.okfn.org>] On Behalf Of Stefan Oderbolz
Sent: Thursday, October 03, 2013 2:34 AM
To: CKAN Development Discussions
Subject: Re: [ckan-dev] implimenting stream filters

Hi Andrew,

afaik IGenshiStreamFilter is deprecated and you shouldn't use it anymore.

But there are several ways to achieve what you want. By default CKAN shows the key of each extra-fields as-is (you can find the code here: https://github.com/okfn/ckan/blob/master/ckan/templates/package/snippets/additional_info.html#L61).

As you can see the displayed text is translated, so this would be one way to change the name that is displayed (e.g. you could specify "dsOriginator" should be translated to "Originator").

Another way would be to override the "additional_info.html" template, so that for each displayed key the "ds" prefix is getting removed, something like
<span scope="row" class="dataset-label" property="rdfs:label">{{ _(key)|replace('ds', '') }}</span>

Note that this might fail if you have keys that contain "ds", a regex would be a better approach.



On Wed, Oct 2, 2013 at 11:08 PM, Koebrick, Andrew (MNIT) <andrew.koebrick at state.mn.us<mailto:andrew.koebrick at state.mn.us>> wrote:
I am attempting to make an extension that customizes the output of some additional fields associated with datasets in our ckan setup.  For example, we have fields called "dsOriginator" which I would like to have displayed as just "Originator".  I know the extension is at least partially functional since it is pulling our custom templates, but it does not seem to do the transform as expectedLive example at: http://devel.gisdata.mn.gov/dataset/base-2008-naip-airphotos-cir-wms

I would be grateful for any pointers.
++++++++++++++++++++++++++++++++++++++++++

from pylons import config
from pylons import request
from genshi.input import HTML
from genshi.filters import Transformer
from ckan.plugins import IGenshiStreamFilter

import ckan.plugins as plugins

class geodatamn(plugins.SingletonPlugin):
   plugins.implements(plugins.IConfigurer, inherit=True)
   plugins.implements(IGenshiStreamFilter, inherit=True)


   def update_config(self, config):
        plugins.toolkit.add_template_directory(config, 'templates')

   def filter(self, stream):
        """Update metadata field names to be human readable
        """
        stream = stream | Transformer('body').substitute('dsOriginator', 'Originator')

        return stream
++++++++++++++++++++++++++++++++++++++++++++++++++

Andrew Koebrick  |  MINNESOTA GEOSPATIAL INFORMATION OFFICE
Web Coordinator / Systems administrator / Librarian
MN.IT<http://MN.IT> Services @ CENTRAL
651-201-2465<tel:651-201-2465> (w)  |   651-296-6398<tel:651-296-6398> (f) |  andrew.koebrick at state.mn.us<mailto:andrew.koebrick at state.mn.us>
658 Cedar St., Room 300, St. Paul, MN 55155, www.mngeo.state.mn.us<http://www.mngeo.state.mn.us/>


Information Technology for Minnesota Government   |   mn.gov/oet<http://www.mn.gov/oet>

 <http://www.mn.gov/oet>
 <http://www.mn.gov/oet>

_______________________________________________
ckan-dev mailing list
ckan-dev at lists.okfn.org
http://lists.okfn.org/mailman/listinfo/ckan-dev
Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev<http://www.mn.gov/oet>


<http://www.mn.gov/oet>
 <http://www.mn.gov/oet>
-- <http://www.mn.gov/oet>
Liip AG  //  Feldstrasse 133 //  CH-8004 Zurich
Tel +41 43 500 39 80 // GnuPG 0x7B588C67 // www.liip.ch<http://www.mn.gov/oet>

_______________________________________________
ckan-dev mailing list
ckan-dev at lists.okfn.org<mailto:ckan-dev at lists.okfn.org>
http://lists.okfn.org/mailman/listinfo/ckan-dev
Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev



--
Liip AG  //  Feldstrasse 133 //  CH-8004 Zurich
Tel +41 43 500 39 80<tel:%2B41%2043%20500%2039%2080> // GnuPG 0x7B588C67 // www.liip.ch<http://www.liip.ch/>

_______________________________________________
ckan-dev mailing list
ckan-dev at lists.okfn.org<mailto:ckan-dev at lists.okfn.org>
http://lists.okfn.org/mailman/listinfo/ckan-dev
Unsubscribe: http://lists.okfn.org/mailman/options/ckan-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20131009/fd3e72f0/attachment-0001.html>


More information about the ckan-dev mailing list