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

lucia.espona at wsl.ch lucia.espona at wsl.ch
Mon Aug 29 14:05:28 UTC 2016


 
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20160829/95d72e82/attachment-0002.html>


More information about the ckan-dev mailing list