src/ckanext-Metadata/ckanext/Metadata/plugin.py
def load_data_into_dict(cls, data_dict):
        '''
        a jinja2 template helper function.
        'extras' contains a list of dicts corresponding to the extras used to store arbitrary key value pairs in CKAN.
        This function moves each entry in 'extras' that is a common core metadata into 'custom_meta'

        Example:
        {'hi':'there', 'extras':[{'key': 'publisher', 'value':'USGS'}]}
        becomes
        {'hi':'there', 'custom_meta':{'publisher':'USGS'}, 'extras':[]}

        '''

        new_dict = copy.deepcopy(data_dict)
        common_metadata = [x['id'] for x in required_metadata+expanded_metadata]

        # needed for repeatable metadata
        # the original data_dict will have the creator set of data keys as follows:
        # creators:0:name # for creator#1
        # creators:0:email
        # creators:0:phone
        # creators:0:address
        # creators:0:organization
        # creators:0:is_a_group

        # creators:1:name # for creator#2
        # creators:1:email
        # creators:1:phone
        # creators:1:address
        # creators:1:organization
        # creators:1:is_a_group
        #
        # In the generated new new_dict we want the set of creator data as follows:
        # new_dict['custom_meta']['creators'] = [
        #                           {'name': name1-value, 'email': email1-value, 'phone': phone1-value, 'address': address1-value, 'organization': org1-value}
        #                           {'name': name2-value, 'email': email2-value, 'phone': phone2-value, 'address': address2-value, 'organization': org2-value}
        #                           { .......}
        #                        ]
        # the same logic above applies to any other repeatable element that we have in the schema

        try:
            new_dict['custom_meta']
        except KeyError:
            new_dict['custom_meta'] = {}

        repeatable_elements = ['creators', 'contributors', 'variables']
        for element in repeatable_elements:
            new_dict['custom_meta'][element] = []

        reduced_extras = []

        sub_name = ''
        sub_email = ''
        try:
            for extra in new_dict['extras']:
                if extra['key'] in common_metadata:
                    new_dict['custom_meta'][extra['key']] = extra['value']
                    # grab the submitter name and email to set default first creator name and email
                    if extra['key'] == 'sub_name':
                        sub_name = extra['value']
                    if extra['key'] == 'sub_email':
                        sub_email = extra['value']
                else:
                    # check if the key matches the any of the repeatable metadata element
                    data_key_parts = extra['key'].split(':')
                    if data_key_parts[0] not in repeatable_elements or len(data_key_parts) != 3:
                        reduced_extras.append(extra)

            # repeatable element key shorting is necessary so that a key like 'creators:10:name"
            # does not come before a key like 'creators:2:name'
            sorted_creator_keys = cls._get_sorted_repeatable_element_keys(new_dict['extras'], 'creators')
            sorted_contributor_keys = cls._get_sorted_repeatable_element_keys(new_dict['extras'], 'contributors')
            sorted_variable_keys = cls._get_sorted_repeatable_element_keys(new_dict['extras'], 'variables')

            cls._load_repeatable_elements_to_dict(new_dict, sorted_creator_keys, new_dict['extras'])
            cls._load_repeatable_elements_to_dict(new_dict, sorted_contributor_keys, new_dict['extras'])
            cls._load_repeatable_elements_to_dict(new_dict, sorted_variable_keys, new_dict['extras'])

            # add the default creator if no creator exists at this point
            set_default_creator(new_dict, sub_name, sub_email)

            new_dict['extras'] = reduced_extras

        except KeyError as ex:
            log.debug('''Expected key ['%s'] not found, attempting to move common core keys to subdictionary''',
                      ex.message)
            #this can happen when a form fails validation, as all the data will now be as key,value pairs, not under extras,
            #so we'll move them to the expected point again to fill in the values
            # e.g.
            # { 'foo':'bar','publisher':'somename'} becomes {'foo':'bar', 'custom_meta':{'publisher':'somename'}}

            keys_to_remove = []

            log.debug('common core metadata: {0}'.format(common_metadata))
            for key, value in new_dict.iteritems():
                #TODO remove debug
                log.debug('checking key: {0}'.format(key))
                if key in common_metadata:
                    #TODO remove debug
                    log.debug('adding key: {0}'.format(key))
                    new_dict['custom_meta'][key] = value
                    keys_to_remove.append(key)

                    # grab the submitter name and email to set default first creator name and email
                    if key == 'sub_name':
                        sub_name = value
                    if key == 'sub_email':
                        sub_email = value
                else:
                    # check if the key matches any of the repeatable metadata element
                    if key in repeatable_elements:
                        for repeat_item in value:
                            new_dict['custom_meta'][key].append(repeat_item)

            for key in keys_to_remove:
                del new_dict[key]

            # add the default creator if no creator exists at this point
            set_default_creator(new_dict, sub_name, sub_email)

        # remove any repeatable elements marked as deleted from the dict
        for element in repeatable_elements:
            valid_repeatables = [c for c in new_dict['custom_meta'][element] if c['delete'] != '1']
            new_dict['custom_meta'][element] = valid_repeatables

        return new_dict

        a jinja2 template helper function.
        'extras' contains a list of dicts corresponding to the extras used to store arbitrary key value pairs in CKAN.
        This function moves each entry in 'extras' that is a common core metadata into 'custom_meta'

        Example:
        {'hi':'there', 'extras':[{'key': 'publisher', 'value':'USGS'}]}
        becomes
        {'hi':'there', 'custom_meta':{'publisher':'USGS'}, 'extras':[]}

        
