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

lucia.espona at wsl.ch lucia.espona at wsl.ch
Tue Aug 30 09:46:04 UTC 2016


 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20160830/60834afc/attachment-0003.html>


More information about the ckan-dev mailing list