[ckan-dev] Require at least one group for datasets (CKAN 1.8)

Knud Hinnerk Möller knud at datalysator.com
Tue Mar 12 15:22:10 UTC 2013


Hi, 

I'm currently trying to define a dataset schema that would require a dataset to have at least one group. My scenario is category information coming from the HTML form in the dataset edit template. 

So far I haven't been successful. I'll outline what I did below, maybe someone can point me in the right direction.

- I'm tring to do this in my plugin's form_to_db_schema() function
- form_to_db_package_schema() in logic/schema.py does this:

    schema['groups'] = {
            'id': [ignore_missing, unicode],
            '__extras': [ignore],
    }
- my first intuition was to change the value for 'groups' in my plugin as such:

    'groups': {
        'id': [not_missing, unicode],
        '__extras': [ignore],
    },
- that works when a group is set in the form. However, when no group is set, I get a server error: a KeyError is raised because of a missing 'id' key. The complete stacktrace is here: http://pastebin.com/K1HmM2Me
- after playing around a bit, I discovered that the 'data' object that gets passed to the validator functions specified in form_to_db_schema() doesn't yet contain the group information
- at this stage the 'groups' field always seems to be ckan.lib.navl.dictization_functions.Missing
- instead, the group information can be found in the __junk field. E.g., in the following case a group was selected from the drop-down list when the form was submitted:

…
('__junk',): {('groups', 0, 'id'): u'3cb2d53a-120d-4d2d-81f7-b79eaa3874f8',},
…
('groups',): <ckan.lib.navl.dictization_functions.Missing object at 0xa7d395cc>,
…

- in other words, I cannot seem to do any validation on 'groups'
- so, it seems that I would have to validate __junk by adding an entry for __junk to the schema in my plugin's form_to_db_schema(). However, that feels really counter-intuitive and potentially hairy. Surely there must be a better way?

Any help would be much appreciated!

Cheers,
Knud



More information about the ckan-dev mailing list