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

Stefan Oderbolz stefan.oderbolz at liip.ch
Wed Aug 31 08:47:40 UTC 2016


Hi Lucia,

I quickly checked the implementation of ckanext-scheming, it seems this was
a problem they ran into as well, here is their approach to fix it:
https://github.com/ckan/ckanext-scheming/blob/083712d6bc00fcb5aeaf91a614769ac16d5c7a3b/ckanext/scheming/converters.py#L3-L23

- Stefan

On Wed, Aug 31, 2016 at 9:30 AM, <lucia.espona at wsl.ch> wrote:

> 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
> <http://envidat02.wsl.ch:5000/organization/clench#> view
> <http://envidat02.wsl.ch:5000/organization/clench#>
> >>  <http://envidat02.wsl.ch:5000/organization/clench#>{% block info %}
> Module /usr/lib/ckan/default/src/ckan/ckan/templates/snippets/
> organization.html:*26* in block "info"
> <http://envidat02.wsl.ch:5000/organization/clench#> view
> <http://envidat02.wsl.ch:5000/organization/clench#>
> >>  <http://envidat02.wsl.ch:5000/organization/clench#>{% block inner %}
> Module /usr/lib/ckan/default/src/ckan/ckan/templates/snippets/
> organization.html:*52* in block "inner"
> <http://envidat02.wsl.ch:5000/organization/clench#> view
> <http://envidat02.wsl.ch:5000/organization/clench#>
> >>  <http://envidat02.wsl.ch:5000/organization/clench#>{% block nums %}
> Module /usr/lib/ckan/default/src/ckan/ckan/templates/snippets/
> organization.html:*56* in block "nums"
> <http://envidat02.wsl.ch:5000/organization/clench#> view
> <http://envidat02.wsl.ch:5000/organization/clench#>
> >>  <http://envidat02.wsl.ch:5000/organization/clench#><dd>{{
> h.SI_number_span(organization.num_followers) }}</dd>
> Module ckan.lib.helpers:*1930* in SI_number_span
> <http://envidat02.wsl.ch:5000/organization/clench#> view
> <http://envidat02.wsl.ch:5000/organization/clench#>
> >>  <http://envidat02.wsl.ch:5000/organization/clench#>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
>
> _______________________________________________
> 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
>
>


-- 
Liip AG  // Limmatstrasse 183 //  CH-8005 Zürich
Tel +41 43 500 39 80 // GnuPG 0x7B588C67 // www.liip.ch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20160831/4e50aea2/attachment-0003.html>


More information about the ckan-dev mailing list