[ckan-dev] Error adding custom field to organization (IGroupForm)

lucia.espona at wsl.ch lucia.espona at wsl.ch
Wed Aug 31 07:30:26 UTC 2016


 Dear all

The problem was the ckan.logic.converters.convert_to_extras function, it basically didn't do anything. I debuged the method and I do not see how that can work with the data dictionary that it gets as input. I have written my own function and now it all works, I am not sure if it is correct for other cases. I pasted below the code, it is also available at https://github.com/espona/ckanext-hierarchy/blob/master/ckanext/hierarchy/plugin.py

Best,
Lucia

def custom_convert_from_extras(key, data, errors, context):
    for data_key in data.keys():
        if (data_key[0] == 'extras'):
            data_value = data[data_key]
            if(data_value['key'] == key[-1]):
                data[key] = data_value['value']
                del data[data_key]
                break


 

_________________________________________________________
Dr. Lucia Espona Pernas

Swiss Federal Institute for Forest, Snow and Landscape Research WSL
Hauptgebäaude Labortrakt (HL C21)
Zürcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 739 28 71 phone direct
+41 44 739 21 11 reception

www.wsl.ch

-----"ckan-dev" <ckan-dev-bounces at lists.okfn.org> wrote: -----
To: CKAN Development Discussions <ckan-dev at lists.okfn.org>
From: lucia.espona at wsl.ch
Sent by: "ckan-dev" 
Date: 30.08.2016 11:46
Subject: Re: [ckan-dev] Error adding custom field to organization	(IGroupForm)

 Dear all,

I managed to get rid of the error by using the proper default schema, but when I give a value to the custom field, save it and then try to modify it, it appears only in the extras. Should the "convert_from_extras" do that? I couldn't find any working example of custom organization field.

This is the problematic function:

class HierarchyForm(p.SingletonPlugin, DefaultOrganizationForm):

    p.implements(p.IGroupForm, inherit=True)

[...]
    def db_to_form_schema(self):
        # Import core converters and validators
        _convert_from_extras = p.toolkit.get_converter('convert_from_extras')
        _ignore_missing = p.toolkit.get_validator('ignore_missing')
        default_validators = [_convert_from_extras, _ignore_missing]

        schema = { 'shortname':default_validators }
        schema.update(s.default_show_group_schema())

        log.debug("New Organization Schema: "  + str(schema))

        return schema

Thanks,
Lucia
_________________________________________________________
Dr. Lucia Espona Pernas

Swiss Federal Institute for Forest, Snow and Landscape Research WSL
Hauptgebäaude Labortrakt (HL C21)
Zürcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 739 28 71 phone direct
+41 44 739 21 11 reception

www.wsl.ch

-----"ckan-dev" <ckan-dev-bounces at lists.okfn.org> wrote: -----
To: "CKAN Development Discussions" <ckan-dev at lists.okfn.org>
From: lucia.espona at wsl.ch
Sent by: "ckan-dev" 
Date: 29.08.2016 16:05
Subject: [ckan-dev] Error adding custom field to organization (IGroupForm)

 
Dear all,

I am trying to add a custom field to the organizations extending the IGroupForm. I cannot use ckanext-scheming because I am modifying ckanext-hierarchy.

I pasted below the error I get when trying to view an organization and the plugin code below. If someone has added a custom organization field, I would be grateful if he/she can share the code with me.

Many thanks in advance,
Lucia

....

 Module /usr/lib/ckan/default/src/ckan/ckan/templates/snippets/organization.html:20 in top-level template code           view
 
>>  {% block info %}  
 Module /usr/lib/ckan/default/src/ckan/ckan/templates/snippets/organization.html:26 in block "info"           view
 
>>  {% block inner %}  
 Module /usr/lib/ckan/default/src/ckan/ckan/templates/snippets/organization.html:52 in block "inner"           view
 
>>  {% block nums %}  
 Module /usr/lib/ckan/default/src/ckan/ckan/templates/snippets/organization.html:56 in block "nums"           view
 
>>  <dd>{{ h.SI_number_span(organization.num_followers) }}</dd>  
 Module ckan.lib.helpers:1930 in SI_number_span           view
 
