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

lucia.espona at wsl.ch lucia.espona at wsl.ch
Fri Sep 2 13:16:32 UTC 2016


 Thanks Stefan

The function is similar to my custom one, I couldn't use that function directly, there are issues looping over the keys and values when the custom field is still empty (value is <ckan.lib.navl.dictization_functions.Missing object at 0x7fa8a40dd0d0>). I am not sure how avoid this so that the new custom field is just an empty string or None, untill a value is assigned to it. For the moment I assign it an empty string in the converter in case there is no value defined in the extras.

I improved my function and the extension, it works anyway (https://github.com/espona/ckanext-hierarchy/blob/master/ckanext/hierarchy/plugin.py).

Best,
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: Stefan Oderbolz 
Sent by: "ckan-dev" 
Date: 31.08.2016 10:48
Subject: Re: [ckan-dev] Error adding custom field to organization	(IGroupForm)

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           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

_______________________________________________
 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
  
_______________________________________________
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/20160902/985bf78b/attachment-0002.html>


More information about the ckan-dev mailing list