[ckan-dev] Custom Search Facets

Renato Guevara guevara.renato at gmail.com
Tue Oct 28 19:27:53 UTC 2014


One of the issues I have is the following:

I have a plugin that is adding new custom fields.

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

        def get_helpers(self):
                return {'queryLanguages': queryLanguages}

        def _modify_package_schema(self, schema):
                '''
                schema.update({
                        'queryLanguage':
[tk.get_validator('ignore_missing'),

tk.get_converter('convert_to_extras')]
                })
                '''
                schema.update({
                        'queryLanguage':
[tk.get_validator('ignore_missing'),

tk.get_converter('convert_to_tags')('queryLanguages')]
                })
                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.update({
                        'queryLanguage':
[tk.get_converter('convert_from_extras'),

tk.get_validator('ignore_missing')]
                })
                '''

schema['tags']['__extras'].append(tk.get_converter('free_tags_only'))
                schema.update({
                        'queryLanguage': [

tk.get_converter('convert_from_tags')('queryLanguages'),
                        tk.get_validator('ignore_missing')]
                })
                return schema

def create_queryLanguages():
        user = tk.get_action('get_site_user')({'ignore_auth': True}, {})
        context = {'user': user['name']}
        try:
                data = {'id': 'queryLanguages'}
                tk.get_action('vocabulary_show')(context, data)
        except tk.ObjectNotFound:
                data = {'name': 'queryLanguages'}
                vocab = tk.get_action('vocabulary_create')(context, data)
                for tag in (u'sparql-language',u'sql'):
                        data = {'name': tag, 'vocabulary_id': vocab['id']}
                        tk.get_action('tag_create')(context, data)

def queryLanguages():
        create_queryLanguages()
        try:
                tag_list = tk.get_action('tag_list')
                queryLanguages = tag_list(data_dict={'vocabulary_id':
'queryLanguages'})
                return queryLanguages
        except tk.ObjectNotFound:
                return None


However, when I add a facet for queryLanguage, should I add it as an
extra?  Because what is happening is that the term "sparql-language' is
getting broken up into two different facets.  Do you know why this may be
happening?

On Tue, Oct 28, 2014 at 4:19 AM, Rachel Knowler <rachel.knowler at liip.ch>
wrote:

>  Sorry, I should have mentioned this in my reply because it's not covered
> well in the docs. The new facet that I added is called 'Level'. It's added
> to all my datasets as an extra field, during harvesting. In this line, I
> add it to the facets_dict:
>
> facets_dict['extras_level'] = p.toolkit._('Level')
>
> It's enough to use the key 'extras_level' to tell CKAN to get the facet
> from the extras. The right-hand side is the name of the facet.
>
> Best,
> Rachel
>
>
>
>
> On 28.10.2014 09:12, Rachel Knowler wrote:
>
> Hi Renato,
>
> To add a custom facet search you have to implement the iFacets interface
> in your plugin. This is described in the documentation here:
> http://docs.ckan.org/en/latest/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IFacets.
> I recently did this and you can see my code for it here:
> https://github.com/openresearchdata/ckanext-ord-hierarchy/blob/master/ckanext/ord_hierarchy/plugin.py#L210
> .
>
> Hope that helps!
>
> - Rachel
>
> On 27.10.2014 16:02, Renato Guevara wrote:
>
> I was able to successfully add a custom field to my dataset.  In fact, it
> was a vocabulary with its corresponding tags.  However, now, I'm looking
> into adding a custom facet search for this specific custom field.  In this
> case, the name of my custom field is queryLanguage.  I looked through the
> forum and documentation to see if there were any tutorials but I couldn't
> find any.  Would someone be able to point me in the right direction.
> Again, I'm trying to add a custom facet search like there is on the default
> Dataset search page on the left side.  Right now, the default ones are
> "Publishers", "Subjects", "Organizations", "Groups", "Tags", "Formats", and
> "Licenses".  I'd like to add "Query Languages".
>
>  Thanks,
>
>  Renato Guevara
>
>
> _______________________________________________
> ckan-dev mailing listckan-dev at lists.okfn.orghttps://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
>
>
> --
> Liip AG // Limmatstrasse 183 // CH-8005 Zürich
> Tel +41 43 500 39 80 // Mob +41 78 658 26 12 // www.liip.ch
>
>
> --
> Liip AG // Limmatstrasse 183 // CH-8005 Zürich
> Tel +41 43 500 39 80 // Mob +41 78 658 26 12 // www.liip.ch
>
>
> _______________________________________________
> 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/20141028/078279a2/attachment-0003.html>


More information about the ckan-dev mailing list