[ckan-dev] Custom fields that are arrays or objects with fields

Renato Guevara guevara.renato at gmail.com
Thu Oct 30 15:13:04 UTC 2014


Ian,

Thank you for your response.  I'm looking through both ckanext-scheming and
ckanext-fluent, but I'm a bit confused on how you use them.  I get the
concept of how ckanext-scheming is used to define your own json schema.
So, when you use the ckanext-fluent plugin, how do you modify
ckanext-scheming to use fluent?  In addition to that, I see that
ckanext-scheming implements the IDatasetForm class, but doesn't implement
the package_create/modify/show functions required.  Does that mean that it
is added by the user?  I'm a bit fuzzy on that.  Would you be able to help
me run through the steps.

For example, before I knew about this plugin, I had written the following
plugin to add my customized fields.

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

class DescribeFieldsPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
        p.implements(p.IDatasetForm)
        p.implements(p.IConfigurer)

        def _modify_package_schema(self, schema):
                schema.update({
                        'webService': [tk.get_validator('ignore_missing'),
                                tk.get_converter('convert_to_extras')]
                })
                return schema

        def create_package_schema(self):
                schema = super(DescribeFieldsPlugin,
self).create_package_schema()
                schema = self._modify_package_schema(schema)
                return schema

        def update_package_schema(self):
                schema = super(DescribeFieldsPlugin,
self).update_package_schema()
                schema = self._modify_package_schema(schema)
                return schema

        def show_package_schema(self):
                schema = super(DescribeFieldsPlugin,
self).show_package_schema()

schema['tags']['__extras'].append(tk.get_converter('free_tags_only'))

                schema.update({
                        'webService': [
                                tk.get_converter('convert_from_extras'),
                                tk.get_validator('ignore_missing')]
                })
                return schema

        def is_fallback(self):
                # Return True to register this plugin as the default
handler for
                # package types not handled by any other IDatasetForm
plugin.
                return True

        def package_types(self):
                # This plugin doesn't handle any special package types, it
just
                # registers itself as the default (above).
                return []

        #update config
        def update_config(self, config):
                # Add this plugin's templates dir to CKAN's
extra_template_paths, so
                # that CKAN will use this plugin's custom templates.
                tk.add_template_directory(config, 'templates')

I wanted to add another field called "queryLanguage" which is essentially a
list of options.  So, I can have something like

queryLanguage: ["sql", "sparql"].

This is what drove me to ask the question.  Do I use this plugin in
conjunction with ckanext-scheming?

Thanks for all your help!

Renato Guevara


On Thu, Oct 30, 2014 at 9:06 AM, Ian Ward <ian at excess.org> wrote:

> It is possible to have custom fields with values that appear as JSON
> objects or lists. In ckanext-fluent I return and accept JSON objects
> with language codes as keys:
> https://github.com/open-data/ckanext-fluent
>
> You need to think about how you're going to handle form inputs as well
> as the "ideal" json types if you want your fields to be editable
> through the normal ckan interface.
>
> I will be starting another extension that will represent repeated text
> fields as JSON lists of strings. Currently I'm storing that kind of
> thing as delimited text, but it's not a very nice solution.
>
> Ian
>
> On Wed, Oct 29, 2014 at 5:19 PM, Renato Guevara
> <guevara.renato at gmail.com> wrote:
> > Is it possible to add custom fields that can be an array of values
> and/or is
> > it possible to add custom fields that are objects with field-value
> mappings.
> >
> > For example,
> >
> > I'd like to add the following
> >
> > queryLanguage: [ sql, sparql]
> >
> > or
> >
> > cat: {
> >           type: animal,
> >           legs: four
> >        }
> >
> > It seems that the only type of field I can add is a simple key-value pair
> > where the value is one string.  There is also a concept of vocabulary and
> > tags, but it also seems that the field that has a tag only holds one tag
> and
> > not multiple.
> >
> > Any help would be very much appreciated.
> >
> > Thanks!
> >
> > Renato Guevara
> >
> > _______________________________________________
> > ckan-dev mailing list
> > ckan-dev at lists.okfn.org
> > https://lists.okfn.org/mailman/listinfo/ckan-dev
> > Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
> >
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> https://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20141030/4ae65c1a/attachment-0003.html>


More information about the ckan-dev mailing list