>>  number = int(number)  UndefinedError: 'dict object' has no attribute 'num_followers'


plugin.py:
[...]
class HierarchyForm(p.SingletonPlugin, DefaultOrganizationForm):

    p.implements(p.IGroupForm, inherit=True)

    # IGroupForm

    def group_types(self):
        return ('organization',)

    def group_controller(self):
        return 'organization'

    def setup_template_variables(self, context, data_dict):
        from pylons import tmpl_context as c
        model = context['model']
        group_id = data_dict.get('id')
        if group_id:
            group = model.Group.get(group_id)
            c.allowable_parent_groups = \
                group.groups_allowed_to_be_its_parent(type='organization')
        else:
            c.allowable_parent_groups = model.Group.all(
                                                group_type='organization')

    def form_to_db_schema_options(self, options):
        ''' This allows us to select different schemas for different
        purpose eg via the web interface or via the api or creation vs
        updating. It is optional and if not available form_to_db_schema
        should be used.
        If a context is provided, and it contains a schema, it will be
        returned.
        '''
        schema = options.get('context', {}).get('schema', None)
        if schema:
            return schema

        if options.get('api'):
            if options.get('type') == 'create':
                return self.form_to_db_schema_api_create()
ÿÿÿÿÿÿÿÿÿÿÿ else:
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ return self.form_to_db_schema_api_update()
ÿÿÿÿÿÿÿ else:
ÿÿÿÿÿÿÿÿÿÿÿ return self.form_to_db_schema()

ÿÿÿ def form_to_db_schema_api_create(self):
ÿÿÿÿÿÿÿ schema = super(HierarchyForm, self).form_to_db_schema_api_create()
ÿÿÿÿÿÿÿ schema = self._modify_group_schema(schema)
ÿÿÿÿÿÿÿ return schema

ÿÿÿ def form_to_db_schema_api_update(self):
ÿÿÿÿÿÿÿ schema = super(HierarchyForm, self).form_to_db_schema_api_update()
ÿÿÿÿÿÿÿ schema = self._modify_group_schema(schema)
ÿÿÿÿÿÿÿ return schema

ÿÿÿ def form_to_db_schema(self):
ÿÿÿÿÿÿÿ schema = super(HierarchyForm, self).form_to_db_schema()
ÿÿÿÿÿÿÿ schema = self._modify_group_schema(schema)
ÿÿÿÿÿÿÿ return schema

ÿÿÿ def _modify_group_schema(self, schema):
ÿÿÿÿÿÿÿÿ #Import core converters and validators
ÿÿÿÿÿÿÿ _convert_to_extras = p.toolkit.get_converter('convert_to_extras')
ÿÿÿÿÿÿÿ _ignore_missing = p.toolkit.get_validator('ignore_missing')

ÿÿÿÿÿÿÿ default_validators = [_ignore_missing, _convert_to_extras]
ÿÿÿÿÿÿÿ schema.update({
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ 'shortname':default_validators
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ })
ÿÿÿÿÿÿÿ return schema

ÿÿÿ def db_to_form_schema(self):
ÿÿÿÿÿÿÿ # Import core converters and validators
ÿÿÿÿÿÿÿ _convert_from_extras = p.toolkit.get_converter('convert_from_extras')
ÿÿÿÿÿÿÿ _ignore_missing = p.toolkit.get_validator('ignore_missing')

ÿÿÿÿÿÿÿ schema = super(HierarchyForm, self).form_to_db_schema()

ÿÿÿÿÿÿÿ default_validators = [_convert_from_extras, _ignore_missing]
ÿÿÿÿÿÿÿ schema.update({
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ 'shortname':default_validators
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ })
ÿÿÿÿÿÿÿ return schema

_________________________________________________________
Dr. Lucia Espona Pernas

Swiss Federal Institute for Forest, Snow and Landscape Research WSL
Hauptgeb„aude Labortrakt (HL C21)
Zrcherstrasse 111
8903 Birmensdorf
Switzerland

+41 44 739 28 71 phone direct
+41 44 739 21 11 reception

www.wsl.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
 
_______________________________________________
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/20160831/41340f5a/attachment-0003.html>


More information about the ckan-dev mailing list