a jinja2 template helper function.
'extras' contains a list of dicts corresponding to the extras used to store arbitrary key value pairs in CKAN.
This function moves each entry in 'extras' that is a common core metadata into 'custom_meta'
a jinja2 template helper function . ' extras ' contains a list of dicts corresponding to the extras used to store arbitrary key value pairs in CKAN . This function moves each entry in ' extras ' that is a common core metadata into ' custom_meta '
def load_data_into_dict(cls, data_dict):
        new_dict = copy.deepcopy(data_dict)
        common_metadata = [x['id'] for x in required_metadata+expanded_metadata]
         In the generated new new_dict we want the set of creator data as follows:
         new_dict['custom_meta']['creators'] = [
                                   {'name': name1-value, 'email': email1-value, 'phone': phone1-value, 'address': address1-value, 'organization': org1-value}
                                   {'name': name2-value, 'email': email2-value, 'phone': phone2-value, 'address': address2-value, 'organization': org2-value}
                                   { .......}
                                ]
         the same logic above applies to any other repeatable element that we have in the schema
        try:
            new_dict['custom_meta']
        except KeyError:
            new_dict['custom_meta'] = {}
        repeatable_elements = ['creators', 'contributors', 'variables']
        for element in repeatable_elements:
            new_dict['custom_meta'][element] = []
        reduced_extras = []
        sub_name = ''
        sub_email = ''
        try:
            for extra in new_dict['extras']:
                if extra['key'] in common_metadata:
                    new_dict['custom_meta'][extra['key']] = extra['value']
                     grab the submitter name and email to set default first creator name and email
                    if extra['key'] == 'sub_name':
                        sub_name = extra['value']
                    if extra['key'] == 'sub_email':
                        sub_email = extra['value']
                else:
                     check if the key matches the any of the repeatable metadata element
                    data_key_parts = extra['key'].split(':')
                    if data_key_parts[0] not in repeatable_elements or len(data_key_parts) != 3:
                        reduced_extras.append(extra)
             repeatable element key shorting is necessary so that a key like 'creators:10:name"
             does not come before a key like 'creators:2:name'
            sorted_creator_keys = cls._get_sorted_repeatable_element_keys(new_dict['extras'], 'creators')
            sorted_contributor_keys = cls._get_sorted_repeatable_element_keys(new_dict['extras'], 'contributors')
            sorted_variable_keys = cls._get_sorted_repeatable_element_keys(new_dict['extras'], 'variables')
            cls._load_repeatable_elements_to_dict(new_dict, sorted_creator_keys, new_dict['extras'])
            cls._load_repeatable_elements_to_dict(new_dict, sorted_contributor_keys, new_dict['extras'])
            cls._load_repeatable_elements_to_dict(new_dict, sorted_variable_keys, new_dict['extras'])
             add the default creator if no creator exists at this point
            set_default_creator(new_dict, sub_name, sub_email)
            new_dict['extras'] = reduced_extras
        except KeyError as ex:
            log.debug('''Expected key ['%s'] not found, attempting to move common core keys to subdictionary''',
                      ex.message)
            this can happen when a form fails validation, as all the data will now be as key,value pairs, not under extras,
            so we'll move them to the expected point again to fill in the values
             e.g.
             { 'foo':'bar','publisher':'somename'} becomes {'foo':'bar', 'custom_meta':{'publisher':'somename'}}
            keys_to_remove = []
            log.debug('common core metadata: {0}'.format(common_metadata))
            for key, value in new_dict.iteritems():
                TODO remove debug
                log.debug('checking key: {0}'.format(key))
                if key in common_metadata:
                    TODO remove debug
                    log.debug('adding key: {0}'.format(key))
                    new_dict['custom_meta'][key] = value
                    keys_to_remove.append(key)
                     grab the submitter name and email to set default first creator name and email
                    if key == 'sub_name':
                        sub_name = value
                    if key == 'sub_email':
                        sub_email = value
                else:
                     check if the key matches any of the repeatable metadata element
                    if key in repeatable_elements:
                        for repeat_item in value:
                            new_dict['custom_meta'][key].append(repeat_item)
            for key in keys_to_remove:
                del new_dict[key]
             add the default creator if no creator exists at this point
            set_default_creator(new_dict, sub_name, sub_email)
         remove any repeatable elements marked as deleted from the dict
        for element in repeatable_elements:
            valid_repeatables = [c for c in new_dict['custom_meta'][element] if c['delete'] != '1']
            new_dict['custom_meta'][element] = valid_repeatables
        return new_dict
