Beispiel #1
0
def tuplize_dict(data_dict):
    '''Takes a dict with keys of the form 'table__0__key' and converts them
    to a tuple like ('table', 0, 'key').

    Dict should be put through parse_dict before this function, to have
    values standardized.

    May raise a DataError if the format of the key is incorrect.
    '''
    tuplized_dict = {}
    for key, value in data_dict.iteritems():
        key_list = key.split('__')
        for num, key in enumerate(key_list):
            if num % 2 == 1:
                try:
                    key_list[num] = int(key)
                except ValueError:
                    raise df.DataError('Bad key')
        tuplized_dict[tuple(key_list)] = value
    return tuplized_dict
Beispiel #2
0
    def check_data_dict(self, data_dict, schema=None):
        '''Check if the return data is correct, mostly for checking out
        if spammers are submitting only part of the form'''

        # Resources might not exist yet (eg. Add Dataset)
        surplus_keys_schema = [
            '__extras', '__junk', 'state', 'groups', 'extras_validation',
            'save', 'return_to', 'resources', 'type', 'owner_org',
            'log_message', 'tag_string', 'tags', 'url', 'version', 'extras'
        ]

        if not schema:
            schema = self.form_to_db_schema()
        schema_keys = schema.keys()
        keys_in_schema = set(schema_keys) - set(surplus_keys_schema)

        missing_keys = keys_in_schema - set(data_dict.keys())
        if missing_keys:
            log.info('incorrect form fields posted, missing %s' % missing_keys)
            raise dictization_functions.DataError(data_dict)