def load_data_into_dict ( cls , data_dict ) : new_dict = copy . deepcopy ( data_dict ) common_metadata = [ x [ 'id' ] for x in required_metadata + expanded_metadata ] try : new_dict [ 'custom_meta' ] except KeyError : new_dict [ 'custom_meta' ] = { } repeatable_elements = [ 'creators' , 'contributors' , 'variables' ] for element in repeatable_elements : new_dict [ 'custom_meta' ] [ element ] = [ ] reduced_extras = [ ] sub_name = '' sub_email = '' try : for extra in new_dict [ 'extras' ] : if extra [ 'key' ] in common_metadata : new_dict [ 'custom_meta' ] [ extra [ 'key' ] ] = extra [ 'value' ] if extra [ 'key' ] == 'sub_name' : sub_name = extra [ 'value' ] if extra [ 'key' ] == 'sub_email' : sub_email = extra [ 'value' ] else : data_key_parts = extra [ 'key' ] . split ( ':' ) if data_key_parts [ 0 ] not in repeatable_elements or len ( data_key_parts ) != 3 : reduced_extras . append ( extra ) sorted_creator_keys = cls . _get_sorted_repeatable_element_keys ( new_dict [ 'extras' ] , 'creators' ) sorted_contributor_keys = cls . _get_sorted_repeatable_element_keys ( new_dict [ 'extras' ] , 'contributors' ) sorted_variable_keys = cls . _get_sorted_repeatable_element_keys ( new_dict [ 'extras' ] , 'variables' ) cls . _load_repeatable_elements_to_dict ( new_dict , sorted_creator_keys , new_dict [ 'extras' ] ) cls . _load_repeatable_elements_to_dict ( new_dict , sorted_contributor_keys , new_dict [ 'extras' ] ) cls . _load_repeatable_elements_to_dict ( new_dict , sorted_variable_keys , new_dict [ 'extras' ] ) set_default_creator ( new_dict , sub_name , sub_email ) new_dict [ 'extras' ] = reduced_extras except KeyError as ex : log . debug ( '''Expected key ['%s'] not found, attempting to move common core keys to subdictionary''' , ex . message ) keys_to_remove = [ ] log . debug ( 'common core metadata: {0}' . format ( common_metadata ) ) for key , value in new_dict . iteritems ( ) : log . debug ( 'checking key: {0}' . format ( key ) ) if key in common_metadata : log . debug ( 'adding key: {0}' . format ( key ) ) new_dict [ 'custom_meta' ] [ key ] = value keys_to_remove . append ( key ) if key == 'sub_name' : sub_name = value if key == 'sub_email' : sub_email = value else : if key in repeatable_elements : for repeat_item in value : new_dict [ 'custom_meta' ] [ key ] . append ( repeat_item ) for key in keys_to_remove : del new_dict [ key ] set_default_creator ( new_dict , sub_name , sub_email ) for element in repeatable_elements : valid_repeatables = [ c for c in new_dict [ 'custom_meta' ] [ element ] if c [ 'delete' ] != '1' ] new_dict [ 'custom_meta' ] [ element ] = valid_repeatables return new_dict
a jinja2 template helper function.
a jinja2 template helper function .
'''
        a jinja2 template helper function.
        'extras' contains a list of dicts corresponding to the extras used to store arbitrary key value pairs in CKAN.
        This function moves each entry in 'extras' that is a common core metadata into 'custom_meta'

        Example:
        {'hi':'there', 'extras':[{'key': 'publisher', 'value':'USGS'}]}
        becomes
        {'hi':'there', 'custom_meta':{'publisher':'USGS'}, 'extras':[]}

        ''' # needed for repeatable metadata # the original data_dict will have the creator set of data keys as follows: # creators:0:name # for creator#1 # creators:0:email # creators:0:phone # creators:0:address # creators:0:organization # creators:0:is_a_group # creators:1:name # for creator#2 # creators:1:email # creators:1:phone # creators:1:address # creators:1:organization # creators:1:is_a_group # # In the generated new new_dict we want the set of creator data as follows: # new_dict['custom_meta']['creators'] = [ #                           {'name': name1-value, 'email': email1-value, 'phone': phone1-value, 'address': address1-value, 'organization': org1-value} #                           {'name': name2-value, 'email': email2-value, 'phone': phone2-value, 'address': address2-value, 'organization': org2-value} #                           { .......} #                        ] # the same logic above applies to any other repeatable element that we have in the schema # grab the submitter name and email to set default first creator name and email # check if the key matches the any of the repeatable metadata element # repeatable element key shorting is necessary so that a key like 'creators:10:name" # does not come before a key like 'creators:2:name' # add the default creator if no creator exists at this point #this can happen when a form fails validation, as all the data will now be as key,value pairs, not under extras, #so we'll move them to the expected point again to fill in the values # e.g. # { 'foo':'bar','publisher':'somename'} becomes {'foo':'bar', 'custom_meta':{'publisher':'somename'}} #TODO remove debug #TODO remove debug # grab the submitter name and email to set default first creator name and email # check if the key matches any of the repeatable metadata element # add the default creator if no creator exists at this point # remove any repeatable elements marked as deleted from the dict
cls
data_dict
examples
None